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.
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.
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.
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.
private void Form1_Resize(object sender, System.EventArgs e)
if (FormWindowState.Minimized == WindowState)
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.
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, the application not appearing on the task bar when minimized and the application restoring itself when the Tray icon is double-clicked.
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.