How to restore minimized form

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 Location or Size of your form if it isn't in the normal state:

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
  if (this.WindowState == FormWindowState.Normal) {
    Settings.Default.WindowLocation = Location;
    Settings.Default.WindowSize = Size;
  }
  Settings.Default.WindowState = WindowState;
  Settings.Default.Save();
}

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 :

private void RestoreWindow()
{
  this.Location = Settings.Default.WindowLocation;
  this.Size = Settings.Default.WindowSize;

  // check for size or location off-screen, etc.

  if ((FormWindowState)Settings.Default.WindowState == FormWindowState.Minimized)
    this.WindowState = FormWindowState.Normal;
  else
    this.WindowState = Settings.Default.WindowState;
}

If you need to restore the last normal position the window was in, then you can use the OnResizeEnd override to save the settings:

protected override void OnResizeEnd(EventArgs e) {
  if (this.WindowState == FormWindowState.Normal) {
    Properties.Settings.Default.Location = this.Location;
    Properties.Settings.Default.Size = this.Size;
  }
  base.OnResizeEnd(e);
}

Then your closing event is just:

protected override void OnFormClosing(FormClosingEventArgs e) {
  Properties.Settings.Default.WindowState = this.WindowState;
  Properties.Settings.Default.Save();
  base.OnFormClosing(e);
}

Store the form's size and location in local variables in the Form.Resize event handler and Form.Move event handler respectively, only if the Form.WindowState property is different than FormWindowState.Minimized. Then save the content of the size and location variables in the settings, in FormClosing event handler.

链接地址: http://www.djcxy.com/p/29194.html

上一篇: 最大化拥有的表单不能正确恢复

下一篇: 如何恢复最小化的表单