要在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));