Click here to Skip to main content
15,032,177 members
Articles / Programming Languages / Visual Basic 6
Tip/Trick
Posted 8 Mar 2011

Tagged as

Stats

16K views
3 bookmarked

Very simple way to move a form holding down mouse button

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
8 Mar 2011CPOL
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)

Share

About the Author

Аslam Iqbal
Software Developer (Senior) Arisaf Tech
Bangladesh Bangladesh
No Biography provided

Comments and Discussions

 
GeneralReason for my vote of 5 this is very helpful, exactly what i... Pin
tarik0771-Dec-11 19:16
Membertarik0771-Dec-11 19:16 
GeneralRe: Thank you Tarik. Pin
Аslam Iqbal2-Dec-11 20:05
professionalАslam Iqbal2-Dec-11 20:05 
GeneralReason for my vote of 5 Cool, Goo to know this, thanks for s... Pin
Gandalf_TheWhite12-Apr-11 1:03
professionalGandalf_TheWhite12-Apr-11 1:03 
GeneralJust Curious!! Pin
Gandalf_TheWhite12-Apr-11 1:09
professionalGandalf_TheWhite12-Apr-11 1:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.