Keep two windows moving together

microdrainage
microdrainage used Ask the Experts™
on
Hi, I'm trying to write a flyout button toolbar control using a second form to contain the flyout buttons.  I want the to display the second form on top of the first form (using .show , ownerform).  This works fine until you move the first form.  Is there a way of dectecting when the first form moving in order to tell the second form to move as well (so they stay aligned)? Is there a better way of doing this - i.e could I lock the second form to the window   second form, or better still lock it to the edge of a control on the first form?

Thanks in advance

Ian
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
write the code in form1_mousemove() event to move form2
Commented:
Try this:

FORM:

Private Sub Form_Load()
    glPrevWndProc = fSubClass()
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Call pUnSubClass
End Sub

MODULE:

Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) 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

Public Const GWL_WNDPROC = (-4)
Public Const WM_EXITSIZEMOVE = &H232
Public Const WM_MOUSEMOVE = &H200
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_NCLBUTTONDOWN = &HA1

Public Const WM_QUIT = &H12
Public Const WM_SIZE = &H5

Public glPrevWndProc As Long

Public Function pMyWindowProc(ByVal hw As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Select Case uMsg
        Case WM_MOUSEMOVE
        Case WM_EXITSIZEMOVE
            Debug.Print Form1.Left, Form1.Top
        Case WM_SIZE
            Debug.Print "Resize"
        Case WM_LBUTTONDOWN
        Case WM_NCLBUTTONDOWN
    End Select
    pMyWindowProc = CallWindowProc(glPrevWndProc, hw, uMsg, wParam, lParam)
End Function

Public Sub pUnSubClass()
    Call SetWindowLong(Form1.hwnd, GWL_WNDPROC, glPrevWndProc)
End Sub

Public Function fSubClass() As Long
    fSubClass = SetWindowLong(Form1.hwnd, GWL_WNDPROC, AddressOf pMyWindowProc)
End Function

regards

v.k.
VK

Commented:
P.S.

If you want to move the child form when you move (not have moved) the parent form you have to respond to the WM_MOVE message istead of responding to the WM_EXITSIZEMOVE Message. Use the vb API Declaration-addin to get it's value.

good luck

v.k.

Author

Commented:
Thanks (I like the good luck at the end of your PS :-)  
This worked like a dream!

Cheers

Ian

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial