How to restore minimized form

This is such a worldly problem, I thought I could fix it right away, but no. The size and location of the form are saved in the application settings when exiting for recovery the next time the application starts. If the user closes the form while it is minimized, I am having problems restoring to normal. The form is restored as minimized, and clicking on the taskbar button does nothing. I save the location and size in the FormClosing event, but if the form is minimized, I save the minimized size (160, 40) and location (-32000, -32000), which is completely wrong for restoring the form. I want the form to never be restored with the minimum size, but for it the last normal size and location. Somehow I have to fix the size and location before the form is minimized and save it, and then in FormClosing do not save the size and location if the form is minimized. All of this is probably not 100% clear, but I hope someone has an understanding.

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

Code recovery:

  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); } } 
+4
source share
2 answers

You should not save the Location or Size your form if it is not in a 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 recovery window procedure does not make full sense. Why keep your location if you center the form? Running the program in minimized mode is probably undesirable, in which case I would use Normal by default:

 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, 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:

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

Save the size and location of the form in local variables in the Form.Resize event handler and Form.Move event handler, respectively, only if the Form.WindowState property is different from FormWindowState.Minimized. Then save the contents of the size and location variables in the settings in the FormClosing event handler.

+1
source

Source: https://habr.com/ru/post/1413554/


All Articles