Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minimizing/Closing Application to system tray using WPF

I want to add application in System Tray when user minimize or close the form. I have done it for the Minimize case. Can anyone tell me that how i can keep my app running and add it into System Tray when I close the form?

public MainWindow()
    {
        InitializeComponent();
        System.Windows.Forms.NotifyIcon ni = new System.Windows.Forms.NotifyIcon();
        ni.Icon = new System.Drawing.Icon(Helper.GetImagePath("appIcon.ico"));
        ni.Visible = true;
        ni.DoubleClick +=
            delegate(object sender, EventArgs args)
            {
                this.Show();
                this.WindowState = System.Windows.WindowState.Normal;
            };
        SetTheme();
    }

    protected override void OnStateChanged(EventArgs e)
    {
        if (WindowState == System.Windows.WindowState.Minimized)
            this.Hide();
        base.OnStateChanged(e);
    }
like image 395
Fahad Avatar asked Dec 03 '14 06:12

Fahad


People also ask

How do I minimize a program to the system tray?

Press Alt + F1 and that window will minimize to the tray.

How minimize windows form to system tray in C#?

This can be done by doing the following in your form's Resize event handler: Check whether the form's WindowState property is set to FormWindowState. Minimized. If yes, hide your form, enable the NotifyIcon object, and show the balloon tip that shows some information. Once the WindowState becomes FormWindowState.

How do I close an application in WPF?

We can close the window either by using "this. Close()"or by using "App. Current. Shutdown()".

What does Minimise to system tray mean?

Minimize to Tray button to every window's title bar so that you can send any window to the system tray in a single mouse click, thus saving precious taskbar space! It is essential for those who work on monitors with low resolution.


Video Answer


2 Answers

You can also override OnClosing to keep the app running and minimize it to the system tray when a user closes the application.

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    // Minimize to system tray when application is minimized.
    protected override void OnStateChanged(EventArgs e)
    {
        if (WindowState == WindowState.Minimized) this.Hide();

        base.OnStateChanged(e);
    }

    // Minimize to system tray when application is closed.
    protected override void OnClosing(CancelEventArgs e)
    {
        // setting cancel to true will cancel the close request
        // so the application is not closed
        e.Cancel = true;

        this.Hide();

        base.OnClosing(e);
    }
}
like image 178
TheMiddleMan Avatar answered Oct 14 '22 04:10

TheMiddleMan


You need not use OnStateChanged(). Instead, use the PreviewClosed event.

public MainWindow()
{
    ...
    PreviewClosed += OnPreviewClosed;
}

private void OnPreviewClosed(object sender, WindowPreviewClosedEventArgs e)
{
    m_savedState = WindowState;
    Hide();
    e.Cancel = true;
}
like image 2
Pavel Ivchenkov Avatar answered Oct 14 '22 04:10

Pavel Ivchenkov