簡體   English   中英

如何在將鼠標移到pictureBox1上時自動在pictureBox2上繪制點?

[英]How can i make that when i move the mouse ober pictureBox1 it will draw points automatic on pictureBox2?

private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                points.Add(new PointF(e.X * xFactor, e.Y * yFactor));
                pictureBox2.Invalidate();
                label5.Visible = true;
                label5.Text = String.Format("X: {0}; Y: {1}", e.X, e.Y);
                counter += 1;
                label6.Visible = true;
                label6.Text = counter.ToString();
            }
        }

         private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        label4.Visible = true;
        label4.Text = String.Format("X: {0}; Y: {1}", e.X, e.Y);
        if (panning)
        {
            movingPoint = new Point(e.Location.X - startingPoint.X,
                                    e.Location.Y - startingPoint.Y);
            pictureBox1.Invalidate();
        }
    }

        private void pictureBox2_Paint(object sender, PaintEventArgs e)
        {
            Pen p;
            p = new Pen(Brushes.Green);
            foreach (PointF pt in points)
            {
                e.Graphics.FillEllipse(Brushes.Red, pt.X, pt.Y, 3f, 3f);
            }

            for (int i = 0; i < points.Count - 1; i++)
            {
                if (points.Count > 1)
                {
                    e.Graphics.DrawLine(p, points[i].X, points[i].Y, points[i+1].X, points[i+1].Y);
                }
            }

            if (checkBox2.Checked)
            {

            }
        }

我添加了一個新的checkBox2,並且在checkBox2的繪畫事件中,我想確定是否檢查是否將鼠標移到pictureBox1區域上而不單擊任何東西,它將在pictureBox2上畫一條線,表示鼠標在pictureBox1中移動的路線。

每當我移動鼠標的X像素時,它將在此行上創建一個綠色的點。

您可以使用GraphicsPath來保存鼠標懸停的所有點,這樣可以比使用某些List或Array存儲點(並在每次繪制塗料時循環遍歷該圖)更好地繪制圖形:

GraphicsPath gp = new GraphicsPath();
Point p;
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    if(!label4.Visible) label4.Visible = true;
    label4.Text = String.Format("X: {0}; Y: {1}", e.X, e.Y);
    if (panning) {
        if(p == Point.Empty) {
          p = e.Location;
          return;
        }
        gp.AddLine(p,e.Location);
        p = e.Location;
        pictureBox2.Invalidate();
    }
}
private void pictureBox2_Paint(object sender, PaintEventArgs e) {
   using(Pen p = new Pen(Color.Green,2f)){
        p.StartCap = p.EndCap = LineCap.Round;
        p.LineJoin = LineJoin.Round;
        e.Graphics.DrawPath(p,gp);
   }
}

注意,您應該using System.Drawing.Drawing2D;添加using System.Drawing.Drawing2D; 在使用代碼之前。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM