How do you intercept the Minimize button?

I want to send my application to the tray icon bar.




Answer this question

How do you intercept the Minimize button?

  • mepurathu

    Actually there might be a simpler way... when the window gets resized you check its WindowState to see if it was minimized.

    For instance:

    private void Form2_Resize (object sender, EventArgs e) {
    if (this.WindowState == FormWindowState.Minimized) {
    MessageBox.Show (
    "Minimized!");
    }
    }

    Regards
    --mc


  • Snapdragon

    Mario's code works fine.

    "Except that no size events are sent when a form is Maximized or Minimized."

    Not true.



  • JohanBarnard

    Mario Cossi wrote:
    Actually there might be a simpler way... when the window gets resized you check its
    Except that no size events are sent when a form is Maximized or Minimized.

  • SeonBong

    Is not what i was looking for... but thank you.

    I was thinking a way to Override the EventHandler (Minimize, Maximize, Close buttons) of my Form

    I hope someone can helpme here.



  • JimLovetoH20Ski

    int WM_SYSCOMMAND = 274;

    int SC_MINIMIZE = 0xF020;

    ///minimize form event code

    /// <summary>

    /// To set the last window style and size in case they minimize then restore later

    /// </summary>

    /// <param name="m"></param>

    protected override void WndProc(ref Message m)

    {

    if (m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_MINIMIZE)

    {

    _eWindowState = this.WindowState;

    _oSize = this.Size;

    }

    base.WndProc(ref m);

    }


  • Moiss

    hi

    I think this snippet will do the trick

  • AvalonBliss

    For whatever reason, the Form class does not have a built-in way to get this information. You have to override WndProc() and process the WM_SIZE message. Here is what I do:

    internal sealed partial class MainForm : Form
    {
    private const int WM_SIZE = 5;
    private const int SIZE_RESTORED = 0;
    private const int SIZE_MINIMIZED = 1;
    private const int SIZE_MAXIMIZED = 2;
    private const int SIZE_MAXSHOW = 3;
    private const int SIZE_MAXHIDE = 4;

    public event EventHandler Minimized;
    public event EventHandler Maximized;
    public event EventHandler Restored;

    private void OnMinimized(EventArgs e)
    {
    if (Minimized != null)
    {
    Minimized(this, e);
    }
    }

    private void OnMaximized(EventArgs e)
    {
    if (Maximized != null)
    {
    Maximized(this, e);
    }
    }

    private void OnRestored(EventArgs e)
    {
    if (Restored != null)
    {
    Restored(this, e);
    }
    }

    protected override void WndProc(ref Message m)
    {
    switch (m.Msg)
    {
    case WM_SIZE:
    switch(m.WParam.ToInt32())
    {
    case SIZE_RESTORED:
    OnRestored(EventArgs.Empty);
    break;
    case SIZE_MINIMIZED:
    OnMinimized(EventArgs.Empty);
    break;
    case SIZE_MAXIMIZED:
    OnMaximized(EventArgs.Empty);
    break;
    }
    break;
    default:
    break;
    }
    base.WndProc(ref m);
    }

    public MainForm()
    {
    this.Minimized += new EventHandler(MainForm_Minimized);
    InitializeComponent();
    }

    void MainForm_Minimized(object sender, EventArgs e)
    {
    // TODO: do something here
    }

    // ...
    }



  • claybin

    In the SizeChanged event handler, check to see if the WindowState property on the form is set to Minimized.

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

    Hope that helps.

    Bruce Johnson [C# MVP]
    http://www.objectsharp.com/blogs/bruce




  • hoangha

    cablehead wrote:

    Mario's code works fine.

    "Except that no size events are sent when a form is Maximized or Minimized."

    Not true.

    Correct, I was thinking of something else.



  • Sunny Jung

    Thank you all! that was just what i was looking for.

  • How do you intercept the Minimize button?