[Lazarus] Window body dragging

Michael Joyner ᏩᏯ mjoyner at vbservices.net
Sat Jan 2 18:45:29 CET 2010

Felipe Monteiro de Carvalho wrote:
> Hello,
> I am investigating how to make a component act like the title bar, in
> the sense that it can be used to drag the window around.
> We have 2 solutions at the moment:
> 1 - Use MouseMove, MouseUp, MouseDown to emulate this, but in my Mac
> it sometimes looses track of MouseMove because the mouse was faster
> then the window, so not 100% good
> 2 - Use platform-specific APIs. It's easy in Windows and we already
> have an idea in X11, but nothing yet in Carbon.
> Has anyone tryed this before and knows if option 1 is good enough or
> how to implement 2 well?
> thanks,
This may be of help:


Create a new project.
Create a blank form.
Change border type to none.
For this example, I will leave the default name of 'Form1'. //for this 
Save all your files.
Goto the global var section for Form1.

  MouseX, MouseY: Integer;

Using the object inspector on Form1, create an event handler for 
The purpose of this event handler is to record the mouse position on the 
form for use later.

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
     if [ssLeft] <= Shift then begin //only interested in left mouse button...
                                  //using <= as we are dealing with SETS.
        MouseX := X; //used later as an offset for the move
        MouseY := Y; //used later as an offset for the move

Now for the actual movement.
We will respond the OnMouseMove event, as the setbounds sets the upper 
left corner of the mainform, we have to subtract out the form relative 
position of the mouse pointer from the new screen position so that the 
top left corner stays relative to the mouse during the move.

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
    var P : TPoint;
     if [ssLeft] <= Shift then begin //only left mouse button again.
                                  //using <= as we are dealing with SETS.
  P := ClientToScreen(Point(X, Y)); //convert to screen relative numbers instead of form relative numbers.
SetBounds(P.X-MouseX, P.Y-MouseY, Width, Height); //Move form to new location.
//Note that we subtract the mouse's relative position in regards to the upper left of the form. 

LyX: http://www.lyx.org/ OpenOffice: http://www.openoffice.org/
Inkscape: http://www.inkscape.org/ Scribus: http://www.scribus.net/
GIMP: http://www.gimp.org/ PDF: http://www.pdfforge.org/

More information about the Lazarus mailing list