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

发布于 2024-09-28 08:01:16 字数 584 浏览 4 评论 0原文

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

 //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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南城旧梦 2024-10-05 08:01:16

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

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

Ok here is some sample code:

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);
}
纵山崖 2024-10-05 08:01:16

首先,添加以下代码:

List<Point> points = new List<Point>();

在您正在绘制的对象上,捕获 OnClick 事件。参数之一应包含单击的 X 和 Y 坐标。将它们添加到点数组中:

points.Add(new Point(xPos, yPos));

最后,在绘制线条的地方,使用以下代码:

 if (DrawShape == 4)
 {
     Graphics G = this.CreateGraphics();
     G.DrawPolygon(Pens.Black, points.ToArray());
 }

编辑:

好的,所以上面的代码并不完全正确。首先,它很可能是 Click 事件而不是 OnClick 事件。其次,要获取鼠标位置,您需要使用点数组声明两个变量,

    int x = 0, y = 0;

然后有一个鼠标移动事件:

    private void MouseMove(object sender, MouseEventArgs e)
    {
        x = e.X;
        y = e.Y;
    }

然后,在您的 Click 事件中:

    private void Click(object sender, EventArgs e)
    {
        points.Add(new Point(x, y));
    }

First, add this code:

List<Point> points = new List<Point>();

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:

points.Add(new Point(xPos, yPos));

And then finally, where you're drawing the lines, use this code:

 if (DrawShape == 4)
 {
     Graphics G = this.CreateGraphics();
     G.DrawPolygon(Pens.Black, points.ToArray());
 }

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,

    int x = 0, y = 0;

Then have a mouse move event:

    private void MouseMove(object sender, MouseEventArgs e)
    {
        x = e.X;
        y = e.Y;
    }

Then, in your Click event:

    private void Click(object sender, EventArgs e)
    {
        points.Add(new Point(x, y));
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文