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.
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.
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).
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! } } }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With