Save the values in the user.config file.
You'll need to create the value in the settings file - it should be in the Properties folder. Create five values:
Top
of type double
Left
of type double
Height
of type double
Width
of type double
- 'Maximized' of type 'bool' - to hold whether the window is maximized or not. If you want to store more information then a different type or structure will be needed.
Initialise the first two to 0 and the second two to the default size of your application, and the last one to false.
In the constructor:
this.Top = Properties.Settings.Default.Top;
this.Left = Properties.Settings.Default.Left;
this.Height = Properties.Settings.Default.Height;
this.Width = Properties.Settings.Default.Width;
// Very quick and dirty - but it does the job
if (Properties.Settings.Default.Maximised)
{
WindowState = WindowState.Maximized;
}
Create a Window_Closing event handler and add the following:
if (WindowState == WindowState.Maximized)
{
// Use the RestoreBounds as the current values will be 0, 0 and the size of the screen
Properties.Settings.Default.Top = RestoreBounds.Top;
Properties.Settings.Default.Left = RestoreBounds.Left;
Properties.Settings.Default.Height = RestoreBounds.Height;
Properties.Settings.Default.Width = RestoreBounds.Width;
Properties.Settings.Default.Maximised = true;
}
else
{
Properties.Settings.Default.Top = this.Top;
Properties.Settings.Default.Left = this.Left;
Properties.Settings.Default.Height = this.Height;
Properties.Settings.Default.Width = this.Width;
Properties.Settings.Default.Maximised = false;
}
Properties.Settings.Default.Save();