Hello!
how can I made a form tag along with the main form when the user choose to move the main form by dragging the mouse??
Regards!!
Printable View
Hello!
how can I made a form tag along with the main form when the user choose to move the main form by dragging the mouse??
Regards!!
How 'bout Form2.Left = Form1.Left and Form2.Top = Form1.Top (or an equivalent function thereof)?
bob
hi Bob!
think that 2 lines only specifies the location of the form upon loading!
what I intend to do is to load up the 2nd form by the side of the first form. When I click on the first form and starts to move it, the 2 forms will move together
Regards!
I always thought that there was Form_Move event. I guess I was wrong :confused:. Anyways here is how I would do it without Form_Move event: add a timer to form1, set interval to 1, then paste this code to timer's timer event:
This should do the trick. You may have to change the position of second form, but it is how I would do it.Code:Private Sub Timer1_Timer()
Form2.Left = Form1.Left + Form1.Width
Form2.Top = Form1.Top
End Sub
HTH
hi QWERTY!
Yar! thanks! think this is temporary OK over!
I'm juz wondering is there any other method in doing this??
Theres almost always another way to do it, so here's my 2 cents:
(0) Make sure you have Form1 and a module
(1) Subclass Form1
(2) Trap the WM_MOVE message and move forms accordingly.
(3) Don't forget to unsubclass Form1 before it gets unloaded :)
I'm going to omit most of the API declarations and constants here cuz they're easily found, so load up API Text Viewer to get the info:
F: CallWindowProc, SetWindowLong, SetProp, GetProp, RemoveProp
C: WM_MOVE, GWL_WNDPROC
Code:' in Form1's code goes:
Public Function WndProc(ByVal oldWndProc As Long, ByVal hWnd As Long, ByVal msg As Long, ByVal wp As Long, ByVal lp As Long) As Long
If (msg = WM_MOVE) Then
Form2.Top = Me.Bottom
End If
WndProc = CallWindowProc(oldWndProc, hWnd, msg, wp, lp)
End Function
' in the module goes:
Declare Function CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As Any, src As Any, ByVal cbData As Long) As Long
Public Sub SubclassForm1(ByRef frm As Form1)
SetProp frm.hWnd, "SCObjPtr", PtrFromObject(frm.hWnd)
SetProp frm.hWnd, "SCWndProc", SetWindowLong(frm.hWnd, GWL_WNDPROC, AddressOf Form1WndProc)
End Sub
Public Sub UnsubclassForm1(ByRef frm As Form1)
SetWindowLong frm.hWnd, GWL_WNDPROC, GetProp(frm.hWnd, "SCWndProc")
RemoveProp frm.hWnd, "SCWndProc"
RemoveProp frm.hWnd, "SCObjPtr"
End Sub
Private Function Form1WndProc(ByVal hWnd As Long, ByVal msg As Long, ByVal wp As Long, ByVal lp As Long) As Long
Dim frm As Form1, oldWndProc As Long
On Error Resume Next
Set frm = ObjectFromPtr(GetProp(hWnd, "SCObjPtr"))
oldWndProc = GetProp(hWnd, "SCWndProc")
Form1WndProc = frm.WndProc(oldWndProc, hWnd, msg, wp, lp)
End Function
Public Function ObjectFromPtr(ByVal lPtr As Long) As Object
Dim oThis As Object
CopyMemory oThis, lPtr, 4 ' turn the pointer into an illegal, uncounted reference!
Set ObjectFromPtr = oThis ' now, using the same method as before, turn the pointer back into a NULL pointer
CopyMemory oThis, 0&, 4 ' now we're safe
End Function
Public Function PtrFromObject(ByRef oThis As Object) As Long
PtrFromObject = ObjPtr(oThis)
End Function
' VERY IMPORTANT: Do *NOT* forget the 'as long' at the end of Form1WndProc's function line
' ALSO VERY IMPORTANT: If you rename 'Form1' (changing the NAME, not the caption), you have to rename both references to Form1 in this module.