1

I want to override the minimize control to instead of sending the window to the taskbar it would do what ever I write it to do.

Basicly this is what I wanted my new minimized and restored effects to be:

    private void ChangeForm(object sender, EventArgs e)
    {
        if (this.WindowState == FormWindowState.Minimized)
        {
            this.Height = 80;
            iDebug.Visible = false;
            mainMenu.Visible = false;
        }
        else
        {
            this.Height = 359;
            iDebug.Visible = true;
            mainMenu.Visible = true;
        }
    }

I have tried to fire an Event on the Resize to do this but without success

this.Resize += new EventHandler(ChangeForm);
Prix
  • 19,417
  • 15
  • 73
  • 132

2 Answers2

5

Cancel A WinForm Minimize?


Just tested this and it will make the form 100 pixels shorter when minimize is clicked without flicker.

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

protected override void WndProc(ref Message m) {
    if (m.Msg == WM_SYSCOMMAND) {
        if (m.WParam.ToInt32() == SC_MINIMIZE) {
            m.Result = IntPtr.Zero;
            Height -= 100;
            return;
        }
    }
    base.WndProc(ref m);
}
Community
  • 1
  • 1
J.D.
  • 2,114
  • 17
  • 13
  • Thanks but that does not prevent the window from minimizing it will minimize and restore from what I undestood and tested. What I want is to have my own function for when the user clicks the minus button that should minimize the window. – Prix Dec 19 '10 at 21:42
  • Check the other answer that talks about overriding the WndProc to cancel the minimize. Then you can run your custom code after that. – J.D. Dec 19 '10 at 21:46
  • thanks overlooked that due to the correct anwser beign marked. – Prix Dec 19 '10 at 21:51
3

The Minimize command has a very well defined meaning to a user, it shouldn't be messed with. Winforms accordingly doesn't have an event for it. But not a real problem, you can detect any Windows message by overriding WndProc(). Like tihs:

    private void OnMinimize() {
        this.Close();   // Do your stuff
    }
    protected override void WndProc(ref Message m) {
        // Trap WM_SYSCOMMAND, SC_MINIMIZE
        if (m.Msg == 0x112 && m.WParam.ToInt32() == 0xf020) {
            OnMinimize();
            return;        // NOTE: delete if you still want the default behavior
        }
        base.WndProc(ref m);
    }
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536