0

I am developing a winforms project. What I need is, draw a clickable polygon on picturebox. Or draw a polygon and detect click if mouse is in polygon. I can draw a polygon but however I can't discover the click part.

Basically, take coordinates and add to array when I click on panel which has picturebox with image. And draw the polygon when clicked 7 times.

But I don't have a idea how to detect if mouse click is in polygon? Or we can say, how to convert these polygons to buttons?


        private void panelPictureBoxBuilding_MouseUp(object sender, MouseEventArgs e)
        {
            if (startMoving)
            {
                Point coord = new Point(e.Location.X, e.Location.Y);
                coordinates.Add(coord);
                if (coordinates.Count == 7)
                {
                    Graphics g = pbBuilding.CreateGraphics();
                    g.DrawPolygon(Pens.Black, coordinates.ToArray());
                }
            }
        }

EDIT : Also how can i start drawing polygon since first mouse click? Current code is drawing polygon when clicked 7 times.

EDIT2 : I got it. Solution is in the below:

GraphicsPath path = new GraphicsPath(FillMode.Winding);
path.AddPolygon(coordinates.ToArray());

// Its mouse click event, after here
if(path.IsVisible(e.Location.X, e.Location.Y))
{
  //Do something
}

0 Answers0