如何在MenuItem中添加图标 [英] How to an add an icon to a MenuItem
问题描述
你好!
我有一个添加了各种项目的ContextMenu.
如何在这些项目上添加图标?
我已经用Google搜索过,但是唯一可用的答案是ContextMenuStrips,而不是ContextMenu的.
我想做什么:
Hello!
I have a ContextMenu with assorted items added.
How do I add icons onto these items?
I have googled, but the only available answers deal with ContextMenuStrips, and not ContextMenu''s.
What I want to do:
ContextMenu trayMenu;
trayMenu.MenuItems.Add("Hello");
trayMenu.MenuItems[0].Icon = ....;
不幸的是,.Icon"属性不存在.
请参阅屏幕截图以进行澄清.
我的样子:
http://i52.tinypic.com/5oz586.png [ http://i52.tinypic.com/zx8u1y.png [
Unfortunately the ".Icon" property is non-existent.
See screenshots for clarification.
Mine looks like:
http://i52.tinypic.com/5oz586.png[^]
I want it to look like:
http://i52.tinypic.com/zx8u1y.png[^]
With those icons at the left.
Any help would be greatly appreciated!
推荐答案
为什么不使用ContextMenuStrip
?没问题.
您可以使用System.Windows.Forms.MenuStrip
或System.Windows.Forms.<code>ContextMenuStrip
.对于这些菜单,可以将类System.Windows.Forms.ToolStripMenuItem
的实例用于项目.可以使用构造函数ToolStripMenuItem(String, Image)
,ToolStripMenuItem(String, Image, EventHandler)
或ToolStripMenuItem(String, Image, ToolStripItem[])
用单个图像实例化它.或者,此菜单项可以使用属性ImageKey
或ImageIndex
在图像列表中选择图像,该列表被设置为属性MenuStrip
或ContextMenuStrip
.该属性是继承的属性System.Windows.Forms.ToolStrip.ImageList
.
参见:
http://msdn.microsoft.com/en-us/library/system. windows.forms.toolstrip.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. windows.forms.menustrip.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. windows.forms.contextmenustrip.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripitem.imageindex.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripitem.imagekey.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripitem.imageindex.aspx [ ^ ].
—SA
Why not usingContextMenuStrip
? There is nothing wrong with it.
You can useSystem.Windows.Forms.MenuStrip
orSystem.Windows.Forms.<code>ContextMenuStrip
. For these menus, you can use the instances of the classSystem.Windows.Forms.ToolStripMenuItem
for items. It can be instantiated with individual image using constructorsToolStripMenuItem(String, Image)
,ToolStripMenuItem(String, Image, EventHandler)
orToolStripMenuItem(String, Image, ToolStripItem[])
. Alternatively, this menu item can use propertyImageKey
orImageIndex
to select the image in the image list, which is set as a propertyMenuStrip
orContextMenuStrip
; the property is inherited propertySystem.Windows.Forms.ToolStrip.ImageList
.
See:
http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstrip.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.menustrip.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.contextmenustrip.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripitem.imageindex.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripitem.imagekey.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripitem.imageindex.aspx[^].
—SA
这篇关于如何在MenuItem中添加图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!