Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the minimize box click of a WPF window

How to get the minimize box click event of a WPF window?

like image 998
Sauron Avatar asked Jun 02 '09 11:06

Sauron


1 Answers

There's an event called StateChanged which (from the help) looks like it might do what you want.

Occurs when the window's WindowState property changes.

The help says it's only supported in .NET 3.0 and 3.5 under Vista, but I've just tried it on XP and it fires when the window is minimized, maximized and restored. However, from my testing, it fires after the state has changed, so if you want to do something before the window minimized this might not be the approach you need.

You'll have to check the actual state to make sure it's correct.

    private void Window_StateChanged(object sender, EventArgs e)
    {
        switch (this.WindowState)
        {
            case WindowState.Maximized:
                MessageBox.Show("Maximized");
                break;
            case WindowState.Minimized:
                MessageBox.Show("Minimized");
                break;
            case WindowState.Normal:
                MessageBox.Show("Normal");
                break;
        }
    }

Obviously if I was just printing out the state I'd use this.WindowState.ToString() ;)

The following should get added to the XAML defintion of your window by Visual Studio:

StateChanged="Window_StateChanged"
like image 77
ChrisF Avatar answered Sep 20 '22 17:09

ChrisF