如何像Windows画图一样绘制一条直线,单击以固定第一个点,第二个点(和线)随鼠标移动,再次单击以固定直线。
int x = 0, y = 0;
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
// Create the graphics object
Graphics g = CreateGraphics();
// Create the pen that will draw the line
Pen p = new Pen(Color.Navy);
// Create the pen that will erase the line
Pen erase = new Pen(Color.White);
g.DrawLine(erase, 0, 0, x, y);
// Save the mouse coordinates
x = e.X; y = e.Y;
g.DrawLine(p, 0, 0, x, y);
}
点击事件部分没问题,但使用上述方法擦除线条实际上是白色线条,这会重叠在其他背景图像和之前绘制的蓝色线条上。
有没有更易于管理的方法来实现它?谢谢