Minimize a WPF application to System Tray in C#
Note:
I have migrated this blog to a new location (http://howcanidoit.in). Please click here to go this post in the new site…
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, we 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.
Thanks for nice blog! I think it’s useful. http://www.wowebook.co.cc/net/wpf-recipes-in-c-2008-a-problem-solution-approach/
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.
It is working good for me… Could you sent me a sniplet of the code or something? Have you added the StateChanged event properly?
i’m using the same config as simon: vs2010, .net 4 client profile under win7 x64. works without a glitch. thanks for the code!
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.
I want to set my article in treeview in wordpress blog ……………….
can you help me ………..
I want to to this in Notification area in WPF Broswer Application………….
Can you help me
Can you explain more on your requirement…
Thanks U saved me a lot of time …
Hey it worked..