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

有帮助吗?

解决方案

You will have to customize the ToolStripItem and obtain the rectangle in which the image lies using the ContentRectangle property.

Have a look at the ComputeImageAndTextLayout code sample on MSDN which calculates the rectangle in which the image lies.

In the MouseDown event of the ToolStripItem you will obtain the coordinates of the mouse in the ToolStripItem when the MouseDown event occurred as shown below

void item_MouseDown(object sender, MouseEventArgs e)
{
    int x = e.X;
    int y = e.Y;
}

You will then have to calculates if your MouseDown coordinates lies in the rectangle obtained and raise a ImageClicked event.

Using the same approach you could also raise a TextClicked event.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top