可点击的ToolStripMenuItem图像 [英] Clickable ToolStripMenuItem Image
问题描述
我正在开发一个C#winForm应用程序,该应用程序具有一个动态添加toolStripMenuItems的ContextMenuStrip。我在资源文件中也有一个图标,我分配给toolStripMenuItem.Image属性,我处理toolStripMenuItem.Click事件。我实际上想要处理单击事件,如果单击图像(图标)但图标/图像没有单击事件。如何创建一个可以分配给toolStripMenuItem的image属性的点击图标?
提前谢谢
-DA
I''m working on a C# winForm app that has a ContextMenuStrip that adds toolStripMenuItems dynamically. I also have an icon in my resource file that I assign to the toolStripMenuItem.Image property and I handle the toolStripMenuItem.Click event. I actually want to handle the click event if the Image (icon) is clicked but Icon/Image does not have a click event. How do create a click icon that can assigned to the image property of the toolStripMenuItem?
Thanks in advance
-DA
推荐答案
ContextMenuStrip ctxt = new ContextMenuStrip();
ToolStripMenuItem tsmi = new ToolStripMenuItem();
tsmi.Name = "Hello World";
tsmi.Image = Properties.Resources.MyIcon.ToBitMap();
tsmi.MouseUp += tsmi_MouseUp;
ctxt.Items.Add(tsmi);
private void tsmi_MouseUp(object sender, MouseEventArgs e)
{
ToolStripMenuItem tsmi = (ToolStripMenuItem)sender;
Rectangle iconRect = new Rectangle(e.Location, tsmi.Image.Size);
if (iconRect.Contains(e.Location))
{
// do whatever you need to do here...
}
}
这篇关于可点击的ToolStripMenuItem图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!