Restore WindowState from Minimized

The easiest way to restore a form to normal state is:

if (MyForm.WindowState == FormWindowState.Minimized)
{
    MyForm.WindowState = FormWindowState.Normal;
}

You could simulate clicking on the taskbar button like this:

SendMessage(docView.Handle, WM_SYSCOMMAND, SC_RESTORE, 0);

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 )
        {
            if (form.WindowState == FormWindowState.Minimized)
            {
                ShowWindow(form.Handle, SW_RESTORE);
            }
        }
    }
}

Then call form.Restore() in my code.

Tags:

Winforms