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.

  • 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. – Dan Is Fiddling By Firelight Aug 10 '21 at 21:12

4 Answers4

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;
}
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
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);
Community
  • 1
  • 1
Chuck Callebs
  • 16,293
  • 8
  • 56
  • 71
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;
}
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
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.

Daniel DiPaolo
  • 55,313
  • 14
  • 116
  • 115