如何以编程方式将ContextMenu添加到系统托盘图标?

时间:2013-07-30 11:56:07

标签: c# wpf contextmenu right-click trayicon

我想以编程方式在我的托盘图标上添加一个上下文菜单,这样当我右键单击托盘图标时,它应该显示菜单。如何为托盘图标编写右键单击事件处理程序?

我试过以下内容:

private void Icon_MouseRightClick(object sender, MouseButtonEventArgs e)
{
 if (e.Button == System.Windows.Forms.MouseButtons.Left)  // shows error ate button
 {
   return;
 }
 if (e.Button == System.Windows.Forms.MouseButtons.Right)
 {
   // code for adding context menu
 }
}

声明Eventhandler为,

NotifyIcon.MouseRightClick += new MouseButtonEventHandler(NotifyIcon_MouseRightClick);

1 个答案:

答案 0 :(得分:18)

右键单击上下文菜单是自动的,无需处理。只需构建菜单并将其分配给NotifyIcon.ContextMenu