操作指南:将快捷菜单与 Windows 窗体 NotifyIcon 组件相关联

注释

尽管 MenuStripContextMenuStrip 替换和添加以前版本的 MainMenuContextMenu 控件的功能,但如果选择,MainMenuContextMenu 都将保留,以实现后向兼容性和将来使用。

NotifyIcon 组件在任务栏的状态通知区域中显示图标。 通常,应用程序允许右键单击此图标以将命令发送到它所表示的应用程序。 通过将ContextMenu组件与NotifyIcon组件相关联,可以将此功能添加到应用程序。

注释

如果希望在启动时最小化应用程序,同时在任务栏中显示组件的实例 NotifyIcon ,请将主窗体 WindowState 的属性 Minimized 设置为并确保 NotifyIcon 组件 Visible 的属性设置为 true

在设计时将快捷菜单与 NotifyIcon 组件相关联

  1. 在你的窗体中添加一个NotifyIcon组件,并设置重要属性,例如Icon属性和Visible属性。

    有关详细信息,请参阅 如何:使用 Windows Forms NotifyIcon 组件将应用程序图标添加到任务栏

  2. ContextMenu 组件添加到 Windows 窗体。

    将您希望在运行时可用的命令添加到快捷菜单中。 这也是向这些菜单项(如访问键)添加菜单增强功能的好时机。

  3. ContextMenu 组件的属性设置为已添加的 NotifyIcon 快捷菜单。

    设置此属性后,单击任务栏上的图标时将显示快捷菜单。

以编程方式将快捷菜单与 NotifyIcon 组件相关联

  1. 创建一个NotifyIcon类的实例和一个ContextMenu类的实例,并根据应用程序的需要设置必要的属性(为Icon组件设置Visible属性,为NotifyIcon组件设置菜单项ContextMenu)。

  2. ContextMenu 组件的属性设置为已添加的 NotifyIcon 快捷菜单。

    设置此属性后,单击任务栏上的图标时将显示快捷菜单。

    注释

    下面的代码示例创建基本菜单结构。 需要自定义适合所开发应用程序的菜单选项。 此外,还需要编写代码来处理这些菜单项的 Click 事件。

    Public ContextMenu1 As New ContextMenu
    Public NotifyIcon1 As New NotifyIcon
    
    Public Sub CreateIconMenuStructure()
       ' Add menu items to shortcut menu.
       ContextMenu1.MenuItems.Add("&Open Application")
       ContextMenu1.MenuItems.Add("S&uspend Application")
       ContextMenu1.MenuItems.Add("E&xit")
    
       ' Set properties of NotifyIcon component.
       NotifyIcon1.Icon = New System.Drawing.Icon _
          (System.Environment.GetFolderPath _
          (System.Environment.SpecialFolder.Personal)  _
          & "\Icon.ico")
       NotifyIcon1.Text = "Right-click me!"
       NotifyIcon1.Visible = True
       NotifyIcon1.ContextMenu = ContextMenu1
    End Sub
    
public NotifyIcon notifyIcon1 = new NotifyIcon();
public ContextMenu contextMenu1 = new ContextMenu();

public void createIconMenuStructure()
{
   // Add menu items to shortcut menu.
   contextMenu1.MenuItems.Add("&Open Application");
   contextMenu1.MenuItems.Add("S&uspend Application");
   contextMenu1.MenuItems.Add("E&xit");

   // Set properties of NotifyIcon component.
   notifyIcon1.Icon = new System.Drawing.Icon
      (System.Environment.GetFolderPath
      (System.Environment.SpecialFolder.Personal)
      + @"\Icon.ico");
   notifyIcon1.Visible = true;
   notifyIcon1.Text = "Right-click me!";
   notifyIcon1.Visible = true;
   notifyIcon1.ContextMenu = contextMenu1;
}
public:
   System::Windows::Forms::NotifyIcon ^ notifyIcon1;
   System::Windows::Forms::ContextMenu ^ contextMenu1;

   void createIconMenuStructure()
   {
      // Add menu items to shortcut menu.
      contextMenu1->MenuItems->Add("&Open Application");
      contextMenu1->MenuItems->Add("S&uspend Application");
      contextMenu1->MenuItems->Add("E&xit");

      // Set properties of NotifyIcon component.
      notifyIcon1->Icon = gcnew System::Drawing::Icon
          (String::Concat(System::Environment::GetFolderPath
          (System::Environment::SpecialFolder::Personal),
          "\\Icon.ico"));
      notifyIcon1->Text = "Right-click me!";
      notifyIcon1->Visible = true;
      notifyIcon1->ContextMenu = contextMenu1;
   }

注释

必须初始化notifyIcon1contextMenu1,,可以通过在窗体的构造函数中包含以下语句来实现初始化。

notifyIcon1 = gcnew System::Windows::Forms::NotifyIcon();
contextMenu1 = gcnew System::Windows::Forms::ContextMenu();

另请参阅