Moving a form...

m_adil
m_adil used Ask the Experts™
on
How can I make a form to move when some one clicks and drags it from TPanel instead of Form's title bar?
I've the code which does the same from form's client area but it does not work when action is performed on a TPanel.

here is the code... and i need to make it work with TPanel instead of Form

procedure WMNCHitTest(var M: TWMNCHitTest); message wm_NCHitTest;
......

procedure TMainFrm.WMNCHitTest(var M: TWMNCHitTest);
begin
  inherited;                    { call the inherited message handler }
  if  M.Result = htClient then  { is the click in the client area?   }
    M.Result := htCaption;      { if so, make Windows think it's     }
                                { on the caption bar.                }
end;


Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2004

Commented:
you could do something like this,
no tricky messages

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls;

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

var
  Form1: TForm1;
  InMove : Boolean = False;
  MovePoint : TPoint;

implementation

{$R *.dfm}

procedure TForm1.Panel1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  MovePoint := Point(x,y);
  InMove := True;
end;

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

procedure TForm1.Panel1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if InMove then
  begin
    top := top - (MovePoint.Y - y);
    left := left - (MovePoint.x - x);
  end;
end;

end.

meikl ;-)
Commented:
only the OnMouseDown event is neccesary:

*****
procedure TForm1.Panel1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
 ReleaseCapture;
 Perform(WM_NCLBUTTONDOWN,HTCAPTION,0);
end;
*****

that's it ;-)
Top Expert 2004

Commented:
hey, cool, pika ;-)
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Commented:
lolz, thx kretzschmar :-D
now you can use that code, too :-)

Commented:
straight from my website:

procedure TForm1.Panel1MouseDown(Sender: TObject; Button: TMouseButton;
    Shift: TShiftState; X, Y: Integer);
const
  SC_DragMove = $F012;  { a magic number }
begin
  ReleaseCapture;
  panel1.perform(WM_SysCommand, SC_DragMove, 0);
end;



The URL is:

http://www.drdelphi.com/delphi/right/tips/compdrag.htm


Good luck!!

Author

Commented:
Great!!! thanks.

Commented:
your welcome ;-)
thanx for the points :D
laterz :-)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial