 |
MikeG Level: Sage
 Registered: 21-02-2003 Posts: 54
|
Link Forms together
How can I link two forms together. Inother words, drag one form, the 2nd one follows in its connected location. Also, where the 2nd form can still be torn off the first and moved freely?
|
|
21-02-2003 at 11:49 PM |
|
|
vbgen Level: Moderator
 Registered: 10-10-2002 Posts: 876
|
Re: Link Forms together
right now i can only give you a hint or tip...
play around with the x and y coordinates of the forms..
when form1's x moves, then set the form2 to move from its own x to the new x, specified by whatever amount you may have calculated as the position parameters..
____________________________
Been busy trying to take a second degree <--it's not working out...
|
|
24-02-2003 at 05:30 PM |
|
|
admin Level: Administrator

 Registered: 04-04-2002 Posts: 530
|
Re: Link Forms together
the problem is that VB6 does not handle the on_moving event...so let's do it with subclassing
Insert this code in a Module:
Option Explicit
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nindex As Long, ByVal dwnewlong As Long) As Long
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal msg As Long, ByVal wParam As Long, lParam As RECT) As Long
Private Declare Function DefWindowProc Lib "user32" Alias "DefWindowProcA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type WndProc
oldWndProc As Long
MainHWnd As Long
End Type
Const WM_ACTIVATE = &H6
Const WM_ACTIVATEAPP = &H1C
Const WM_CLOSE = &H10
Const WM_CREATE = &H1
Const WM_DESTROY = &H2
Const WM_ENABLE = &HA
Const WM_HOTKEY = &H312
Const WM_HSCROLL = &H114
Const WM_KEYDOWN = &H100
Const WM_KEYUP = &H101
Const WM_MOUSEMOVE = &H200
Const WM_MOUSEACTIVATE = &H21
Const WM_MOVE = &H3
Const WM_MOVING = 534
Const GWL_WNDPROC = (-4)
Dim data(0 To 100) As WndProc
Private Property Let OldhWndProc(hWnd As Long, NewValue As Long)
Dim i As Integer
For i = 0 To 100
If data(i).MainHWnd = hWnd Then
data(i).oldWndProc = NewValue
Exit Property
End If
Next
For i = 0 To 100
If data(i).MainHWnd = 0 Then
data(i).MainHWnd = hWnd
data(i).oldWndProc = NewValue
Exit Property
End If
Next
End Property
Private Property Get OldhWndProc(hWnd As Long) As Long
Dim i As Integer
For i = 0 To 100
If data(i).MainHWnd = hWnd Then
OldhWndProc = data(i).oldWndProc
Exit Property
End If
Next
End Property
Private Sub OnMove(hWnd As Long, x As RECT)
'enable onmoving event
Form2.Move (150 + x.Left) * Screen.TwipsPerPixelX, (200 + x.Top) * Screen.TwipsPerPixelY
End Sub
Private Function MyWindowProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, lParam As RECT) As Long
If uMsg = WM_MOVING Then
OnMove hWnd, lParam
End If
MyWindowProc = CallWindowProc(OldhWndProc(hWnd), hWnd, uMsg, wParam, lParam)
End Function
Public Sub Install_OnMove(frm As Form)
OldhWndProc(frm.hWnd) = SetWindowLong(frm.hWnd, GWL_WNDPROC, AddressOf MyWindowProc)
End Sub
Public Sub Remove_OnMove(frm As Form)
Dim i As Long
For i = 0 To 100
If data(i).MainHWnd = frm.hWnd Then
data(i).MainHWnd = 0
SetWindowLong frm.hWnd, GWL_WNDPROC, data(i).oldWndProc
Exit Sub
End If
Next
End Sub |
Then create two forms (Form1 and Form2)
and add this code inside Form1
Option Explicit
Private Sub Form_Load()
Form2.Show
Install_OnMove Form1
End Sub
Private Sub Form_Unload(Cancel As Integer)
Remove_OnMove Form1
End Sub |
Moving form1 will cause form2 to move according to the offset used in the On_Move event
____________________________
AndreaVB
|
|
25-02-2003 at 07:17 AM |
|
|
|
|
 |
 |