如何:将快捷菜单与 Windows 窗体 NotifyIcon 组件相关联

注意

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

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

注释

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

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

  1. NotifyIcon 组件添加到窗体,并设置重要属性,例如 IconVisible 属性。

    有关详细信息,请参阅 如何:使用 Windows 窗体 NotifyIcon 组件将应用程序图标添加到 TaskBar。

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

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

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

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

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

  1. 创建 NotifyIcon 类和 ContextMenu 类的实例,并根据应用程序需要进行相应的属性设置(NotifyIcon 组件的IconVisible 属性,以及 ContextMenu 组件的菜单项)。

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

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

    注意

    下面的代码示例创建基本菜单结构。 需要自定义适合所开发应用程序的菜单选项。 此外,还需要编写代码来处理这些菜单项的 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();  

另请参阅