tags:

views:

391

answers:

4

Is there an Event that is fired when you maximize a WinForms Form or "un-maximize" it again?

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.

+1  A: 

If there's no obvious event to listen for, you're probably going to need to hook into the Windows API and catch the appropriate message (Google turns up that you'll want to intercept the WM_SYSCOMMAND message: http://www.codeguru.com/forum/archive/index.php/t-234554.html).

Matt
+5  A: 

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
        }
    }

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

Reed Copsey
Little Additions: base.WndProc(ref m); should be called at the end. Works like a charm!
Michael Stum
Yeah - I didn't finish the method - showed just this part.
Reed Copsey
+1  A: 

Would testing the windowstate in the form's activate event help?

Beth
+1  A: 

Another little addition in order to check for the restore to the original dimension and position after the maximization:

protected override void WndProc(ref Message m)
{
    base.WndProc(ref m);

    // WM_SYSCOMMAND
    if (m.Msg == 0x0112)
    {
        if (m.WParam == new IntPtr(0xF030) // Maximize event - SC_MAXIMIZE from Winuser.h
            || m.WParam == new IntPtr(0xF120)) // Restore event - SC_RESTORE from Winuser.h
        {
            UpdateYourUI();
        }
    }
}

Hope this help.

Lorenzo Melato