如何将图标添加到System.Windows.Forms.MenuItem?如何将、图标、System、MenuItem

2023-09-04 00:21:40 作者:都是矿泉水装什么纯中纯

我想一个图标添加到我的上下文菜单项之一,但我不能这样做。任何人可以帮助我吗?

这里的code我已经写了:

 私人System.Windows.Forms.ContextMenu notifyContextMenu;
 私人无效美孚(){
            如果(NotifyIcon的== NULL){
                NotifyIcon的=新System.Windows.Forms.NotifyIcon();
            }

           如果(notifyContextMenu == NULL){
               notifyContextMenu =新System.Windows.Forms.ContextMenu();
               notifyContextMenu.MenuItems.Add(退出);
               //我如何添加一个图标,该图标上下文菜单项?
             }
            notifyIcon.ContextMenu = notifyContextMenu;
          }
     }
 

解决方案

的MainMenu /文本菜单已过时,您应该使用菜单条的类来代替。

修改

  notifyContextMenu =新System.Windows.Forms.ContextMenu();
notifyContextMenu.MenuItems.Add(退出);
 

  notifyContextMenu =新System.Windows.Forms.ContextMenuStrip();
VAR exitMenuItem = notifyContextMenu.Items.Add(退出);
exitMenuItem.Image = ...;
 
System.Windows.Forms.Message并不包含Show的定义

的http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripitem.image.aspx

最后附上上下文菜单条通知图标,

  notifyIcon.ContextMenuStrip = notifyContextMenu;
 

I tried to add an icon to one of my context menu items, but I couldn't do it. Can anybody help me?

Here's the code I've written:

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

解决方案

MainMenu/ContextMenu are obsolete, you should use the menu strip classes instead.

Change

notifyContextMenu = new System.Windows.Forms.ContextMenu();
notifyContextMenu.MenuItems.Add("Exit");

to

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

Finally attach the context menu strip to notify icon,

notifyIcon.ContextMenuStrip = notifyContextMenu;