Tuesday, August 14, 2012

Drag & Drop III - Moving Borderless Forms

I know there are already lots of samples shown by experts on how to drag a borderless form.  But just in case you wanted to see another simple way, here it is.  If you will be simply allowing dragging via the form's surface itself, then you can simply do this as shown in my codes below.  If there are lots of objects where you need to bind the dragging, like a fake title bar, the caption on that fake title bar, etc.;  then you need to use BindEvent().

The reason why I am sharing this is because my earlier approach is to use MouseDown() first to perform validation whether left mouse button is clicked, then call another user defined method like MoveMe().  What I wanted to show here is a more direct usage of an object's MouseMove() event.  You drag (move) things via the left mouse button, so use MouseMove.

Here are the codes:

Local oForm
oForm=Newobject("Form1")
oForm.Show
Read Events
Return

Define Class Form1 As Form
      Height = 200
      Width = 500
      AutoCenter = .T.
      ShowTips = .T.
      TitleBar = 0

      Add Object label1 As Label With ;
            Caption = 'This is a demo of moving Bordeless form by directly tapping onto MouseMove Event.',;
            Top = 10, Left = 10, Width = 480, Height = 40, WordWrap = .T.

      Add Object command1 As CommandButton With ;
            top = 160, Left = 400, Caption = '\<Close', Height = 30

      Procedure Load
            * This is for dragging
            Declare Integer SendMessage In win32API ;
                  integer HWnd, Integer wMsg, ;
                  integer wParam, Integer Lparam
      Endproc

      Procedure MouseMove
            Lparameters nButton, nShift, nXCoord, nYCoord
            * Check first if form is set to move or not
            If m.nButton = 1 And Thisform.Movable = .T.
                  SendMessage(Thisform.HWnd, 0x202, 0x0, 0x0)  && MouseUp
                  SendMessage(Thisform.HWnd, 0x112, 0xf012, 0x0)  && Move form
            Endif
      Endproc

      Procedure command1.Click
            Thisform.Release
      Endproc

      Procedure Destroy
            Clear Events
      Endproc

Enddefine



Cheers!