Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleRestore a form to the position it had when it was last run
Keywordsform, position, GetWindowPlacement, SetWindowPlacement
CategoriesTips and Tricks, API
 
In the Form_Unload event, save the form's placement information obtained using the GetWindowPlacement API function. In the Form_Load event handler, get the values and set them using the SetWindowPlacement API function.

Thanks to Rhett Livingston for pointing out an error a the previous version.

 
' Load the form's geometry from the registry.
Private Sub Form_Load()
Dim wp As WINDOWPLACEMENT
Dim window_state As Long

    ' Initialize wp from the registry.
    With wp
        .Length = Len(wp)
        .flags = GetSetting(APP_NAME, SECTION_NAME, _
            "flags", 0)
        .showCmd = GetSetting(APP_NAME, SECTION_NAME, _
            "showCmd", SW_RESTORE)
        .ptMinPosition.X = GetSetting(APP_NAME, _
            SECTION_NAME, "MinX", 100)
        .ptMinPosition.Y = GetSetting(APP_NAME, _
            SECTION_NAME, "MinY", 100)
        .rcNormalPosition.Left = GetSetting(APP_NAME, _
            SECTION_NAME, "Left", 0)
        .rcNormalPosition.Top = GetSetting(APP_NAME, _
            SECTION_NAME, "Top", 0)
        .rcNormalPosition.Right = GetSetting(APP_NAME, _
            SECTION_NAME, "Right", 100)
        .rcNormalPosition.Bottom = GetSetting(APP_NAME, _
            SECTION_NAME, "Bottom", 100)
    End With

    ' Position the form.
    SetWindowPlacement hwnd, wp
End Sub

' Save the form's geometry in the registry.
Private Sub Form_Unload(Cancel As Integer)
Dim disallow_minimized As Boolean
Dim wp As WINDOWPLACEMENT

    ' Do nothing if the form is not visible.
    If Visible = False Then Exit Sub

    ' Uncomment this line if you do not want to
    ' allow the form to come up minimized.
'    disallow_minimized = True

    ' Get the window's normalized placement.
    wp.Length = Len(wp)
    GetWindowPlacement hwnd, wp

    With wp
        If disallow_minimized And _
            ((.showCmd = SW_MINIMIZE) Or _
             (.showCmd = SW_SHOWMINIMIZED)) _
        Then
            SaveSetting APP_NAME, SECTION_NAME, _
                "showCmd", SW_NORMAL
        Else
            SaveSetting APP_NAME, SECTION_NAME, _
                "showCmd", .showCmd
        End If

        SaveSetting APP_NAME, SECTION_NAME, _
            "flags", .flags
        SaveSetting APP_NAME, SECTION_NAME, _
            "MinX", .ptMinPosition.X
        SaveSetting APP_NAME, SECTION_NAME, _
            "MinY", .ptMinPosition.Y
        SaveSetting APP_NAME, SECTION_NAME, _
            "Left", .rcNormalPosition.Left
        SaveSetting APP_NAME, SECTION_NAME, _
            "Top", .rcNormalPosition.Top
        SaveSetting APP_NAME, SECTION_NAME, _
            "Right", .rcNormalPosition.Right
        SaveSetting APP_NAME, SECTION_NAME, _
            "Bottom", .rcNormalPosition.Bottom
    End With
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated