如何:使用 Windows 窗体 NotifyIcon 组件向任务栏添加应用程序图标

Windows 窗体 NotifyIcon 组件在任务栏的状态通知区域中显示单个图标。 若要在状态区域中显示多个图标,窗体上必须有多个 NotifyIcon 组件。 若要设置控件显示的图标,请使用该 Icon 属性。 还可以在 DoubleClick 事件处理程序中编写代码,以便在用户双击图标时发生某种情况。 例如,可以让用户显示一个对话框来配置由图标表示的背景进程。

注释

NotifyIcon 组件仅用于通知目的,提醒用户作或事件已发生,或者某种状态发生了更改。 应使用菜单、工具栏和其他用户界面元素进行标准与应用程序交互。

设置图标

  1. 为属性赋值 Icon 。 该值的类型必须为类型 System.Drawing.Icon ,并且可以从.ico文件加载。 可以在代码中指定图标文件,也可以通过单击Visual Studio 属性窗口中位于Icon属性旁边的省略号按钮(...),然后在显示的打开对话框中选择文件。

  2. Visible 属性设置为 true

  3. Text 属性设置为适当的工具提示字符串。

    在下面的代码示例中,图标位置的路径设置为 “我的文档 ”文件夹。 之所以使用此位置,是因为你可以假定运行 Windows 操作系统的大多数计算机将包含此文件夹。 选择此位置还使具有最小系统访问级别的用户能够安全地运行应用程序。 以下示例需要一个已添加NotifyIcon控件的窗体。 它还需要名为 Icon.ico 的图标文件。

    ' 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"
    
    // 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";
    
    // 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";
    

另请参阅