Udostępnij za pośrednictwem


How to: Add Application Icons to the TaskBar with the Windows Forms NotifyIcon Component

The Windows Forms NotifyIcon component displays a single icon in the status notification area of the taskbar. To display multiple icons in the status area, you must have multiple NotifyIcon components on your form. To set the icon displayed for a control, use the Icon property. You can also write code in the DoubleClick event handler so that something happens when the user double-clicks the icon. For example, you could make a dialog box appear for the user to configure the background process represented by the icon.

Note

The NotifyIcon component is used for notification purposes only, to alert users that an action or event has occurred or there has been a change in status of some sort. You should use menus, toolbars, and other user-interface elements for standard interaction with applications.

To set the icon

  1. Assign a value to the Icon property. The value must be of type System.Drawing.Icon and can be loaded from an .ico file. You can specify the icon file in code or by clicking the ellipsis button (VisualStudioEllipsesButton screenshot) next to the Icon property in the Properties window, and then selecting the file in the Open dialog box that appears.

  2. Set the Visible property to true.

  3. Set the Text property to an appropriate ToolTip string.

    In the following code example, the path set for the location of the icon is the My Documents folder. This location is used because you can assume that most computers running the Windows operating system will include this folder. Choosing this location also enables users with minimal system access levels to safely run the application. The following example requires a form with a NotifyIcon control already added. It also requires an icon file named Icon.ico.

    [Visual Basic]

    ' You should replace the bold icon in the sample below
    ' with an icon of your own choosing.
    NotifyIcon1.Icon = New _ 
       System.Drawing.Icon(System.Environment.GetFolderPath _
       (System.Environment.SpecialFolder.Personal) _
       & "\Icon.ico")
    NotifyIcon1.Visible = True
    NotifyIcon1.Text = "Antivirus program"
    

    [C#]

    // You should replace the bold icon in the sample below
    // with an icon of your own choosing.
    // Note the escape character used (@) when specifying the path.
    notifyIcon1.Icon = 
       new System.Drawing.Icon (System.Environment.GetFolderPath
       (System.Environment.SpecialFolder.Personal)
       + @"\Icon.ico");
    notifyIcon1.Visible = true;
    notifyIcon1.Text = "Antivirus program";
    

    [cpp]

    // You should replace the bold icon in the sample below
    // with an icon of your own choosing.
    notifyIcon1->Icon = gcnew 
       System::Drawing::Icon(String::Concat
       (System::Environment::GetFolderPath
       (System::Environment::SpecialFolder::Personal),
       "\\Icon.ico"));
    notifyIcon1->Visible = true;
    notifyIcon1->Text = "Antivirus program";
    

See Also

Tasks

How to: Associate a Shortcut Menu with a Windows Forms NotifyIcon Component

Reference

NotifyIcon Component Overview (Windows Forms)

NotifyIcon

Icon

Other Resources

NotifyIcon Component (Windows Forms)