This is such a mundane issue I thought I could fix it immediately, but no.
My form size and location are saved in app settings on exit for restore next time the app is run. If the user closes the form while it is minimized I am having trouble restoring to normal state. The form restores as minimized, and clicking on the taskbar button does nothing. I save the location and size in FormClosing event, but if the form is minimized I am saving the minimized size (160, 40) and location (-32000, -32000), which is totally incorrect for restoring the form. I want to force the form to never restore minimized, but to it’s last normal size and location. Somehow I must capture the size and location before the form is minimized and save that, and then on FormClosing do not save size and location if the form is minimized. This is all probably not 100% clear, but I hope someone has some insight on this.
FormClosing handler:
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
Settings.Default.WindowLocation = Location;
Settings.Default.WindowSize = Size;
Settings.Default.WindowState = WindowState;
Settings.Default.Save();
}
Restoring code:
private void RestoreWindow()
{
Location = Settings.Default.WindowLocation;
if(Location.X == 0 && Location.Y == 0)
StartPosition = FormStartPosition.CenterScreen;
Size = Settings.Default.WindowSize;
WindowState = FormWindowState.Normal;
if(Size.Width > Screen.PrimaryScreen.WorkingArea.Width)
{
Location = new Point(0, Location.Y);
Size = new Size(Screen.PrimaryScreen.WorkingArea.Width, Size.Height);
}
if(Size.Height > Screen.PrimaryScreen.WorkingArea.Height)
{
Location = new Point(Location.X, 0);
Size = new Size(Size.Width, Screen.PrimaryScreen.WorkingArea.Height);
}
}
You shouldn’t save the
LocationorSizeof your form if it isn’t in the normal state:Your Restore window routine doesn’t make complete sense. Why save the location if you are centering the form? Starting a program in Minimized mode is probably undesirable, in which case, I would default it to
Normal:If you need to restore the last normal position the window was in, then you can use the
OnResizeEndoverride to save the settings:Then your closing event is just: