42

I'm making a new WPF application and I need to be able to minimize the application and have nice and snug in the system tray, right beside the clock (or in that general area).

This has to work on Windows XP, Vista and 7. I don't have to support older versions of Windows.

What's the simplest way to achieve this if I'm using .NET 4?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Only Bolivian Here
  • 35,719
  • 63
  • 161
  • 257

3 Answers3

80

Example in MSDN forum

Here's a quick example to show how to minimize to the notification area. You need to add references to the System.Window.Forms and System.Drawing assemblies.

public partial class Window1 : System.Windows.Window
{

    public Window1()
    {
        InitializeComponent();

        System.Windows.Forms.NotifyIcon ni = new System.Windows.Forms.NotifyIcon();
        ni.Icon = new System.Drawing.Icon("Main.ico");
        ni.Visible = true;
        ni.DoubleClick += 
            delegate(object sender, EventArgs args)
            {
                this.Show();
                this.WindowState = WindowState.Normal;
            };
    }

    protected override void OnStateChanged(EventArgs e)
    {
        if (WindowState == System.Windows.WindowState.Minimized)
            this.Hide();

        base.OnStateChanged(e);
    }
}
Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
LaGrandMere
  • 10,265
  • 1
  • 33
  • 41
  • 1
    So I gotta have an Icon called "Main.ico" in my ApplicationDirectory ? – Felix D. Jul 04 '16 at 11:06
  • 1
    LeGrandMere - thanks for excellent succint fix. Felix D: You can add Icon using: var iconStream = Application.GetResourceStream(new Uri( "pack://application:,,,/LaunchPad.UI;component/Images/Launch.ico"))?.Stream; then do var ni = new NotifyIcon { Icon = new Icon(iconStream), Visible = true }; – yonsk Dec 16 '16 at 10:14
  • add icon to your solution and then open project's properties and change the post build events like this: copy /Y "$(SolutionDir)Utility\Main.ico" "$(SolutionDir)Utility\bin\Debug\Main.ico" – Barış Akkurt Jun 05 '19 at 15:56
  • I had a hard time accessing the icon file. My solution was now: Add the file into the application directory. In solution explorer change the build setting to `embedded resource`. Then the icon can be accessed via: `Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("YOUR_NAMESPACE.ICON_FILENAME"))` and set on the NotificationIcon like in the answer `ni.Icon = new System.Drawing.Icon(stream);` – Matthias Tylkowski May 05 '20 at 07:39
  • Works well. As of 2020 in VS 2019 16.7.3, the presence of `base.OnStateChanged(e)` causes a stack overflow. – Emil Mocan Sep 21 '20 at 13:17
  • Also, don't forget to dispose of the notification icon once the window is closed. Then is better to make it a member. – Emil Mocan Sep 21 '20 at 13:29
18

I've had success using this free notify-icon implementation in WPF.

http://www.hardcodet.net/projects/wpf-notifyicon

It's pretty simple to setup and the source code is provided. It doesn't rely on Windows Forms, so it's 'pure' WPF and very customizable.

You can find a tutorial on how to use it on CodeProject.
And here is the Nuget Package

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
Alex McBride
  • 6,881
  • 3
  • 29
  • 30
0

Add notifyIcon to your App from Toolbox.
Select your main form >> go to the Properties >> select Events icon >> under FromClosing event type MainForm_FormClosing >> hit enter.

enter image description here

In opened .cs file enter following event action:

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
     this.Hide();
     notifyIcon.Visible = true;
     ShowInTaskbar = false;
     e.Cancel = true;
}

Now your main FORM window will be minimized to the system tray when you click on X button. Next step is to get FORM back to normal state.
Go to the Properties of your notifyIcon >> find DoubleClick event >> type NotifyIcon_DoubleClick and hit enter to get event function created for you.

enter image description here

Put this code to your event:

private void NotifyIcon_DoubleClick(object sender, EventArgs e)
{
    this.Show();
    notifyIcon.Visible = false;
}

Now, if you want to make the notify icon in fancy style you can add context menu and link it to your notify icon, so you get something like that:

enter image description here

Here is where you link contextMenuStrip to NotifyIcon:

enter image description here

Good luck!

Community
  • 1
  • 1
Serge V.
  • 3,377
  • 3
  • 20
  • 28