Mega Code Archive

 
Categories / Delphi / Forms
 

Drag the form by the client area, NOT the title bar

Title: Drag the form by the client area, NOT the title bar This lets the user drag the form by the client area, but not the title bar. To mannually disable such dragging, set Form1.AllowDrag to false. To enable draggin, set the property to true. --------------------------------------- unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; type 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); procedure FormCreate(Sender: TObject); private { Private declarations } FDownPoint: TPoint; FDragging: Boolean; FAllowDrag: Boolean; published { Public declarations } property AllowDrag: Boolean read FAllowDrag write FAllowDrag; end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin if Button = mbLeft then begin FDragging := True; GetCursorPos(FDownPoint); FDownPoint.X := FDownPoint.X - Self.Left; FDownPoint.Y := FDownPoint.Y - Self.Top; end; end; procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); var MovePoint: TPoint; begin if FDragging and FAllowDrag then begin GetCursorPos(MovePoint); Self.SetBounds( MovePoint.X - FDownPoint.X, MovePoint.Y - FDownPoint.Y, Self.Width, Self.Height ); end; end; procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin if Button = mbLeft then FDragging := False; end; procedure TForm1.FormCreate(Sender: TObject); begin FAllowDrag := True; end; end.