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.
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;
}
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With