简体   繁体   中英

How to know if a GraphicsPath contains a point in C#

I'm using .NET to draw a diagram, and I want to highlight objects when the user performs a click on them. It is easy when a figure is fully contained in a rectangle:

if (figure.Bounds.Contains(p)) // bounds is a rectangle

But I don't know how to manage it if the figure is a complex GraphicsPath .

I have defined the following GraphicsPath for the figure (the green circle).

的GraphicsPath

I want to highlight the figure when the user click on it. I would like to know if a Point is contained in that GraphicsPath .

Any ideas? Thanks in advance.

我不知道DrawingPath (你的意思是:graphics.DrawPath)但是GraphicsPathIsVisible方法来检查一个点是否在路径中。

bool isInPath = graphicsObj.IsVisible(point)

Using both .IsOutlineVisible and .IsVisible together cover the whole thing, border and within border, for this rectangle example, but as you know GraphicsPath can works for different shapes.

  bool b = gp.IsVisible(point) || gp.IsOutlineVisible(point, pen);     

For it in code

 Rectangle r = new Rectangle(new Point(50, 100), new Size(500, 100));
 bool b;
 // say Point p is set.
 // say Pen pen is set.

 using (var gp = new GraphicsPath())
 using (var pen = new Pen(Color.Black, 44)) {
    gp.AddRectangle(r);
    bool b = gp.IsVisible(point) || gp.IsOutlineVisible(point, pen);              
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM