【发布时间】:2016-08-07 21:15:46
【问题描述】:
我想在位于PictureBox 的bmp 中用Graphic.DrawLine() 画一条线,我可以用鼠标移动它。我找不到任何功能来检查鼠标是否在线。我找到了很多方法来检查鼠标是否在Graphic.FillPolygon() 上,但没有关于DrawLine() 的方法。有什么好的办法来检查吗?
编辑: 所以根据建议我做了这样一个功能:
private bool IsPointInPolygon4(Point[] poly, Point p)
{
System.Drawing.Drawing2D.GraphicsPath test = new System.Drawing.Drawing2D.GraphicsPath();
if (poly.Length == 2) // it means there are 2 points, so it's line not the polygon
{
test.AddLine(poly[0], poly[1]);
if (test.IsVisible(p, g))
{
MessageBox.Show("You clicked on the line, congratulations", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
test.Dispose();
return true;
}
}
else
{
test.AddPolygon(poly);
if (test.IsVisible(p, g))
{
MessageBox.Show("You clicked on the polygon, congratulations", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
return true;
}
}
return false;
}
它适用于多边形。但是我仍然无法在线获得鼠标事件。有什么建议吗?
【问题讨论】:
-
它总是一条完美的水平线还是垂直线,没有其他角度?
-
不,不幸的是它可以是任何角度,更重要的是,我想添加一个功能,当有人捕捉到顶点时改变角度
标签: c# winforms system.drawing