tags:

views:

1119

answers:

3

Is there an easy method to restore a minimized form to its previous state, either Normal or Maximized? I'm expecting the same functionality as clicking the taskbar (or right-clicking and choosing restore).

So far, I have this, but if the form was previously maximized, it still comes back as a normal window.

if (docView.WindowState == FormWindowState.Minimized)
    docView.WindowState = FormWindowState.Normal;

Do I have to handle the state change in the form to remember the previous state?

+2  A: 

You could simulate clicking on the taskbar button like this:

SendMessage(docView.Handle, WM_SYSCOMMAND, SC_RESTORE, 0);
jeffm
A: 

For me, the code above does NOT work.

But at last I found the working code. Here it is:

    CxImports.ManagedWindowPlacement placement = new CxImports.ManagedWindowPlacement();
    CxImports.GetWindowPlacement(Convert.ToUInt32(Handle.ToInt64()), placement);
    if (placement.flags == CxImports.WPF_RESTORETOMAXIMIZED)
      WindowState = FormWindowState.Maximized;
    else
      WindowState = FormWindowState.Normal;

I guess, you can find all the needed "imported" functions by simple googling.

A: 

I use the following extension method:

using System.Runtime.InteropServices;

namespace System.Windows.Forms
{
    public static class Extensions
    {
        [DllImport( "user32.dll" )]
        private static extern int ShowWindow( IntPtr hWnd, uint Msg );

        private const uint SW_RESTORE = 0x09;

        public static void Restore( this Form form )
        {
            ShowWindow( form.Handle, SW_RESTORE );
        }
    }
}

Then call form.Restore() in my code.

DMM