简体   繁体   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)
            {

            }
        }

I added a new checkBox2 and in the paint event of checkBox2 i want to make that if its checked if i just move the mouse over the pictureBox1 area without clicking anything it will draw a line on pictureBox2 a route of where the mouse is moving in pictureBox1. 我添加了一个新的checkBox2,并且在checkBox2的绘画事件中,我想确定是否检查是否将鼠标移到pictureBox1区域上而不单击任何东西,它将在pictureBox2上画一条线,表示鼠标在pictureBox1中移动的路线。

And ever X pixels i move the mouse it will create a point in Green color on this line. 每当我移动鼠标的X像素时,它将在此行上创建一个绿色的点。

You can use a GraphicsPath to save all the points mouse is over, that's way the drawing can be better than using some List or Array to store the points (and loop through that drawing everytime paint is raised): 您可以使用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);
   }
}

Note that you should add using System.Drawing.Drawing2D; 注意,您应该using System.Drawing.Drawing2D;添加using System.Drawing.Drawing2D; before using the code. 在使用代码之前。

暂无
暂无

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

相关问题 移动trackBar1时,如何保持在图片盒1中的图像上绘制的点? - How can i keep the points im drawing on the image in the picturebox1 when moving the trackBar1? 图片从picturebox1到picturebox2 - image from picturebox1 to picturebox2 如果用户处于鼠标按下事件中,如何检查pictureBox1鼠标离开事件? - How can i check on pictureBox1 mouse leave event if the user is in mouse down event? 如何将pictureBox1中的图像替换为pictureBox1矩形区域上绘制的裁剪图像? - How can i replace the image in pictureBox1 with a crop image of a drawn on the pictureBox1 rectangle area? 如何在鼠标移动事件中实时在pictureBox1中的图像上绘制红色像素? - How to draw pixels in color red on the image in pictureBox1 in real time on mouse move event? 如何使计时器向前或向后更改pictureBox1中显示的图像? - How can I make timer that will change the images display in pictureBox1 forward or backward? 如何使用factor变量使位图适合pictureBox1? - How can I use the factor variable to make the bitmap to fit in the pictureBox1? 我有两个其他尺寸的pictureBox,当我单击一个pictureBox时,如何在较小的pictureBox上绘制一个点,该怎么做? - I have two pictureBoxes in other sizes how can i make that when i click on one pictureBox it will draw a point on the smaller pictureBox? C#如何使用鼠标移动图片框 - C# How can i move a picturebox with mouse 如何检查button1是否不会移出pictureBox1边界/边框? - How do i check that the button1 will not move out of the pictureBox1 bounds/borders?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM