gpt4 book ai didi

c# - 在 C# 中使用鼠标点绘制多边形

转载 作者:太空狗 更新时间:2023-10-29 23:03:47 26 4
gpt4 key购买 nike

我需要能够使用鼠标点击位置来绘制多边形。这是我当前的代码:

 //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);
}

谢谢

最佳答案

好的,这是一些示例代码:

private List<Point> polygonPoints = new List<Point>();

private void TestForm_MouseClick(object sender, MouseEventArgs e)
{
switch(e.Button)
{
case MouseButtons.Left:
//draw line
polygonPoints.Add(new Point(e.X, e.Y));
if (polygonPoints.Count > 1)
{
//draw line
this.DrawLine(polygonPoints[polygonPoints.Count - 2], polygonPoints[polygonPoints.Count - 1]);
}
break;

case MouseButtons.Right:
//finish polygon
if (polygonPoints.Count > 2)
{
//draw last line
this.DrawLine(polygonPoints[polygonPoints.Count - 1], polygonPoints[0]);
polygonPoints.Clear();
}
break;
}
}

private void DrawLine(Point p1, Point p2)
{
Graphics G = this.CreateGraphics();
G.DrawLine(Pens.Black, p1, p2);
}

关于c# - 在 C# 中使用鼠标点绘制多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3988105/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com