Minimize a WPF application to System Tray in C#

There is no direct way to minimize a WPF application  to system tray. This does not means that we cannot do that at all.

To minimize WPF app to System Tray/add the Tray notification, imagewe just need to add the “System.Windows.Forms” and “System.Drawing” assembly references to your WPF project. Once we add the references, we can create new instance of the “NotifyIcon” class and  use it in our WPF application.

 

After we add the references to the WPF application, we have to create a global variable for NotifyIcon.

private System.Windows.Forms.NotifyIcon MyNotifyIcon;

Now we have to initiate the NotifyIcon. This can be done in the Window’s constructor. We also have to declare an Event to retrieve back the application from SystemTray.

public MainWindow()
{
    InitializeComponent();
    MyNotifyIcon = new System.Windows.Forms.NotifyIcon();
    MyNotifyIcon.Icon = new System.Drawing.Icon(
                    @"C:\Archive\Icon-Archive.ico");
    MyNotifyIcon.MouseDoubleClick += 
        new System.Windows.Forms.MouseEventHandler
            (MyNotifyIcon_MouseDoubleClick);
}

To minimize the application to the system tray we need to handle the appropriate events. There are multiple events that can be used to achieve the output… The most appropriate will be the Form SizeChanged event.

To revive the app back we need to write a couple more lines. This time the event to be handled is NotifyIcon MouseDoubleClick or NotifyIcon MouseClick.

void MyNotifyIcon_MouseDoubleClick(object sender, 
   System.Windows.Forms.MouseEventArgs e)
{
   this.WindowState = WindowState.Normal;
}

private void Window_StateChanged(object sender, EventArgs e)
{
   if (this.WindowState == WindowState.Minimized)
   {
      this.ShowInTaskbar = false;
      MyNotifyIcon.BalloonTipTitle = "Minimize Sucessful";
      MyNotifyIcon.BalloonTipText = "Minimized the app ";
      MyNotifyIcon.ShowBalloonTip(400);
      MyNotifyIcon.Visible = true;
   }
   else if (this.WindowState == WindowState.Normal)
   {
      MyNotifyIcon.Visible = false;
      this.ShowInTaskbar = true;
   }
}

If you are not interested in the balloon tip, we can omit the BalloonTip and ShowBalloon lines.

About these ads
    • Simon Saygal
    • October 23rd, 2011

    Hi, Not works for me. I am using VS 2010 .NET 4.0 and running on windows 7 x64.. it’s just minimizing on task bar but not in system tray. Please let me know what i am doing wrong.

      • Ebby Peter
      • October 23rd, 2011

      It is working good for me… Could you sent me a sniplet of the code or something? Have you added the StateChanged event properly?

    • henry
    • January 3rd, 2012

    i’m using the same config as simon: vs2010, .net 4 client profile under win7 x64. works without a glitch. thanks for the code!

    • Paul
    • January 3rd, 2012

    Works for me. VS2010, .Net 4.0 client profile, Win7 x64.

    However, the balloon will not show unless you put the line
    MyNotifyIcon.Visible = true;
    BEFORE the call to ShowBalloonTip.

    • tejasbhalani
    • June 2nd, 2012

    I want to set my article in treeview in wordpress blog ……………….
    can you help me ………..

    • tejasbhalani
    • June 2nd, 2012

    I want to to this in Notification area in WPF Broswer Application………….
    Can you help me

      • Ebby Peter
      • June 2nd, 2012

      Can you explain more on your requirement…

    • Dhanushka Madushan Lk
    • September 6th, 2012

    Thanks U saved me a lot of time …

  1. Hey it worked..

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: