Ale*_*dre 5 .net contextmenu menu winforms
我试图在我的一个上下文菜单项中添加一个图标,但我无法做到.有谁能够帮我?
这是我写的代码:
 private System.Windows.Forms.ContextMenu notifyContextMenu;
 private void foo() {
            if (notifyIcon == null) {
                notifyIcon = new System.Windows.Forms.NotifyIcon();   
            }
           if (notifyContextMenu == null) {
               notifyContextMenu = new System.Windows.Forms.ContextMenu();
               notifyContextMenu.MenuItems.Add("Exit");
               // How do I add an icon to this context menu item?
             }
            notifyIcon.ContextMenu =  notifyContextMenu;
          }
     }
Cod*_*ray 11
莱克斯李的回答涵盖了最简单的做到这一点的方法:从开关MainMenu控制的MenuStrip控制,它提供了内置的,出的现成的添加图标,每个菜单项的支持.不幸的是,正如我在评论中所讨论的那样,这个解决方案会产生一些难看的后果.
特别是,如果你使用的MenuStrip控制,你的菜单将永远看在新版本的Windows丑陋出来的地方,因为他们自定义的通过可能永远不会被更新.NET代码绘制.当然,他们在Windows XP上看起来很光滑,但这已经是至少5年的旧闻了.从Windows Vista开始,菜单看起来完全不同,这也是您的用户对您的应用程序的期望.世界上最酷的图标不会让你看起来更现代.相比:
               
因此,更多涉及的解决方案是坚持使用MainMenu控件,该控件实际上使用由Windows本身绘制的菜单,但是编写一些处理添加图标的代码.
幸运的是,Wyatt O'Day已经为您编写了一个很棒的自定义控件.您所要做的就是下载它,将其放入您的项目,编译并开始使用它.它是开源的,在BSD许可下获得许可,并且它生成的菜单在所有版本的Windows上看起来都是平台原生的.从他的网站下载,或者从他的介绍和100%准确的咆哮开始.
结果很棒:
               
MainMenu/ContextMenu已过时,您应该使用菜单条类.
更改
notifyContextMenu = new System.Windows.Forms.ContextMenu();
notifyContextMenu.MenuItems.Add("Exit");
至
notifyContextMenu = new System.Windows.Forms.ContextMenuStrip();
var exitMenuItem = notifyContextMenu.Items.Add("Exit");
exitMenuItem.Image = ...;
http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripitem.image.aspx
最后附上上下文菜单条来通知图标,
notifyIcon.ContextMenuStrip = notifyContextMenu;