10

How do I take a form that is currently minimized and restore it to its previous state. I can't find any way to determine if its previous WindowState was Normal or Maximized; but I know the information has to be stored somewhere because windows doesn't have a problem doing it with apps on the taskbar.

1
  • I'm not going to waste anyone else's time trying to get this re-opened; but the alleged duplicate doesn't answer my question because it's answer will restore to normal (open but non-maximized) always, including if pre-minimization it was in the maximized state. Aug 10, 2021 at 21:12

4 Answers 4

11

There is no managed API for this. The way to do it is to PInvoke GetWindowPlacement and check for WPF_RESTORETOMAXIMIZED.

For details, see this Microsoft How To (which is demonstrating the technique in VB).

In C#, this would be:

[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);


private struct WINDOWPLACEMENT
{
    public int length;
    public int flags;
    public int showCmd;
    public System.Drawing.Point ptMinPosition;
    public System.Drawing.Point ptMaxPosition;
    public System.Drawing.Rectangle rcNormalPosition;
}

public void RestoreFromMinimzied(Form form)
{
   const int WPF_RESTORETOMAXIMIZED = 0x2;
   WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
   placement.length = Marshal.SizeOf(placement);
   GetWindowPlacement(form.Handle, ref placement);

   if ((placement.flags & WPF_RESTORETOMAXIMIZED) == WPF_RESTORETOMAXIMIZED)
       form.WindowState = FormWindowState.Maximized;
   else
       form.WindowState = FormWindowState.Normal;
}
2
  • @Dan: Thanks - I fixed the errors - (I was typing on the fly, and copying from pinvoke.net, which had the ref/out issue wrong...) Dec 10, 2010 at 22:11
  • 1
    @Dan: Sounds like this worked for you, though, at least ;) Dec 10, 2010 at 22:11
10
 this.WindowState = FormWindowState.Normal;

You also have:

 this.WindowState = FormWindowState.Minimized;
 this.WindowState = FormWindowState.Maximized;

Ah, I misunderstood the question:

Restore WindowState from Minimized should be what you're looking for. It says you can mimic the taskbar behavior like this:

SendMessage(form.Handle, WM_SYSCOMMAND, SC_RESTORE, 0);
3
  • 2
    That doesn't answer his question - he's asking whether to go to Normal or Maximized if the form is currently minimized... Dec 10, 2010 at 16:05
  • I updated my answer. I misunderstood the question. Dec 10, 2010 at 16:07
  • This is a much cleaner solution - very simple to implement. Dec 23, 2010 at 7:03
3

You can track the window state changes through the Resize event. Like this:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        prevState = currState = this.WindowState;
    }
    protected override void OnResize(EventArgs e) {
        if (currState != this.WindowState) {
            prevState = currState;
            currState = this.WindowState;
        }
        base.OnResize(e);
    }
    private FormWindowState prevState, currState;
}
2

If you want to store the previous state whenever there's a change (maximize/minimize), you'll have to hook into the SizeChanged event, according to this post on MSDN. You can get the WindowState there and store it.

1
  • 1
    There's a better option than storing this yourself. It's already tracked by the Windows API. Dec 10, 2010 at 16:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.