Does anyone know how to raise an event everytime a form is dragged and dropped?
Printable View
Does anyone know how to raise an event everytime a form is dragged and dropped?
You can subclass it.
Code for a Module
Code for a FormCode:Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Declare Function SetWindowLong& Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long)
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Const GWL_WNDPROC = (-4)
Private Const WM_EXITSIZEMOVE = &H232
Private Const WM_MOVING = &H216
Private Const HTCAPTION = 2
Private Const WM_NCLBUTTONDOWN = &HA1
Public WndProcOld As Long
Public Function WindProc(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If wMsg = WM_NCLBUTTONDOWN And wParam = HTCAPTION Then
Form1.Cls
Form1.Print "The form is beginning to move"
End If
If wMsg = WM_EXITSIZEMOVE Then
Form1.Cls
Form1.Print "The form has stopped moving"
End If
If wMsg = WM_MOVING Then
Form1.Cls
Form1.Print "The form is moving"
End If
WindProc = CallWindowProc(WndProcOld&, hwnd&, wMsg&, wParam&, lParam&)
End Function
Sub SubClassWnd(hwnd As Long)
WndProcOld& = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindProc)
End Sub
Sub UnSubclassWnd(hwnd As Long)
SetWindowLong hwnd, GWL_WNDPROC, WndProcOld&
WndProcOld& = 0
End Sub
Code:Private Sub Form_Load()
SubClassWnd hwnd
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnSubclassWnd hwnd
End Sub
Cheers Megatron,
I gave this a go but the Sub UnSubclassWnd(hwnd As Long)function causes VB to crash. I`ve done it another way now using a image as the pickup (drag) point instead of the title bar. (then used mouse_move and mouseup)