Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Programmatically Unminimize form [duplicate]

Tags:

c#

winforms

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.

like image 447
Dan Is Fiddling By Firelight Avatar asked Dec 10 '10 16:12

Dan Is Fiddling By Firelight


2 Answers

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;
}
like image 61
Reed Copsey Avatar answered Sep 20 '22 00:09

Reed Copsey


 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);
like image 21
Chuck Callebs Avatar answered Sep 20 '22 00:09

Chuck Callebs