在 C# 中使用鼠标点绘制多边形
我需要能够使用鼠标单击位置绘制多边形。 这是我当前的代码:
//the drawshape varible is called when a button is pressed to select use of this tool
if (DrawShape == 4)
{
Point[] pp = new Point[3];
pp[0] = new Point(e.Location.X, e.Location.Y);
pp[1] = new Point(e.Location.X, e.Location.Y);
pp[2] = new Point(e.Location.X, e.Location.Y);
Graphics G = this.CreateGraphics();
G.DrawPolygon(Pens.Black, pp);
}
谢谢
I need to be able to draw a polygon using mouse click locations.
Here is my current code:
//the drawshape varible is called when a button is pressed to select use of this tool
if (DrawShape == 4)
{
Point[] pp = new Point[3];
pp[0] = new Point(e.Location.X, e.Location.Y);
pp[1] = new Point(e.Location.X, e.Location.Y);
pp[2] = new Point(e.Location.X, e.Location.Y);
Graphics G = this.CreateGraphics();
G.DrawPolygon(Pens.Black, pp);
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,这是一些示例代码:
Ok here is some sample code:
首先,添加以下代码:
在您正在绘制的对象上,捕获 OnClick 事件。参数之一应包含单击的 X 和 Y 坐标。将它们添加到点数组中:
最后,在绘制线条的地方,使用以下代码:
编辑:
好的,所以上面的代码并不完全正确。首先,它很可能是 Click 事件而不是 OnClick 事件。其次,要获取鼠标位置,您需要使用点数组声明两个变量,
然后有一个鼠标移动事件:
然后,在您的 Click 事件中:
First, add this code:
On the object you are drawing on, capture the OnClick event. One of the arguments should have the X and Y coordinates of the click. Add them to the points array:
And then finally, where you're drawing the lines, use this code:
EDIT:
Ok, so the above code isn't exactly correct. First of all, its most likely a Click event instead of a OnClick event. Second, To get the mouse position, you need two variables declared up with the points array,
Then have a mouse move event:
Then, in your Click event: