Virtual Developer Workshop: Containerized Development with Docker

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.

  1. To get started, open an existing C# Windows form (or create a new one).
  2. Open the Visual Studio Toolbox.
  3. 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.
  4. 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".
  5. Set the control's Icon property to the icon that you want to appear in the System Tray.
  6. 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.
  7. 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.
  8. private void Form1_Resize(object sender, System.EventArgs e)
       if (FormWindowState.Minimized == WindowState)
  9. 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.
  10. private void notifyIcon1_DoubleClick(object sender,
                                         System.EventArgs e)
        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.

  1. From the Visual Studio Toolbox, drag a ContextMenu control onto the form.
  2. Right-click the ContextMenu control and select the Edit Menu.option.
  3. 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.
  4. 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.
  5. 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

About the Author

Tom Archer - MSFT

I am a Program Manager and Content Strategist for the Microsoft MSDN Online team managing the Windows Vista and Visual C++ developer centers. Before being employed at Microsoft, I was awarded MVP status for the Visual C++ product. A 20+ year veteran of programming with various languages - C++, C, Assembler, RPG III/400, PL/I, etc. - I've also written many technical books (Inside C#, Extending MFC Applications with the .NET Framework, Visual C++.NET Bible, etc.) and 100+ online articles.


  • nice

    Posted by ganesh on 09/14/2012 07:30am

    simple and usable thank you..

  • worked successfully

    Posted by thebestterminator on 09/22/2008 10:17am

    well done

  • Great article

    Posted by dandar3 on 06/01/2008 01:06pm

    Thanks Tom, great article! Dan

  • Great Lesson

    Posted by fototheory on 01/18/2008 09:42pm

    Simple explanation. I like it. Thanks.

  • Code doesn't work for me.

    Posted by jpladue on 04/03/2007 12:08am

    I 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.

    • Fix to get app off the taskbar

      Posted by Globox on 09/11/2007 05:45pm

      Do 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.

  • still no tray icon for me...

    Posted by hillscottc on 04/02/2005 04:58pm

    I can't figure what I have done wrong, while trying to implement this.
    I added the two event handlers, as you indicated. I used the IDE to specify the event handlers, which auto-generated :
    this.Resize += new System.EventHandler(this.Form1_Resize);
    this.notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);
    for me as needed.
    But when I minimize the app, it just dissapears all together. I don't see it in my System Tray. What am I doing wrong? Thanks.

    • u may forget

      Posted by thebestterminator on 09/22/2008 10:20am

      1. 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

    • OK, I figured it out.

      Posted by hillscottc on 04/02/2005 05:06pm

      I added the Icon property to the Form, but not the notifyIcon itself. So it had no icon, of course, in the Tray. Silly me.

  • Shortcut to tray icon

    Posted by skory on 03/15/2005 04:04pm

    Great 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.

    • One possible solution

      Posted by Tom Archer on 03/15/2005 05:55pm

      Off 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).

  • 5 Stars

    Posted by mjhadden on 05/18/2004 01:48pm

    At last, an explanation\hint that is not written in gobblygook. Would have saved me 3 hours if I had looked here first.

    • Perfect!

      Posted by Tom Archer on 05/18/2004 07:30pm

      This is exactly what I'm aiming for. Thanks for the feedback!

  • Simply and adequately explained.

    Posted by garylutchansky on 04/14/2004 06:27am

    Great article. The concepts are explained simply and adequately. Thanks!

    • Change of pace for me

      Posted by Tom Archer on 04/15/2004 04:56am

      Thanks 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.

  • Release Resources

    Posted by msbon on 04/10/2004 11:50am

    when exit,make sure add the line [notifyIcon1.Dispose();]to release the resources

    • Clarify?

      Posted by hillscottc on 04/02/2005 05:09pm

      you say 'when exit' .... can you explain a bit more please?

    • Thanks

      Posted by Tom Archer on 04/15/2004 04:50am

      I'll send this correction to the folks at CG so they can update the article.

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date