[英]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.