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

use*_*971 9 c# wpf trayicon contextmenu right-click

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

我试过以下:

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
 }
}
Run Code Online (Sandbox Code Playgroud)

声明Eventhandler为,

NotifyIcon.MouseRightClick += new MouseButtonEventHandler(NotifyIcon_MouseRightClick);
Run Code Online (Sandbox Code Playgroud)

pie*_*dar 19

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