user1890098
user1890098

Reputation: 495

Is it possible to change the event of Minimize button of a WPF window?

I have a WPF window. It has the minimize button enabled. But the requirement is that on clicking the minimize button, the window should go into the system tray. The following code will hide the window in system tray. I want to execute this code on click event of Minimize button?

CurrentWindowState = WindowState.Minimized;
            OnStartClick();
            Window_StateChanged();


    private void Window_StateChanged()
    {
        if (CurrentWindowState == WindowState.Minimized)
        {
            ShowInTaskBar = false;
            _notifyIcon.BalloonTipTitle = "Minimize Sucessful";
            _notifyIcon.BalloonTipText = "Minimized the app ";
            _notifyIcon.ShowBalloonTip(400);
            _notifyIcon.Visible = true;
        }
        else if (CurrentWindowState == WindowState.Normal)
        {
            _notifyIcon.Visible = false;
            ShowInTaskBar = true;
        }
    }

Is there anyway to do this? Or are there any other ways to do this?

Upvotes: 1

Views: 1295

Answers (1)

pushpraj
pushpraj

Reputation: 13679

add event handler to StateChanged event from Window

eg

this.StateChanged += (s,e) => Window_StateChanged();

and check as follows

if (this.WindowState == WindowState.Minimized)

or

else if (this.WindowState == WindowState.Normal)

assuming this is the instance of window and this code is written in constructor.

Upvotes: 4

Related Questions