1

I'd like to detect if my application is minimized under certain situations, and if it is, the window needs to be restored. I can do that easily as follows:

if(this.WindowState == FormWindowState.Minimized) {
    this.WindowState = FormWindowState.Normal;
}

However, what happens if the user first maximizes the form, then minimizes it? I don't know whether to set the WindowState to FormWindowState.Normal or FormWindowState.Maximized. Is there a method or API call I can check to solve this problem?

We Are All Monica
  • 13,000
  • 8
  • 46
  • 72

6 Answers6

2

The code shown below does what you need. Overriding the user's choice is pretty unwise btw.

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        mLastState = this.WindowState;
    }
    FormWindowState mLastState;
    protected override void OnResize(EventArgs e) {
        base.OnResize(e);
        if (mLastState != this.WindowState) {
            if (this.WindowState == FormWindowState.Minimized) this.WindowState = mLastState;
            else mLastState = this.WindowState;
        }
    }
}
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • I think you misinterpreted my question (or I didn't provide enough information). I'm not overriding the user's choice. This is a single-instance application with file associations. When the user opens a file, I want the existing instance to come to the front. Your code still solves my problem, though. – We Are All Monica Jan 11 '10 at 18:46
2

I use this solution to restore forms in MDI form. First you have to define:

[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);

private const int SW_RESTORE = 9;

and when it comes to restore:

ShowWindowAsync(this.MdiChildren[i].Handle, this.SW_RESTORE);

This will restore form to the previous state without using additional state holders. Also you may find this article interesting

Maciej
  • 31
  • 3
1

I think you should be able to call this.Show() and it will restore to the previous (visible) state.

Michael Bray
  • 14,998
  • 7
  • 42
  • 68
0

https://stackoverflow.com/a/6837421/578731:

Not sure this will work for everybody, but I ran into this today and someone on the team suggested "have you tried Normal"?

Turns out he was right. The following seems to nicely restore your window:

if (myWindow.WindowState == WindowState.Minimized)
    myWindow.WindowState = WindowState.Normal;

That works just fine, restoring the window to Maximized if needed. It seems critical to check for the minimized state first as calling WindowState.Normal a second time will "restore" your window to its non-maximized state.

Hope this helps.

Community
  • 1
  • 1
FooBarTheLittle
  • 459
  • 4
  • 7
0

Here's an approach that utilizes the OnResize method of the form

curtisk
  • 19,950
  • 4
  • 55
  • 71
0

I think this is the best option (Microsoft said):

this.WindowState = System.Windows.Forms.FormWindowState.Normal;
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Samuel
  • 11
  • 1