Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB6

Very simple way to move a form holding down mouse button

5.00/5 (4 votes)
8 Mar 2011CPOL 18.3K  
Very simple way to move a form holding down mouse button
Just paste this into your form code window. No API call is needed.
Press F5 & hold down mouse key & move it. The Form will move with the mouse also.
VB
Dim pastx, pasty, presentx, presenty, bt As Long
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
pastx = X
pasty = Y
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
presentx = X
presenty = Y
bt = Button
moving
End Sub
Function moving()
xx = presentx - pastx
yy = presenty - pasty
If bt Then 'set your condition: left btn or right btn or mid btn
Me.Left = Me.Left + xx
Me.Top = Me.Top + yy
End If
End Function


Actually, I didn't know about the below API before I coded that. It was long time ago when I started my programming in VB6. You can also move form using these APIs.
But using the above code, the below APIs are no longer needed.

Thanks to all.
Aslam Iqbal

VB
Declare Function ReleaseCapture& Lib "user32" ()
 Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Const WM_NCLBUTTONDOWN = &HA1

Public Const HTCAPTION = 2

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)