Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting the event when a form has been moved (deplaced) 1

Status
Not open for further replies.

leyann

IS-IT--Management
Sep 16, 2002
29
FR
Does anyone know how to retrieve the event corresponding to the form moving ?
I tried Form_DragDrop event but it seems not to work.
 
I’m not sure how to detect the movement of a form. . Was a bit curious though. Why do you need to know if the form was moved or is being moved?
 
I want two forms to move together when the user move one. I assume, If I knew when the main form is moved, I could move automatically the other form.
Do you have a better idea ?
 
Assuming that each form are from the same EXE. . .

One could monitor the coordinates of a form by using a timer in each from.

The code in the timer’s could read the other forms Top and Left properties and take appropriate action when the other forms Top / Left coordinates have changed.
 
RonVaught,
Thanks a lot for your suggestion, but I'm not sure this would be the best solution : the timer should verify the coordinates very often to be really efficient (I mean to obtain the second form to stick to the first one) and I'm afraid it takes to many ressources...
Why this kind of event couldn't be catched ?
 
You could use the MouseUp event in each form. Assuming the user used the mouse to move the form. . . When the mouse button is released set each forms coordinates accordingly. Would this work for you?
 
Good Idea !
... but I've ever tried, and using the mouse on the title bar to move the form does not make a MouseUp event.
MouseUp event is only throwed when the user click in the content of the form.
 
I see. . .
How about setting the border styles on the forms to (0-None). Makes the forms look a bit ugly but they can’t be manually moved by the user.
 
To do that, you have to create a hook to subclass your window with the WM_MOVE message...

Basically set the window handle of your form to your own customized message handler. Check for WM_MOVE (and move the 2nd form along with it) and pass pass the messages onto the original message handler.

Here's a link to a sample
 
Here's an example a little more relevant to want you wanted. It shows two forms and when either one is moved it moves the other one to stay "attached" to it.

Private 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

Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hWnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long

Private Const GWL_WNDPROC = -4
Private Const WM_MOVE = &H3

Private lpPrevWndProc1 As Long
Private lpPrevWndProc2 As Long
Private hWnd1 As Long
Private hWnd2 As Long

Function WindowProc(ByVal hw As Long, _
ByVal uMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long

If hw = hWnd1 Then
If uMsg = WM_MOVE Then Form2.Move Form1.Left + Form1.Width, Form1.Top
WindowProc = CallWindowProc(lpPrevWndProc1, hw, uMsg, wParam, lParam)
Else
If uMsg = WM_MOVE Then Form1.Move Form2.Left - Form2.Width, Form2.Top
WindowProc = CallWindowProc(lpPrevWndProc2, hw, uMsg, wParam, lParam)
End If
End Function

Private Sub main()
Load Form1
Load Form2

hWnd1 = Form1.hWnd
hWnd2 = Form2.hWnd

lpPrevWndProc1 = SetWindowLong(hWnd1, GWL_WNDPROC, AddressOf WindowProc)
lpPrevWndProc2 = SetWindowLong(hWnd2, GWL_WNDPROC, AddressOf WindowProc)

Form1.Show
Form2.Show
End Sub
 
Thank you very much (it works exactly like I wanted) !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top