Easiest way to have a program minimize itself to the system tray using .NET 4

38,842

Solution 1

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);
    }
}

Solution 2

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

Share:
38,842
Only Bolivian Here
Author by

Only Bolivian Here

Updated on July 09, 2022

Comments

  • Only Bolivian Here
    Only Bolivian Here almost 2 years

    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?

  • Felix D.
    Felix D. almost 8 years
    So I gotta have an Icon called "Main.ico" in my ApplicationDirectory ?
  • yonsk
    yonsk over 7 years
    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 };
  • Abimael López
    Abimael López over 6 years
    It seems to me that you are describing Windows Forms instead of WPF.
  • Barış Akkurt
    Barış Akkurt almost 5 years
    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"
  • Amir Astaneh
    Amir Astaneh about 4 years
    The question is about WPF not instead of WINFORM
  • Matthias Tylkowski
    Matthias Tylkowski about 4 years
    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("Y‌​OUR_NAMESPACE.ICON_F‌​ILENAME")) and set on the NotificationIcon like in the answer ni.Icon = new System.Drawing.Icon(stream);
  • Emil Mocan
    Emil Mocan over 3 years
    Works well. As of 2020 in VS 2019 16.7.3, the presence of base.OnStateChanged(e) causes a stack overflow.
  • Emil Mocan
    Emil Mocan over 3 years
    Also, don't forget to dispose of the notification icon once the window is closed. Then is better to make it a member.
  • Damian Ubowski
    Damian Ubowski over 2 years
    Could you show some code samples, please?