Placing Your C# Application in the System Tray
There are many cases when it's advantageous to place an application's icon in the System Tray. For example, firewall/antivirus and instant messaging applications do this so as to run in the background and still be accessible to the user without crowding up the task bar.
In this week's installment of my .NET Tips and Techniques series, I'll show you the steps involved in specifying that an application is to be minimized to the Tray, how to allow the user to restore the application by double-clicking the icon, and how to create and respond to a System Tray icon's context menu.
- To get started, open an existing C# Windows form (or create a new one).
- Open the Visual Studio Toolbox.
- Drag a NotifyIcon control onto the form. The control will named notifyIcon1 by default and placed below the form because it has no visual representation on the form itself.
- Set the NotifyIcon control's Text property to the name you want to appear when the user pauses the mouse over the application's icon. For example, this value could be "KillerApp 1.0".
- Set the control's Icon property to the icon that you want to appear in the System Tray.
- Add an event handler for the form's Resize event that will hide the application when it's minimized. That way, it won't appear on the task bar.
- Add an event handler for the NotifyIcon.DoubleClick event and code it as follows so that the application will be restored when the icon is double-clicked.
Tip: If you have a BMP file that you want to convert to an icon file, I highly recommend the QTam Bitmap to Icon 3.5 application.
private void Form1_Resize(object sender, System.EventArgs e)
{
if (FormWindowState.Minimized == WindowState)
Hide();
}
private void notifyIcon1_DoubleClick(object sender,
System.EventArgs e)
{
Show();
WindowState = FormWindowState.Normal;
}
At this point, your application will fuction perfectly in terms of an icon appearing in the System Tray when the application is run (see Figure 1), the application not appearing on the task bar when minimized and the application restoring itself when the Tray icon is double-clicked.
Figure 1
Now, let's see the steps involved with adding a context menu to the icon.
- From the Visual Studio Toolbox, drag a ContextMenu control onto the form.
- Right-click the ContextMenu control and select the Edit Menu.option.
- Type in the options that you want to appear in your context menu. For example, you can add options such as Restore and Close Application.
- As with any menu, double-click the menu item to create and code each item's handler. As an example, you could copy the code from the form's DoubleClick handler into the context menu's Restore handler and for the Close Application menu item; simply call the form's Close method.
- Finally, set the NotifyIcon control's ContextMenu property to the new context menu you just created by selecting the menu from the drop-down list. Figure 2 shows a simple Tray context menu.
Figure 2

Comments
Amazing!
Posted by wherlereeni on 10/22/2012 10:44amSorry but viagra dublin , cialis mg 20 italia
Replynice
Posted by ganesh on 09/14/2012 12:30amsimple and usable thank you..
Replyworked successfully
Posted by thebestterminator on 09/22/2008 03:17amwell done
ReplyGreat article
Posted by dandar3 on 06/01/2008 06:06amThanks Tom, great article! Dan
ReplyGreat Lesson
Posted by fototheory on 01/18/2008 01:42pmSimple explanation. I like it. Thanks.
ReplyCode doesn't work for me.
Posted by jpladue on 04/02/2007 05:08pmI added the controls and code into my app but the icon appears immediately in the tasktray when the app runs, and the app doesn't disappear from the taskbar. Also, doubleclicking on the tasktray icon does nothing, but I can right click and access the the context menu items which work fine.
-
ReplyFix to get app off the taskbar
Posted by Globox on 09/11/2007 10:45amDo you have the following in your InitializeComponent method? this.Resize += new System.EventHandler(this.Form1_Resize); if not, then add it A notifyIcon1_MouseDoubleClick event Show(); should solve your doubleclicking problem.
Replystill no tray icon for me...
Posted by hillscottc on 04/02/2005 08:58am-
-
Replyu may forget
Posted by thebestterminator on 09/22/2008 03:20am1. make sure that u browse to an icon in the icon tab found n the property menu for the notify icon 2.make sure that the notify icon visible property is set to true good luck
ReplyOK, I figured it out.
Posted by hillscottc on 04/02/2005 09:06amI added the Icon property to the Form, but not the notifyIcon itself. So it had no icon, of course, in the Tray. Silly me.
ReplyShortcut to tray icon
Posted by skory on 03/15/2005 08:04amGreat article, it helps me very much. But still I have one problem. How to call application from tray only with key shortcut (without using mouse). If you could help me with this I would be thankful.
-
ReplyOne possible solution
Posted by Tom Archer on 03/15/2005 09:55amOff the top of my head, I would suggest using a global keyboard hook. I explain how they're done in my Visual C++ Bible, but I don't think I ever did an article on it. However, nowadays you can easily google for the sort of thing and you'll find many examples of how to write and install a hook that will do what you want. Simply capture the desired key and show the application's main window (ShowWindow).
Reply5 Stars
Posted by mjhadden on 05/18/2004 06:48amAt last, an explanation\hint that is not written in gobblygook. Would have saved me 3 hours if I had looked here first.
-
ReplyPerfect!
Posted by Tom Archer on 05/18/2004 12:30pmThis is exactly what I'm aiming for. Thanks for the feedback!
ReplySimply and adequately explained.
Posted by garylutchansky on 04/13/2004 11:27pmGreat article. The concepts are explained simply and adequately. Thanks!
-
ReplyChange of pace for me
Posted by Tom Archer on 04/14/2004 09:56pmThanks Gary. As you probably know, I'm accustomed to writing longer articles that cover more technically difficult subjects. However, with this series I'm trying more for shorter, succinct "tips" that while not earth-shaking in difficulty might save someone an hour or so of digging around through MSDN or the web. Comments like yours let me know that I'm hitting the mark. Thanks again for the input.
ReplyLoading, Please Wait ...