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.

  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)
          Hide();
    }
    
  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)
    {
        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.

  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.

Comments

  • Amazing!

    Posted by wherlereeni on 10/22/2012 10:44am

    Sorry but viagra dublin , cialis mg 20 italia

    Reply
  • nice

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

    simple and usable thank you..

    Reply
  • worked successfully

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

    well done

    Reply
  • Great article

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

    Thanks Tom, great article! Dan

    Reply
  • Great Lesson

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

    Simple explanation. I like it. Thanks.

    Reply
  • Code doesn't work for me.

    Posted by jpladue on 04/02/2007 05:08pm

    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 10:45am

      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.

      Reply
    Reply
  • still no tray icon for me...

    Posted by hillscottc on 04/02/2005 08:58am

    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 03: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

      Reply
    • OK, I figured it out.

      Posted by hillscottc on 04/02/2005 09:06am

      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.

      Reply
    Reply
  • Shortcut to tray icon

    Posted by skory on 03/15/2005 08:04am

    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 09:55am

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

      Reply
    Reply
  • 5 Stars

    Posted by mjhadden on 05/18/2004 06:48am

    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 12:30pm

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

      Reply
    Reply
  • Simply and adequately explained.

    Posted by garylutchansky on 04/13/2004 11:27pm

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

    • Change of pace for me

      Posted by Tom Archer on 04/14/2004 09:56pm

      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.

      Reply
    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

  • As mobile devices have pushed their way into the enterprise, they have brought cloud apps along with them. This app explosion means account passwords are multiplying, which exposes corporate data and leads to help desk calls from frustrated users. This paper will discover how IT can improve user productivity, gain visibility and control over SaaS and mobile apps, and stop password sprawl. Download this white paper to learn: How you can leverage your existing AD to manage app access. Key capabilities to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds