如何:将快捷菜单与 Windows 窗体 NotifyIcon 组件关联
提示
尽管 MenuStrip 和 ContextMenuStrip 取代了早期版本的 MainMenu 和 ContextMenu 控件并添加了功能,但是,可以选择保留 MainMenu 和 ContextMenu 以实现向后兼容并供将来使用。
NotifyIcon 组件在任务栏的状态通知区域中显示一个图标。 通常,应用程序允许您右击此图标将命令发送到它表示的应用程序。 将 ContextMenu 组件与 NotifyIcon 组件关联,便可以将此功能添加到应用程序中。
提示
如果要让应用程序在启动时最小化,同时在任务栏中显示 NotifyIcon 组件的一个实例,可将主窗体的 WindowState 属性设置为 Minimized,并确保将 NotifyIcon 组件的 Visible 属性设置为 true。
在设计时将快捷菜单与 NotifyIcon 组件关联
向窗体添加 NotifyIcon 组件,并设置重要属性,如 Icon 和 Visible 属性。
有关更多信息,请参见 如何:使用 Windows 窗体 NotifyIcon 组件向任务栏添加应用程序图标。
向 Windows 窗体添加 ContextMenu 组件。
向表示您希望在运行时使其可用的命令的快捷菜单添加菜单项。 这也是向这些菜单项添加菜单增强功能的好时机,如访问键。
将 NotifyIcon 组件的 ContextMenu 属性设置为您添加的快捷菜单。
设置此属性后,单击任务栏上的图标时将显示快捷菜单。
以编程方式将快捷菜单与 NotifyIcon 组件关联
使用应用程序所需的任何属性设置(NotifyIcon 组件的 Icon 和 Visible 属性,ContextMenu 组件的菜单项),创建 NotifyIcon 类的一个实例和一个 ContextMenu 类。
将 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 NotifyIcon notifyIcon1 = new NotifyIcon();
public ContextMenu contextMenu1 = new ContextMenu();
public void createIconMenuStructure()
{
// Add menu items to shortcut menu.
contextMenu1.get_MenuItems().Add("&Open Application");
contextMenu1.get_MenuItems().Add("S&uspend Application");
contextMenu1.get_MenuItems().Add("E&xit");
// Set properties of NotifyIcon component.
notifyIcon1.set_Icon(new System.Drawing.Icon
(System.Environment.GetFolderPath
(System.Environment.SpecialFolder.Personal)
+ "\\Icon.ico"));
notifyIcon1.set_Text("Right-click me!");
notifyIcon1.set_Visible(true);
notifyIcon1.set_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;
}
提示
必须初始化 notifyIcon1 和 contextMenu1,,可以通过在窗体的构造函数中包含以下语句来执行此操作:
notifyIcon1 = gcnew System::Windows::Forms::NotifyIcon();
contextMenu1 = gcnew System::Windows::Forms::ContextMenu();
请参见
任务
如何:使用 Windows 窗体 NotifyIcon 组件向任务栏添加应用程序图标