Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event when a window gets maximized/un-maximized

Tags:

c#

winforms

Is there an event that is fired when you maximize a Form or un-maximize it?

Before you say Resize or SizeChanged: Those get only fired if the Size actually changes. If your window happens to be equal in size to the maximized window, they do not fire. Location looks like the next best bet, but that again feels like gambling on a coincidence.

like image 346
Michael Stum Avatar asked Aug 18 '09 19:08

Michael Stum


People also ask

What happens when a window is maximized?

Maximize allows the user to enlarge a window, usually making it fill the entire screen or the program window where it is contained. When a window is maximized, it cannot be moved until it is reduced in size using the Restore button.

How do I open windows form in maximized?

In the Properties window, click the Shortcut tab (A). Locate the Run: section, and click the down arrow on the right side (red circle). In the drop-down menu that appears, select Maximized (B). Click Apply (C), and then click OK (D).


2 Answers

Suprising that no one mentioned the inbuilt .NET method.

This way you don't need to override the Window Message Processing handler.

It even captures maximize/restore events caused by double-clicking the window titlebar, which the WndProc method does not.

Copy this in and link it to the "Resize" event handler on the form.

    FormWindowState LastWindowState = FormWindowState.Minimized;     private void Form1_Resize(object sender, EventArgs e) {          // When window state changes         if (WindowState != LastWindowState) {             LastWindowState = WindowState;               if (WindowState == FormWindowState.Maximized) {                  // Maximized!             }             if (WindowState == FormWindowState.Normal) {                  // Restored!             }         }      } 
like image 104
Robin Rodricks Avatar answered Oct 03 '22 06:10

Robin Rodricks


You can do this by overriding WndProc:

protected override void WndProc( ref Message m ) {     if( m.Msg == 0x0112 ) // WM_SYSCOMMAND     {         // Check your window state here         if (m.WParam == new IntPtr( 0xF030 ) ) // Maximize event - SC_MAXIMIZE from Winuser.h         {               // THe window is being maximized         }     }     base.WndProc(ref m); } 

This should handle the event on any window. SC_RESTORE is 0xF120, and SC_MINIMIZE is 0XF020, if you need those constants, too.

like image 40
Reed Copsey Avatar answered Oct 03 '22 05:10

Reed Copsey