我想在图片框上画一条连续的线,还要在光标处画一个十字。如何仅在鼠标位置绘制光标(并在下一步移动时将其清除)并保留该行?
问问题
895 次
1 回答
1
这只是为了让您了解如何解决这个问题......
将 PictureBox 添加到窗体。运行它并在 PictureBox 中单击并拖动:
public partial class Form1 : Form
{
Point cursor;
Point lastPoint;
System.Drawing.Drawing2D.GraphicsPath curPath = null;
private List<System.Drawing.Drawing2D.GraphicsPath> paths = new List<System.Drawing.Drawing2D.GraphicsPath>();
public Form1()
{
InitializeComponent();
pictureBox1.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
pictureBox1.MouseMove += new MouseEventHandler(pictureBox1_MouseMove);
pictureBox1.Paint += new PaintEventHandler(pictureBox1_Paint);
}
void pictureBox1_Paint(object sender, PaintEventArgs e)
{
foreach (System.Drawing.Drawing2D.GraphicsPath gp in paths)
{
e.Graphics.DrawPath(Pens.Red, gp);
}
e.Graphics.DrawLine(Pens.Black, new Point(cursor.X, 0), new Point(cursor.X, pictureBox1.Height));
e.Graphics.DrawLine(Pens.Black, new Point(0, cursor.Y), new Point(pictureBox1.Width, cursor.Y));
}
void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
lastPoint = new Point(e.X, e.Y);
curPath = new System.Drawing.Drawing2D.GraphicsPath();
paths.Add(curPath);
}
}
void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
cursor = new Point(e.X, e.Y);
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
Point newPoint = new Point(e.X, e.Y);
curPath.AddLine(lastPoint, newPoint);
lastPoint = newPoint;
pictureBox1.Refresh();
}
pictureBox1.Refresh();
}
}
于 2013-06-11T18:36:04.367 回答