且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-06 14:30:41

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

修改

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

  notifyContextMenu =新System.Windows.Forms.ContextMenuStrip();
VAR exitMenuItem = notifyContextMenu.Items.Add(退出);
exitMenuItem.Image = ...;
 

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;