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.
            Asked
            
        
        
            Active
            
        
            Viewed 1e+01k times
        
    4 Answers
133
            
            
        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
        
- 8,230
- 6
- 30
- 34
- 
                    2Note that this seems to be one of those places where [Exceptions can be swallowed](http://stackoverflow.com/q/4933958/119527). – Jonathon Reinhart Nov 07 '13 at 03:23
- 
                    1You also need this.Resize += new System.EventHandler(this.Form1_Resize); – Tom Jul 09 '19 at 08:24
84
            
            
        To get in before the form has been minimised 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)
                {
                    // Do your action
                }
                // If you don't want to do the default action then break
                break;
        }
        base.WndProc(ref m);
    }
To react after the form has been minimised 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 your action
    }
}
 
    
    
        ChrisF
        
- 134,786
- 31
- 255
- 325
- 
                    0xF030 is when maximizing from normal window state. 0xF120 is when maximizing from the windows bar – k4yaman Oct 19 '17 at 09:54
- 
                    Actually, this only catches the Minimize *Click* event! What you're looking for is WM_SIZE (0x0005) & SIZE_MINIMIZED (1) – Ori Nachum Dec 27 '17 at 08:51
17
            
            
        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
        
- 11,958
- 3
- 48
- 72
- 
                    2This combined with a private "lastState" flag is the easiest way to go about it. – Matthew Scharley Jun 27 '09 at 14:34
7
            
            
        For people who search for WPF windows minimizing event :
It's a bit different. For the callback use WindowState :
private void Form1_Resize(object sender, EventArgs e)
{
    if (WindowState == FormWindowState.Minimized)
    {
        // Do some stuff
    }
}
The event to use is StateChanged (instead Resize):
public Main()
{
    InitializeComponent();
    this.StateChanged += Form1_Resize;
}
 
    
    
        Stealth Rabbi
        
- 10,156
- 22
- 100
- 176
 
    
    
        RaphLeFlamboyant
        
- 87
- 1
- 2
 
    