簡體   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