繁体   English   中英

在C# windows窗体中用鼠标单击两次在图片框中画线

[英]Draw line in picture box with two mouse clicks in C# windows form

我需要帮助在图片框上画线。

我在鼠标单击时获得了 point2 和 point2 的坐标,并在这两个坐标之间画了一条线。

我需要做的是取点 1 的值并从该点 1 开始绘制线,线应该跟随我的光标,当我第二次单击图片框时,它应该被绘制。

任何帮助,将不胜感激。

这是我到目前为止

// Points p1(x1,y1), p2(x2,y2)
        private Point p1, p2;
        List<Point> p1List = new List<Point>();
        List<Point> p2List = new List<Point>();

        int count = 0;
        // Click 2 times on image to get coordinate values for p1 & p2
        private void pbChooseDirection_MouseDown(object sender, MouseEventArgs e)
        {

            if(count/2 == 0)
            {
                pbChooseDirection.Image = drawingLineImage;
            }

            if (p1.X == 0)
            {
                p1.X = e.X;
                p1.Y = e.Y;
                count++;
            }
            else
            {
                p2.X = e.X;
                p2.Y = e.Y;
                count++;

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

                Invalidate();
                // Sets X to 0 and choose p1 again
                p1.X = 0;
            }

        }

private void pbChooseDirection_Paint_1(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]);
                }
            }
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM