【发布时间】:2019-08-02 17:01:18
【问题描述】:
您好,我想使用鼠标事件在 winforms 中的图片框中绘制一条直线。我使用鼠标按下、鼠标移动和鼠标向上事件来绘制一条线。但是,当我移动鼠标时,还会绘制其他几条线。
如果有人可以提供有关如何解决此问题的指南,我将不胜感激。
我还附上了我的代码的 sn-ps 供您参考。提前谢谢!
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
mousepress = true;
x1 = e.Location.X;
y1= e.Location.Y;
if (counter>0)
{
this.Invalidate();
pictureBox1.Refresh();
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
Graphics g = pictureBox1.CreateGraphics();
Pen newpen = new Pen(Color.Blue, 1);
if (mousepress)
{
g.DrawLine(newpen, x1, y1, e.Location.X, e.Location.Y);
x2 = e.Location.X;
y2 = e.Location.Y;
angle = GetAngle(x1, y1, x2, y2);
}
Invalidate();
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
mousepress = false;
double tempX=e.Location.X, tempY=e.Location.Y;
{
textBox_coordinates.Text = "Index: " + i + Environment.NewLine + "X: " + x2
+ Environment.NewLine + "Y: " + y2 + Environment.NewLine + "Angle: " + angle;
i++;
}
counter++;
}
【问题讨论】:
-
您可以将线条信息存储在一个结构中,并在重新绘制图像后绘制它stackoverflow.com/questions/20876476/…