Recent

Author Topic: Moving/dragging TPanel in TForm  (Read 14284 times)

asdf

  • Sr. Member
  • ****
  • Posts: 310
Moving/dragging TPanel in TForm
« on: October 30, 2010, 01:58:29 pm »
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.
Lazarus 1.2.4 / Win 32 / THAILAND

jixian.yang

  • Full Member
  • ***
  • Posts: 173
Re: Moving/dragging TPanel in TForm
« Reply #1 on: October 30, 2010, 02:39:45 pm »
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

  • Sr. Member
  • ****
  • Posts: 310
Re: Moving/dragging TPanel in TForm
« Reply #2 on: October 31, 2010, 03:22:18 am »
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?
Lazarus 1.2.4 / Win 32 / THAILAND

w135n03

  • Newbie
  • Posts: 6
Re: Moving/dragging TPanel in TForm
« Reply #3 on: October 31, 2010, 05:00:10 am »
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

  • Sr. Member
  • ****
  • Posts: 310
Re: Moving/dragging TPanel in TForm
« Reply #4 on: October 31, 2010, 06:29:27 am »
One more feature please.
Is there any way to make it like "AlwayShowOnTop"?
Since it was sometimes hidden by some displaying controls.
Lazarus 1.2.4 / Win 32 / THAILAND

jixian.yang

  • Full Member
  • ***
  • Posts: 173
Re: Moving/dragging TPanel in TForm
« Reply #5 on: October 31, 2010, 01:15:50 pm »
If there must be a "AlwayShowOnTop" feature, we prefer TForm to TPanel.

Here the Form1 worked as a "AlwayShowOnTop" TPanel:

program project1;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Interfaces, // this includes the LCL widgetset
  Controls, Forms, Unit1, Unit2
  { you can add units after this };

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TForm2, Form2);
  Application.CreateForm(TForm1, Form1);
  Form1.Parent := Form2;
  Form1.Top:=0;
  Form1.Left:=0;
  Form1.BorderStyle:=bsNone;
  Form1.FormStyle:=fsStayOnTop;
  Form1.Show;
  Application.Run;
end.

asdf

  • Sr. Member
  • ****
  • Posts: 310
Re: Moving/dragging TPanel in TForm
« Reply #6 on: October 31, 2010, 03:15:00 pm »
Now the calculator can be moved/dragged perfect.

But if I click to show the calculator before showing the TPageControl, the calculator was always hidden behind the TPageControl.

On the other hand, if I I click to show the calculator "after" showing the TPageControl, the calculator was always on top of everything (Sorry, I don't need to create another form).

Help me please.

Lazarus 1.2.4 / Win 32 / THAILAND

w135n03

  • Newbie
  • Posts: 6
Re: Moving/dragging TPanel in TForm
« Reply #7 on: October 31, 2010, 09:28:58 pm »
Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

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

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    PageControl1: TPageControl;
    Panel1: TPanel;
    TabSheet1: TTabSheet;
    procedure Button1Click(Sender: TObject);
    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, isTop : 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.Button1Click(Sender: TObject);
begin
  if isTop = 0 then begin
    Panel1.SendToBack;
    isTop := 1;
  end
  else if isTop = 1 then begin
    PageControl1.SendToBack;
    isTop := 0;
  end;
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.

I add some lines of code, may be it help. Sorry for my English

arbelest

  • New Member
  • *
  • Posts: 15
Re: Moving/dragging TPanel in TForm
« Reply #8 on: November 01, 2010, 01:46:58 am »
The code is OK, but i think it better if you create your own components.
It will make your code more modular and reuseable.

About the code, you can modify w135n03's code like this
Code: [Select]
.....
procedure TForm1.Button1Click(Sender: TObject);
begin
    Panel1.visible=not Panel1.visible;
    if Panel1.visible then
       Panel1.BringToFront;
end;
...
It will make your calculator panel show on top when toggle visible.



asdf

  • Sr. Member
  • ****
  • Posts: 310
Re: Moving/dragging TPanel in TForm
« Reply #9 on: November 01, 2010, 12:28:46 pm »
    Panel1.visible=not Panel1.visible;
 

There is an error message against this line.
Error: Illegal Expression
Lazarus 1.2.4 / Win 32 / THAILAND

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Moving/dragging TPanel in TForm
« Reply #10 on: November 01, 2010, 12:37:39 pm »
You are missing the ':' before the '='.

Code: [Select]
Panel1.visible := not Panel1.visible;

asdf

  • Sr. Member
  • ****
  • Posts: 310
Re: Moving/dragging TPanel in TForm
« Reply #11 on: November 01, 2010, 01:53:32 pm »
The result is the same ...

If I click to show the calculator before showing the TPageControl, the calculator was always hidden behind the TPageControl. Why the calculator could not brought'ToFront?

On the other hand, if I I click to show the calculator "after" showing the TPageControl, the calculator was always on top of everything. Very strange!

I tried to use the code posted by: w135n03
(and I created a procedure to hide the TPageControl ......

Procedure TMajorForm_Form.SendToBackTPageControl;
  begin
  for i := 0 to MajorForm_Form.ControlCount - 1 do
  if MajorForm_Form.Controls is TPageControl then
  //MajorForm_Form.Controls.Visible:= false;
  MajorForm_Form.Controls.SendToBack;
end;                                   

But still not perfect, I'm afraid that ...
MajorForm_Form.Controls.SendToBack;
is still wrong.




Lazarus 1.2.4 / Win 32 / THAILAND

arbelest

  • New Member
  • *
  • Posts: 15
Re: Moving/dragging TPanel in TForm
« Reply #12 on: November 02, 2010, 02:09:56 am »
On the other hand, if I I click to show the calculator "after" showing the TPageControl, the calculator was always on top of everything. Very strange!

Sorry i confuse about "always on top of everything". Did you mean overlap all other panel, including top panel contain toggle button etc ? What is calculator's control parent ? If it is Main Form, of course it will overlap all other control inside main form.
Assuming it's OK if calculator always on top of everything and PageControl is toggle too, you can try :
Code: [Select]
...
procedure TMainForm.ButtonToggleCalculatorClick(Sender: TObject);
begin
    PanelCalculator.visible:=not PanelCalculator.visible;
    if PanelCalculator.visible then
       PanelCalculator.BringToFront;
end;
...
procedure TMainForm.ButtonTogglePageControlClick(Sender: TObject);
begin
   PageControl1.visible:=not PageControl1.visible
   // force bring calculator to front if it visible
   if PanelCalculator.visible then
      PanelCalculator.BringToFront;
end;
...

@typo: thanks for your correction  :D

asdf

  • Sr. Member
  • ****
  • Posts: 310
Re: Moving/dragging TPanel in TForm
« Reply #13 on: November 03, 2010, 04:38:24 am »
Everything is in only one form.
When a TPageControl shows, it will fit the width and  height of Form except TMenu and TToolbar.
Lazarus 1.2.4 / Win 32 / THAILAND

asdf

  • Sr. Member
  • ****
  • Posts: 310
Re: Moving/dragging TPanel in TForm
« Reply #14 on: November 08, 2010, 08:37:51 am »
Code: [Select]
Panel1.visible := not Panel1.visible;

Could anybody explain the meaning of this syntax please?
Lazarus 1.2.4 / Win 32 / THAILAND

 

TinyPortal © 2005-2018