views:

204

answers:

1

When I click minimize button in my Windows Forms application, I don't want it to perform the classic Windows minimize animation (window going down to taskbar).

As far as I know, there's no Minimize event, I can just use Resize, but I have no clue how to detect if I clicked minimize button. I tried to use if ( WindowState = FormWindowState.Minimized ) { ... }, but that does the animation anyway and triggers the code after.

Is there any way to detect minimize button click? Is there any way to disable animations or is that triggered by Windows settings?

+3  A: 

This works, but it has an unpleasant side-effect on the taskbar button. I can't think of another way, animation isn't even accessible from SystemParametersInfo().

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
    }
    protected override void WndProc(ref Message m) {
        // Catch WM_SYSCOMMAND, SC_MINIMIZE
        if (m.Msg == 0x112 && m.WParam.ToInt32() == 0xf020) {
            this.Hide();
            this.WindowState = FormWindowState.Minimized;
            this.BeginInvoke(new Action(() => this.Show()));
            return;
        }
        base.WndProc(ref m);
    }
}
Hans Passant
This is actually exactly what I'm looking for. I don't even mind the side effect as I'm minimizing my application to tray. Thanks!
Ondrej Slinták
If this is for a tray icon window, the better approach is to hide the minimize button. It just confuses the user. Let the user close the form, call Hide() in the FormClosing event handler.
Hans Passant