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.
Asked
Active
Viewed 8,341 times
10

Dan Is Fiddling By Firelight
- 5,981
- 17
- 77
- 128
-
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 Answers
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
-
@Dan: Thanks - I fixed the errors - (I was typing on the fly, and copying from pinvoke.net, which had the ref/out issue wrong...) – Reed Copsey Dec 10 '10 at 22:11
-
1
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
-
2That doesn't answer his question - he's asking whether to go to Normal or Maximized if the form is currently minimized... – Reed Copsey Dec 10 '10 at 16:05
-
-
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
-
1There's a better option than storing this yourself. It's already tracked by the Windows API. – Reed Copsey Dec 10 '10 at 16:06