Forum > LCL

Moving/dragging TPanel in TForm

(1/4) > >>

asdf:
I have made a calculator, all buttons and displays are on a TPanel.
I'd like the user can move/drag to some place they may need.
But I could not do, kindly help.

jixian.yang:
The code for form, it is easy to be panel:

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs;

type

  { TForm1 }

  TForm1 = class(TForm)
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { private declarations }
  public
    { public declarations }

  end;

var
  Form1: TForm1;
  oldx,oldy,isMove: integer;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if isMove=1 then
  begin
    left:=left +x-oldx;
    top:=top +y-oldy;
  end;

end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  oldx:=x;
  oldy:=y;
  isMove:=1;
end;

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  isMove:=0;
end;

end.

asdf:
Dear jixian.yang

Your code moved the TForm, it did not move the TPanel.
I just need to move the TPanel in the TForm, not the TForm.
Could you help me please?

w135n03:
try this, i fix some code from jixian.yang

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  ExtCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Panel1: TPanel;
    procedure Panel1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Panel1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer
      );
    procedure Panel1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;
  oldX, oldY, isMove : Integer;

implementation

{ TForm1 }

procedure TForm1.Panel1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  oldX := X;
  oldY := Y;
  isMove := 1;
end;

procedure TForm1.Panel1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if isMove = 1 then begin
    Panel1.Left := Panel1.Left + X - oldX;
    Panel1.Top := Panel1.Top + Y - oldY;
  end;
end;

procedure TForm1.Panel1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  isMove := 0;
end;

initialization
  {$I unit1.lrs}

end.

asdf:
One more feature please.
Is there any way to make it like "AlwayShowOnTop"?
Since it was sometimes hidden by some displaying controls.

Navigation

[0] Message Index

[#] Next page

Go to full version