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);
}
}