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

前端 未结 2 887
孤街浪徒
孤街浪徒 2021-01-11 23:36

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

相关标签:
2条回答
  • 2021-01-12 00:14

    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);              
      }
    
    0 讨论(0)
  • 2021-01-12 00:25

    I don't know a DrawingPath (you mean probably; graphics.DrawPath) but a GraphicsPath has the IsVisible method to check if a point is in the path.

    bool isInPath = graphicsObj.IsVisible(point)
    
    0 讨论(0)
提交回复
热议问题