[英]How do you get a different Context Menu if you Lt-Click or Rt-Click on a notify icon?
我有一個基於系統托盤的應用程序。 如果你右鍵單擊它我有一個很好的上下文菜單但是我想要一個不同的上下文菜單,如果你點擊它就會出現。 現在我把不同的菜單顯示出來
private void niTrayIcon_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
cmsTrayLeftClick.Show(Cursor.Position);
}
}
這使得菜單顯示但單擊菜單不會使其消失,使菜單消失的唯一方法是單擊項目或rt單擊托盤圖標。
我也想出了這個黑客,但它確實感覺這是正確的方法。
private void niTrayIcon_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
niTrayIcon.ContextMenuStrip = cmsTrayLeftClick;
MethodInfo mi = typeof(NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic);
mi.Invoke(niTrayIcon, null);
niTrayIcon.ContextMenuStrip = cmsTrayRtClick;
}
}
這是正確的做法還是有更優雅的方式?
因為沒有其他人發布了一種有效的方法,我想正確的做法是
private void niTrayIcon_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
niTrayIcon.ContextMenuStrip = cmsTrayLeftClick;
MethodInfo mi = typeof(NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic);
mi.Invoke(niTrayIcon, null);
niTrayIcon.ContextMenuStrip = cmsTrayRtClick;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.