VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Store and restore window size and position in your applications with this simple procedure.

by Matt Terry (3 Submissions)
Category: Registry
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Fri 27th April 2001
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Store and restore window size and position in your applications with this simple procedure.

Rate Store and restore window size and position in your applications with this simple procedure.



'State properties to the registry and and apply them the next time your
'program is run.  Place this Sub in a code module in your project, and
'call it from anywhere in your program.

'1st argument, form to persist to or from registry
'2nd argument, True to save to registry, False to load from registry
'To use this Sub in your Forms, simply put
'RegFormDimensions Me, False in your Form.Load procedure, and
'RegFormDimensions Me, True in your Form.Unload procedure
Public Sub RegFormDimensions(ByVal frmF As Form, ByVal fVerifyAndSave As Boolean)
    Dim szRegEntry As String, szSetting As String
    Dim szKey As String
    
    Dim lWidth As Long, lHeight As Long, lTop As Long, lLeft As Long
    Dim lState As Long
    
    'registry entries will be made in
    'HKEY_CURRENT_USER\Software\VB and VBA Program Settings    'section of the registry, under the application's title
    
    With frmF
        szRegEntry = App.Title
        szSetting = "Settings"
        szKey = .Name
        
        If fVerifyAndSave Then  'Store to registry
            'Don't save to registry if window is minimized
            'This prevents the window from being loaded in a minimized state
            If .WindowState <> vbMinimized Then
                SaveSetting szRegEntry, szSetting, szKey & "W", .Width
                SaveSetting szRegEntry, szSetting, szKey & "H", .Height
                SaveSetting szRegEntry, szSetting, szKey & "X", .Left
                SaveSetting szRegEntry, szSetting, szKey & "Y", .Top
                SaveSetting szRegEntry, szSetting, szKey & "State", .WindowState
            End If
        Else                    'load from registry
            lWidth = CLng(GetSetting(szRegEntry, szSetting, szKey & "W", 0))
            lHeight = CLng(GetSetting(szRegEntry, szSetting, szKey & "H", 0))
            lLeft = CLng(GetSetting(szRegEntry, szSetting, szKey & "X", 0))
            lTop = CLng(GetSetting(szRegEntry, szSetting, szKey & "Y", 0))
            lState = CLng(GetSetting(szRegEntry, szSetting, szKey & "State", 0))
            
            'If all values are 0, no entries were found in the registry.
            'In this case, the form will simply retain the settings that
            'it already had
            If Not (lWidth = 0 And lHeight = 0 And lLeft = 0 And lTop = 0) Then
                .Move lLeft, lTop, lWidth, lHeight
            End If
            
            'Apply window state, unless window was minimized when closed
            If lState <> vbMinimized Then
                .WindowState = lState
            Else
                .Width = vbNormal
            End If
        End If
    End With
End Sub



Download this snippet    Add to My Saved Code

Store and restore window size and position in your applications with this simple procedure. Comments

No comments have been posted about Store and restore window size and position in your applications with this simple procedure.. Why not be the first to post a comment about Store and restore window size and position in your applications with this simple procedure..

Post your comment

Subject:
Message:
0/1000 characters