How can I set something to be changed whenever the form is moved?
thanks in advance
-Rog
Printable View
How can I set something to be changed whenever the form is moved?
thanks in advance
-Rog
Put a timer on the form, set its Interval property to something small, and include this code in your form:
Code:Option Explicit
Private mlngStartLeft As Long
Private mlngStartTop As Long
Private Sub Form_Load()
mlngStartLeft = Me.Left
mlngStartTop = Me.Top
End Sub
Private Sub Timer1_Timer()
If Me.Left <> mlngStartLeft Or _
Me.Top <> mlngStartTop Then
' Put code to set whatever you want here
mlngStartLeft = Me.Left
mlngStartTop = Me.Top
End If
End Sub
This code, from Megatron, may be better for going about to detect when yoru form is being moved.
Code:'Module code:
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)
Const WM_MOVE = &H3
Const WM_EXITSIZEMOVE = &H232
Global 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_EXITSIZEMOVE Then
'The Form has finished moving
MsgBox ("Form1 has been moved")
End If
If wMsg = WM_MOVE Then
'The Form is moving
Form1.Print "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
'Form code:
Private Sub Form_Load()
SubClassWnd Me.hwnd
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnSubclassWnd Me.hwnd
End Sub