?
Solved

Need event to fire upon form relocation  vb6

Posted on 2004-11-13
3
Medium Priority
?
325 Views
Last Modified: 2010-05-02
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.
0
Comment
Question by:Mike Eghtebas
3 Comments
 
LVL 27

Assisted Solution

by:Dabas
Dabas earned 400 total points
ID: 12575357
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
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 12575425
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
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1600 total points
ID: 12575473
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month13 days, 20 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question