12

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

我尝试了以下方法:

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

将事件处理程序声明为,

NotifyIcon.MouseRightClick += new MouseButtonEventHandler(NotifyIcon_MouseRightClick);
4

1 回答 1

27

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

于 2013-07-30T12:13:01.107 回答