2008-09-25 94 views
6

我發現當我爲contextmenustrip(右鍵單擊菜單)執行show()方法時,如果位置在它屬於的窗體的外部,它顯示在任務欄上也是如此。顯示一個沒有顯示在任務欄上的ContextMenuStrip

我想創建一個右擊菜單,當點擊notifyicon時,但由於菜單懸停在系統托盤上方而不是在窗體內(因爲窗體可以在右擊時最小化),它顯示在一些奇怪的原因

任務欄下面是目前我的代碼:

private: System::Void notifyIcon1_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) { 

if(e->Button == System::Windows::Forms::MouseButtons::Right) { 

     this->sysTrayMenu->Show(Cursor->Position); 

    } 
} 

做什麼選擇我需要設置,因此不會出現在任務欄上的空白處理。

+0

同上,我有同樣的問題/錯誤。 – 2009-04-13 18:59:05

回答

7

嘗試將菜單分配給NotifyIcon的ContextMenuStrip屬性,而不是在鼠標單擊處理程序中顯示它。

+0

啊!你是個天才!我不知道他們有這個財產!我再次以'硬'的方式做事:) – Cetra 2008-09-25 13:35:10

0

我的問題是,我的菜單可從雙擊中間點擊通知圖標。

右擊通知圖標時,沒有任務欄按鈕,但是當我手動顯示(Cursor.Position)時,它會顯示一個任務欄按鈕。

1

我們假設您有兩個上下文菜單項:ContextMenuLeftContextMenuRight。默認情況下,從NotifyIcon屬性中已經分配了其中的一個。在致電Left Button Click之前,只需更改它們,顯示上下文菜單,然後再次更改它們。

NotifyIcon.ContextMenuStrip = ContextMenuLeft; //let's asign the other one 
MethodInfo mi = typeof(NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic); 
mi.Invoke(NotifyIcon, null); 
NotifyIcon.ContextMenuStrip = ContextMenuRight; //switch back to the default one 

希望這會有所幫助。

4

最好的和正確的方法,沒有反思是:

{ 
    UnsafeNativeMethods.SetForegroundWindow(new HandleRef(notifyIcon.ContextMenuStrip, notifyIcon.ContextMenuStrip.Handle)); 
    notifyIcon.ContextMenuStrip.Show(Cursor.Position); 
} 

其中UnsafeNativeMethods.SetForegroundWindow是:

public static class UnsafeNativeMethods 
{ 
    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] 
    public static extern bool SetForegroundWindow(HandleRef hWnd); 
}