Recent

Author Topic: [SOLVED] How do I drag/move a frame-less form  (Read 297 times)

Aruna

  • Hero Member
  • *****
  • Posts: 665
[SOLVED] How do I drag/move a frame-less form
« on: June 13, 2025, 02:32:15 pm »
In the attached screenshot, the light yellow sticky note at the top, how can I make it draggable or movable so I can reposition it to a new location? I am using Linux Debian.
« Last Edit: June 14, 2025, 12:25:25 am by Aruna »

Warfley

  • Hero Member
  • *****
  • Posts: 1930
Re: How do I drag/move a frame-less form
« Reply #1 on: June 13, 2025, 04:00:05 pm »
Simple, when the mouse moves, just update the window position according to the cursor position:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  2.   Y: Integer);
  3. const
  4.   DragOffset: TPoint = (X:-1;Y:-1);
  5. var
  6.   AbsPos: TPoint;
  7. begin
  8.   if not (ssLeft in Shift) then
  9.     Exit;
  10.   if DragOffset.X<0 then
  11.   begin
  12.     DragOffset:=Point(X,Y);
  13.     Exit;
  14.   end;
  15.   AbsPos := ClientToScreen(Point(X, Y)) - DragOffset;
  16.   Left := AbsPos.X;
  17.   Top := AbsPos.Y;
  18. end;

If you want to have it a little less clunky, put the DragOffset as member into the TForm and set it in the MouseDown event instead of on the first drag.

Aruna

  • Hero Member
  • *****
  • Posts: 665
Re: How do I drag/move a frame-less form
« Reply #2 on: June 14, 2025, 12:15:53 am »
Simple, when the mouse moves, just update the window position according to the cursor position:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  2.   Y: Integer);
  3. const
  4.   DragOffset: TPoint = (X:-1;Y:-1);
  5. var
  6.   AbsPos: TPoint;
  7. begin
  8.   if not (ssLeft in Shift) then
  9.     Exit;
  10.   if DragOffset.X<0 then
  11.   begin
  12.     DragOffset:=Point(X,Y);
  13.     Exit;
  14.   end;
  15.   AbsPos := ClientToScreen(Point(X, Y)) - DragOffset;
  16.   Left := AbsPos.X;
  17.   Top := AbsPos.Y;
  18. end;

If you want to have it a little less clunky, put the DragOffset as member into the TForm and set it in the MouseDown event instead of on the first drag.
Thank you @Warfley this is perfect! I was about to post saying your code does not seem to work when it hit me I have the Memo aligned to alClient, once I fixed that it works like a charm. Thanks !

 

TinyPortal © 2005-2018