How to add ContextMenu to the system tray icon programmatically?

天涯浪子 提交于 2020-08-22 09:45:12

问题


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);

回答1:


Context menu on right-click is automatic, no need to handle it. Just build your menu and assign it to NotifyIcon.ContextMenu.



来源:https://stackoverflow.com/questions/17946380/how-to-add-contextmenu-to-the-system-tray-icon-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!