单击两个新点并使用鼠标事件在这两点之间绘制一条线

发布于 2024-09-15 21:20:21 字数 154 浏览 4 评论 0原文

有什么建议如何通过单击两个新点然后在它们之间画一条线来创建一条线吗? 我正在尝试创建一个像 adobe acrobat 中的距离工具。

图像示例

alt text

Any suggestions how to create a line by clicking two new points then draw a line between them?
I am trying to create a distance tool like the one in adobe acrobat.

Image Example

alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

瞳孔里扚悲伤 2024-09-22 21:20:21

问题解决了!

编辑:
这是代码:

private Point p1, p2;
List<Point> p1List = new List<Point>();
List<Point> p2List = new List<Point>();

    private void Panel1_MouseDown(object sender, MouseEventArgs e)
    {
        if (p1.X == 0)
        {
            p1.X = e.X;
            p1.Y = e.Y;
        }
        else
        {
            p2.X = e.X;
            p2.Y = e.Y;

            p1List.Add(p1);
            p2List.Add(p2);

            Invalidate();
            p1.X = 0;
        }
    }

    private void Panel1_Paint(object sender, PaintEventArgs e)
    {
        using(var p = new Pen(Color.Blue, 4))
        {
            for(int x = 0; x<p1List.Count; x++){
                e.Graphics.DrawLine(p, p1List[x], p2List[x]);
            }
        }
    }

Problem Solved!

EDIT:
Here's the code:

private Point p1, p2;
List<Point> p1List = new List<Point>();
List<Point> p2List = new List<Point>();

    private void Panel1_MouseDown(object sender, MouseEventArgs e)
    {
        if (p1.X == 0)
        {
            p1.X = e.X;
            p1.Y = e.Y;
        }
        else
        {
            p2.X = e.X;
            p2.Y = e.Y;

            p1List.Add(p1);
            p2List.Add(p2);

            Invalidate();
            p1.X = 0;
        }
    }

    private void Panel1_Paint(object sender, PaintEventArgs e)
    {
        using(var p = new Pen(Color.Blue, 4))
        {
            for(int x = 0; x<p1List.Count; x++){
                e.Graphics.DrawLine(p, p1List[x], p2List[x]);
            }
        }
    }
一抹苦笑 2024-09-22 21:20:21

您可以处理面板上的鼠标单击事件(例如)并检索单击的位置(使用事件参数)。将此位置存储在属性中。根据您的需要,执行此操作以获得尽可能多的积分。
在面板绘制事件中,调用父绘制,然后在点之间绘制线条。

You can handle the mouse click event on the panel (for example) and retrieve the location of the click (using the event args). Store this location in an attribute. Do that for as many points as you need.
In the panel paint event, call the parent paint, then draw the lines between your points.

旧故 2024-09-22 21:20:21

像这样的事情应该做到这一点:

Point firstPoint;
Point seondPoint;

private void panel1_MouseDown(object sender, MouseEventArgs e)
{
    if (this.firstPoint == null) {
        this.firstPoint = e.Location;
    }

    if (this.secondPoint == null) {
        this.secondPoint = e.Location;
    }

    panel1.Invalidate();
}

private void panel1_Paint_1(object sender, PaintEventArgs e)
{       
    Using (pn as new Pen(Color.Blue, 5))
    {
        e.Graphics.DrawLine(pn, firstPoint, secondPoint);
    }
}

编辑:您也不需要执行 CreateGraphics 来绘制线条 - 在 Paint 事件中您已经有了一个图形对象。

Something like this should do it:

Point firstPoint;
Point seondPoint;

private void panel1_MouseDown(object sender, MouseEventArgs e)
{
    if (this.firstPoint == null) {
        this.firstPoint = e.Location;
    }

    if (this.secondPoint == null) {
        this.secondPoint = e.Location;
    }

    panel1.Invalidate();
}

private void panel1_Paint_1(object sender, PaintEventArgs e)
{       
    Using (pn as new Pen(Color.Blue, 5))
    {
        e.Graphics.DrawLine(pn, firstPoint, secondPoint);
    }
}

EDIT: You also dont need to do CreateGraphics to draw the line - in the Paint event you have a graphics object already.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文