如何:將捷徑功能表與 Windows Form NotifyIcon 元件關聯
注意
雖然 MenuStrip 和 ContextMenuStrip 會取代之前版本的 MainMenu 和 ContextMenu 控制項並新增功能,但您可以選擇保留 MainMenu 和 ContextMenu 以提供回溯相容性並供未來使用。
NotifyIcon 元件會在工作列的狀態通知區域中顯示圖示。 通常,應用程式可讓您以滑鼠右鍵按一下此圖示,將命令傳送至它所代表的應用程式。 將 ContextMenu 元件與 NotifyIcon 元件產生關聯,您即可將這項功能新增至您的應用程式。
注意
如果您要在啟動時將應用程式最小化,同時在工作列中顯示 NotifyIcon 元件的執行個體,請將主表單的 WindowState 屬性設定為 Minimized,並確定 NotifyIcon 元件的 Visible 屬性設定為 true
。
在設計階段將快顯功能表與 NotifyIcon 元件產生關聯
將 NotifyIcon 元件新增至表單,然後設定重要屬性,例如 Icon 和 Visible 屬性。
如需詳細資訊,請參閱如何:使用 Windows Forms NotifyIcon 元件將應用程式圖示加入至 TaskBar。
將 ContextMenu 元件新增至您的 Windows Form。
將功能表項目新增至快顯功能表,代表您希望在執行階段可供使用的命令。 這也是將功能表增強功能新增至這些功能表項目的好時機,例如存取鍵。
將 NotifyIcon 元件的 ContextMenu 屬性設定為您新增的快顯功能表。
設定此屬性後,按一下工作列上的圖示時,就會顯示快顯功能表。
以程式設計方式將快顯功能表與 NotifyIcon 元件產生關聯
建立 NotifyIcon 類別和 ContextMenu 類別的執行個體,並搭配應用程式所需的任何屬性設定 (NotifyIcon 元件的 Icon 和 Visible 屬性、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:
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();