Click here to Skip to main content
15,887,875 members
Articles / Programming Languages / Visual Basic
Alternative
Tip/Trick

Very simple way to restore form window to its previous state

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
3 Jun 2011CPOL 5.9K  
Here's another way. This saves the form's normal WindowState settings, so you can still save even if the form is minimized, and it can be used for any form. It also includes a sub to keep forms inside the desktop area.Check the Regpath string before using!...

Here's another way. This saves the form's normal WindowState settings, so you can still save even if the form is minimized, and it can be used for any form. It also includes a sub to keep forms inside the desktop area.


Check the Regpath string before using! ;)


'////////////////////////////////////////////////////
' Save & Restore form size and location.            /
' Check form size/edges, fit desktop working area.  /
'                                                   /
'             Edgemeal- June 03, 2011               /
'////////////////////////////////////////////////////

'---------------------------------
' example usage:
' --------------
'Sub Form_FormClosing(...
'    SavePosition(Me)
'End Sub
'
'Sub Form_Load(...
'    RestorePosition(Me)
'End Sub
'---------------------------------

Module ModFormLocSize
    Private Declare Function GetWindowPlacement Lib "user32" (ByVal hwnd As IntPtr, _
                                                              ByRef lpwndpl As WINDOWPLACEMENT) As Integer
    Private Regpath As String = "HKEY_CURRENT_USER\Software\" _
                                & Application.CompanyName _
                                & "\" & My.Application.Info.Title _
                                & "\"

    Private Structure POINTAPI
        Public x As Integer
        Public y As Integer
    End Structure

    Private Structure RECT
        Public Left As Integer
        Public Top As Integer
        Public Right As Integer
        Public Bottom As Integer
    End Structure

    Private Structure WINDOWPLACEMENT
        Public Length As Integer
        Public flags As Integer
        Public showCmd As Integer
        Public ptMinPosition As POINTAPI
        Public ptMaxPosition As POINTAPI
        Public rcNormalPosition As RECT
    End Structure

    Public Sub SavePosition(ByVal frm As Form)
        Dim wp As WINDOWPLACEMENT
        wp.Length = System.Runtime.InteropServices.Marshal.SizeOf(wp)
        GetWindowPlacement(frm.Handle, wp)
        With wp.rcNormalPosition
            My.Computer.Registry.SetValue(Regpath, frm.Name & "_Top", .Top)
            My.Computer.Registry.SetValue(Regpath, frm.Name & "_Left", .Left)
            My.Computer.Registry.SetValue(Regpath, frm.Name & "_Height", .Bottom - .Top)
            My.Computer.Registry.SetValue(Regpath, frm.Name & "_Width", .Right - .Left)
        End With
    End Sub

    Public Sub RestorePosition(ByVal frm As Form)
        ' get height value
        Dim H As Integer = CInt(My.Computer.Registry.GetValue(Regpath, frm.Name & "_Height", 0))
        ' if h=0 then assume settings were not yet saved, center the form, default size
        If H = 0 Then
            Dim screenW As Integer = Screen.PrimaryScreen.WorkingArea.Width
            Dim screenH As Integer = Screen.PrimaryScreen.WorkingArea.Height
            frm.Location = New Point(CInt((screenW - frm.Width) / 2), CInt((screenH - frm.Height) / 2))
        Else ' get remaining form settings
            Dim X As Integer = CInt(My.Computer.Registry.GetValue(Regpath, frm.Name & "_Left", 0))
            Dim Y As Integer = CInt(My.Computer.Registry.GetValue(Regpath, frm.Name & "_Top", 0))
            Dim W As Integer = CInt(My.Computer.Registry.GetValue(Regpath, frm.Name & "_Width", 0))
            ' set form location and size
            frm.Location = New Point(X, Y)
            frm.Size = New Size(W, H)
        End If
        ' check form size and location, keep inside desktop working area.
        CheckEdge(frm)
    End Sub

    Public Sub CheckEdge(ByVal Frm As Form)
        ' // Keep a form inside the desktop work area.//
        ' Edgemeal 02/12/2004 - Updated 06-03-2011 for VB10
        Dim Desktop As Rectangle = Screen.PrimaryScreen.WorkingArea
        Dim X As Integer = Frm.Left
        Dim Y As Integer = Frm.Top
        Dim W As Integer = Frm.Width
        Dim H As Integer = Frm.Height
        ' make sure form is not larger then desktop "working" area. (accounts for taskbar!)
        If H > (Desktop.Bottom - Desktop.Top) Then H = (Desktop.Bottom - Desktop.Top)
        If W > (Desktop.Right - Desktop.Left) Then W = (Desktop.Right - Desktop.Left)
        ' Check if edges are off screen.
        If Frm.Top < Desktop.Top Then Y = Desktop.Top
        If Frm.Left < Desktop.Left Then X = Desktop.Left
        If Y > Desktop.Bottom - H Then Y = Desktop.Bottom - H
        If X > Desktop.Right - W Then X = Desktop.Right - W
        ' reposition the window  'MoveWindow(Frm.Handle, X, Y, W, H, 1)
        Frm.Location = New Point(X, Y)
        Frm.Size = New Size(W, H)
    End Sub

End Module

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --