Mega Code Archive

 
Categories / Delphi / Forms
 

How to drag forms with no border

Title: How to drag forms with no border? Question: I have made a form with no borders, and it's not movable, how can I drag it across the screen like a normal form? Answer: There are two ways to make a borderless form drag across the screen: 1. The form will be moved in a blank square like created when dragging normal windows with borders. 2. The form will be moved as it is shown when it's not moved. The first way is easier, all you have to do is to post this code onMouseDown event of the form you want to drag. procedure TForm1.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin If button = mbleft then begin releasecapture; TWincontrol (Parent).perform (WM_syscommand, $F012, 0); end; The second way is a little harder, but I think it's a better way. The first thing you need to do is declare a TPoint variable in the implementation area, so all the procedures can use it, and then perform this code: implementation var MPos:TPoint; {Position of the Form before drag} . . . procedure TForm1.FormMouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin MPos.X := X; MPos.Y := Y; end; procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); begin if ssLeft in Shift then begin Form1.Left := Form1.Left - (MPos.X-X); Form1.Top := Form1.Top - (MPos.Y-Y); end; end;