Раздел «Технологии программирования».NotifyIcon:
-- ViktoryaSavraeva? - 29 May 2004

NotifyIcon Class


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.

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.

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

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

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

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.

Example The following example demonstrates using the NotifyIcon class to display an icon for an application in the notification area. The example demonstrates setting the Icon, ContextMenu, Text and Visible properties and handling the DoubleClick event. A ContextMenu with a Exit item on it is assigned to the NotifyIcon. ContextMenu property that allows the user to close the application. When the NotifyIcon. DoubleClick event occurs, the application form is activated by calling the Form . Activate method. The next code is taken from www.codeproject.com

using System;
using System.Drawing;
using System.Windows.Forms;
public class Form1 : System.Windows.Forms.Form
    private System.Windows.Forms.NotifyIcon notifyIcon1;
    private System.Windows.Forms.ContextMenu contextMenu1;
    private System.Windows.Forms.MenuItem menuItem1;
    private System.ComponentModel.IContainer components;
    static void Main() 
        Application.Run(new Form1());
    public Form1()
        this.components = new System.ComponentModel.Container();
        this.contextMenu1 = new System.Windows.Forms.ContextMenu();
        this.menuItem1 = new System.Windows.Forms.MenuItem();
        // Initialize contextMenu1
                    new System.Windows.Forms.MenuItem[] {this.menuItem1});
        // Initialize menuItem1
        this.menuItem1.Index = 0;
        this.menuItem1.Text = "E&xit";
        this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
        // Set up how the form should be displayed.
        this.ClientSize = new System.Drawing.Size(292, 266);
        this.Text = "Notify Icon Example";
        // Create the NotifyIcon.
        this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
        // The Icon property sets the icon that will appear
        // in the systray for this application.
        notifyIcon1.Icon = new Icon("appicon.ico");
        // The ContextMenu property sets the menu that will
        // appear when the systray icon is right clicked.
        notifyIcon1.ContextMenu = this.contextMenu1;
        // The Text property sets the text that will be displayed,
        // in a tooltip, when the mouse hovers over the systray icon.
        notifyIcon1.Text = "Form1 (NotifyIcon example)";
        notifyIcon1.Visible = true;
        // Handle the DoubleClick event to activate the form.
        notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);
    protected override void Dispose( bool disposing )
        // Clean up any components being used.
        if( disposing )
            if (components != null)
        base.Dispose( disposing );
    private void notifyIcon1_DoubleClick(object Sender, EventArgs e) 
        // Show the form when the user double clicks on the notify icon.
        // Set the WindowState to normal if the form is minimized.
        if (this.WindowState == FormWindowState.Minimized)
            this.WindowState = FormWindowState.Normal;
        // Activate the form.
    private void menuItem1_Click(object Sender, EventArgs e) {
        // Close the form, which closes the application.


Creating and Using Tray Icons

To create a Tray Icon, you need to call the following function: -

private void SendToTrayButton_Click(object sender, System.EventArgs e)

Feel free to add more events or to improve on these ones.

Attachment sort Action Size Date Who Comment
image002.jpg manage 20.9 K 29 May 2004 - 20:32 ViktoryaSavraeva?  
image003.jpg manage 3.1 K 29 May 2004 - 20:38 ViktoryaSavraeva?