C# 如何以编程方式将 ContextMenu 添加到系统托盘图标?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17946380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add ContextMenu to the system tray icon programmatically?
提问by user2622971
I want to programmatically add a context menu to my tray icon, so that when I right-click on the tray icon, it should show me the menu.How should I write the right-click event handler for my tray icon?
我想以编程方式将上下文菜单添加到我的托盘图标,这样当我右键单击托盘图标时,它应该显示菜单。我应该如何为我的托盘图标编写右键单击事件处理程序?
I have tried the below:
我尝试了以下方法:
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
}
}
Declared Eventhandler as,
将事件处理程序声明为,
NotifyIcon.MouseRightClick += new MouseButtonEventHandler(NotifyIcon_MouseRightClick);
采纳答案by piedar
Context menu on right-click is automatic, no need to handle it. Just build your menu and assign it to NotifyIcon.ContextMenu.
右键单击的上下文菜单是自动的,无需处理。只需构建您的菜单并将其分配给NotifyIcon.ContextMenu。