Need event to fire upon form relocation vb6

I have a popup form.  Using mouse, I sometimes move this form around (not resize), How can I locate an event to fire when I move this form around.  I have tried drag and drop also mouse up with no luck so far.

The reason I am looking for such an event is to record left and top position of the form in a table.  I have timer as an option but would like to have a better solution if you can think of.

Thanks.
LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DabasCommented:
Hi eghtebas:
Looks like the timer is the way to go.
Unless you want to record left and top only so that next time you start the application, it places itself at the same position where it last exited.
In that case QueryUnload becomes an option

Dabas
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
I am using QueryUnload to record top and left position when it closes.  I need for ocasion when I move it around.  I have a second popup form that attaches itself to the first popup form.  No, when I move first one, I want the second popup form to follow the first form.

Therefore, I need an event to read top and left of moved form and also use this event to relocated the second form.

Mike
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
To make a form move with another in real time as if it were docked, requires you to subclass the main form and trap the WM_MOVING message so you can move the docked form right along with it.  Below is an example of Form2 being docked to the bottom left corner of Form1.  Form2 is opened when the button on Form1 is clicked.  The second button on Form1 is used to enable/disable docking of Form2 to Form1.

When dealing with subclassing, always close the application by closing the forms with the X in the top right.  Ending the application with the Stop button in the IDE will result in the IDE crashing and you losing your work.  Always save your work before running since any minor errors in the code usually result in an IDE crash when subclassing is involved.

~IM

' ----------------------------------------------------------------------
' Form1
' ----------------------------------------------------------------------
Option Explicit

Private Sub Form_Load()
    ' center the form
    Me.Left = (Screen.Width / 2) - (Me.Width / 2)
    Me.Top = (Screen.Height / 2) - (Me.Height / 2)
   
    ' subclass the window
    HookWindow Me.hWnd
End Sub

Private Sub Command1_Click()
    Form2.Move Form1.Left, Form1.Top + Form1.Height
    Form2.Show
End Sub

Private Sub Command2_Click()
    HookWindow Me.hWnd
    If lPrevProc <> 0 Then
        If Form2.WindowState = vbNormal And Form2.Visible Then
            Form2.Move Form1.Left, Form1.Top + Form1.Height
        End If
    End If
End Sub

Private Sub Form_Resize()
    If Me.WindowState = vbNormal And Me.Visible Then
        If Form2.WindowState = vbNormal And Form2.Visible Then
            Form2.Move Form1.Left, Form1.Top + Form1.Height
        End If
    End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
    If lPrevProc <> 0 Then
        HookWindow Me.hWnd
    End If
End Sub

' ----------------------------------------------------------------------
' Module1
' ----------------------------------------------------------------------
Option Explicit

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

Public Const GWL_WNDPROC As Long = (-4)
Public Const WM_MOVING = &H216
Public Const WM_SIZING = &H214

Public lPrevProc As Long

Public Sub HookWindow(ByVal lHandle As Long)
    If lPrevProc = 0 Then
        lPrevProc = SetWindowLong(lHandle, GWL_WNDPROC, AddressOf HookProc)
    Else
        Call SetWindowLong(lHandle, GWL_WNDPROC, lPrevProc)
        lPrevProc = 0
    End If
End Sub

Public Function HookProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Select Case uMsg
        Case WM_SIZING, WM_MOVING
            If Form2.WindowState = vbNormal And Form2.Visible Then
                Form2.Move Form1.Left, Form1.Top + Form1.Height
            End If
            HookProc = 1
            Exit Function
    End Select
   
    HookProc = CallWindowProc(lPrevProc, hWnd, uMsg, wParam, lParam)
End Function

' ----------------------------------------------------------------------
' Form2
' ----------------------------------------------------------------------
Option Explicit

Private Sub Form_Resize()
    If Me.WindowState = vbNormal And Me.Visible Then
        If Form1.WindowState = vbNormal And Form1.Visible Then
            Me.Move Form1.Left, Form1.Top + Form1.Height
        End If
    End If
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.