Thursday, February 4, 2010

Creating a system tray application in C#

Many a times, we want to create applications that sit in the system tray (the area where the clock, volume control etc are displayed) instead of/in addition to the taskbar. Or, when the user minimizes the application window, we would want it to work in the background and display an icon for it in the system tray. Doing this is very easy and can be accomplished by the following steps:

  1. Hiding the form
  2. Turn OFF the taskbar display
  3. Setting properties for display in system tray
  • Hiding the form – This is pretty straightforward and needs just 2 lines of C# code
   1: this.WindowState = FormWindowState.Minimized;


   2: this.Hide();






where “this” refers to the form being hidden




  • Set taskbar display to be OFF – This needs setting just a single property which can be set either at design time or at run time, as follows





   1: this.ShowInTaskbar = false;




again, “this” refers to the form being hidden




  • Setting properties for display in system tray – To display the application icon in the system tray, we need to drop the NotifyIcon control from the toolbox onto the form. The control will be displayed under the actual form on the design surface. After dropping the control, we can set the following properties for it:



    • Text – This is the text that will be displayed when you hover the mouse over the system tray icon


    • Icon – An "ico” icon file which will be used as the icon in the system tray


    • BalloonTipText – The text which will be displayed when you enable the balloon tip


    • BalloonTipTitle – The title of the balloon tip when it is enabled




So there you have it, 3 simple steps to enable system tray display for your application.

No comments:

Post a Comment