要在ContextMenuStrip中添加图标,您需要使用ToolStripMenuItem类的Image属性
ContextMenuStrip contextMenuStrip1 = new ContextMenuStrip();
ToolStripMenuItem openToolStripMenuItem = new ToolStripMenuItem("打开");
ToolStripMenuItem closeToolStripMenuItem = new ToolStripMenuItem("关闭");
contextMenuStrip1.Items.AddRange(new ToolStripItem[] { openToolStripMenuItem, closeToolStripMenuItem });
openToolStripMenuItem.Image = openImage;
closeToolStripMenuItem.Image = closeImage;
button1.ContextMenuStrip = contextMenuStrip1;
现在,当您右键单击关联的控件时,应该会看到包含图标的ContextMenuStrip菜单。
注意:如果您想要调整图标的大小,可以使用Image.GetThumbnailImage方法。例如,将图像大小更改为16x16像素:
openToolStripMenuItem.Image = new Bitmap(openImage, new Size(16, 16));
closeToolStripMenuItem.Image = new Bitmap(closeImage, new Size(16, 16));