Nasıl yapılır: Bir Kısayol Menüsünü Windows Forms NotifyIcon Bileşeniyle İlişkilendirme

Not

önceki sürümlerin ve denetimlerine ve işlevlerini değiştirse ve eklese de, ve hem geriye dönük uyumluluk hem de tercih edersanız MenuStripContextMenuStrip gelecekteki kullanım için MainMenuContextMenuMainMenuContextMenu korunur.

Bileşen, NotifyIcon görev çubuğunun durum bildirim alanında bir simge görüntüler. Uygulamalar genellikle bu simgeye sağ tıklar ve temsil ettiği uygulamaya komut gönderir. Bir bileşeni ContextMenu bileşenle bağ bağ NotifyIcon bağarak bu işlevi uygulamalarınıza eklemek için kullanabilirsiniz.

Not

Görev çubuğunda bileşenin bir örneğini görüntülerken, uygulamanın başlangıçta en aza indirilmesine yardımcı olmak için ana formun özelliğini olarak ayarlayın ve bileşenin özelliğinin olarak ayarlanmış NotifyIconWindowState olduğundan emin MinimizedNotifyIconVisibletrue olun.

Bir kısayol menüsünü tasarım zamanında NotifyIcon bileşeniyle ilişkilendirmek için

  1. Formnize NotifyIcon bir bileşen ekleyin ve ve özellikleri gibi önemli özellikleri IconVisible ayarlayın.

    Daha fazla bilgi için, bkz. How to: Add Application Icons to the TaskBar with the Windows Forms NotifyIcon Component.

  2. Windows ContextMenu Form' Windows ekleyin.

    Çalışma zamanında kullanılabilir hale eklemek istediğiniz komutları temsil eden kısayol menüsüne menü öğeleri ekleyin. Bu, erişim anahtarları gibi bu menü öğelerine menü geliştirmeleri eklemek için de iyi bir zaman.

  3. Bileşenin ContextMenuNotifyIcon özelliğini, eklenen kısayol menüsüne ayarlayın.

    Bu özellik ayarlendiğinde, görev çubuğundaki simgeye tıklendiğinde kısayol menüsü görüntülenir.

Bir kısayol menüsünü program aracılığıyla NotifyIcon bileşeniyle ilişkilendirmek için

  1. Uygulama için gerekli olan özellik ayarlarıyla ( ve bileşenin özellikleri, bileşenin menü öğeleri) ile sınıfının ve sınıfının NotifyIconContextMenu bir örneğini IconVisibleNotifyIconContextMenu oluşturun.

  2. Bileşenin ContextMenuNotifyIcon özelliğini, eklenen kısayol menüsüne ayarlayın.

    Bu özellik ayarlendiğinde, görev çubuğundaki simgeye tıklendiğinde kısayol menüsü görüntülenir.

    Not

    Aşağıdaki kod örneği temel bir menü yapısı oluşturur. Menü seçimlerini geliştirmekte olduğunu uygulamaya uygun olacak şekilde özelleştirmeniz gerekir. Ayrıca, bu menü öğelerinin olaylarını işlemek Click için kod yazmak da gerekir.

    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;  
   }  

Not

Form notifyIcon1contextMenu1, oluşturucusuza aşağıdaki deyimleri dahil eder ve bunu yapabilirsiniz:

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

Ayrıca bkz.