views:

2539

answers:

3

How to? I know that I can get the current state by WindowState, but I want to know if there's any event that will fire up when the user tries to minimize the form.

+7  A: 

To get in before the form has been minimized you'll have to hook into the WndProc procedure:

    private const int WM_SYSCOMMAND = 0x0112;
    private const int SC_MINIMIZE = 0xF020; 

    [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
    protected override void WndProc(ref Message m)
    {
        switch(m.Msg)
        {
            case WM_SYSCOMMAND:
                int command = m.WParam.ToInt32() & 0xfff0;
                if (command == SC_MINIMIZE && this.minimizeToTray)
                {
                    MinimizeToTray();  // For example
                }
                break;
        }
        base.WndProc(ref m);
    }

To react after the form has been mimized hook into the Resize event as the other answers point out (included here for completeness):

private void Form1_Resize ( object sender , EventArgs e )
{
    if ( WindowState == FormWindowState.Minimized )
    {
        // Do some stuff
    }
}

Though I'm sure this question has been asked before I can't find it at the moment

ChrisF
+8  A: 

I don't know of a specific event, but the Resize event fires when the form is minimized, you can check for FormWindowState.Minimized in that event

Dan F
This combined with a private "lastState" flag is the easiest way to go about it.
Matthew Scharley
+12  A: 

You can use the Resize event and check the Forms.WindowState Property in the event.

private void Form1_Resize ( object sender , EventArgs e )
{
    if ( WindowState == FormWindowState.Minimized )
    {
        // Do some stuff
    }
}
Steve Dignan