Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restore WindowState from Minimized

Tags:

winforms

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?

like image 977
tbetts42 Avatar asked Dec 09 '08 22:12

tbetts42


1 Answers

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.

like image 183
Mesmo Avatar answered Sep 22 '22 12:09

Mesmo