This is useful for saving window position and size when an application closes and then restoring these settings when the application starts again.
1. In app.config, create three new settings:
- IsMaximized (Boolean)
- MainFormPosition (System.Drawing.Point)
- MainFormSize (System.Drawing.Size)
2. In the form's Load event handler, add the following code:
If My.Settings.MainFormPosition <> Nothing _
AndAlso My.Computer.Screen.Bounds.Contains(My.Settings.MainFormPosition) Then
Me.StartPosition = FormStartPosition.Manual
Me.Location = My.Settings.MainFormPosition
Else
Me.StartPosition = FormStartPosition.CenterScreen
End If
If My.Settings.IsMaximized <> Nothing _
AndAlso My.Settings.IsMaximized Then
Me.WindowState = FormWindowState.Maximized
Else
Me.WindowState = FormWindowState.Normal
End If
If My.Settings.MainFormSize <> Nothing Then
Me.Size = New Size(Math.Max(100, My.Settings.MainFormSize.Width), Math.Max(100, My.Settings.MainFormSize.Height))
End If
3. In the form's FormClosing event handler, add the following code:
If Me.WindowState = FormWindowState.Maximized Then
My.Settings.IsMaximized = True
My.Settings.MainFormPosition = New Point(Me.RestoreBounds.Left, Me.RestoreBounds.Top)
My.Settings.MainFormSize = New Size(Me.RestoreBounds.Width, Me.RestoreBounds.Height)
Else
My.Settings.IsMaximized = False
My.Settings.MainFormPosition = Me.Location
My.Settings.MainFormSize = Me.Size
End If
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment