C#中如何画线?鼠标点击一下,记下第一个点,鼠标抬起时记录第二个点,同时停止画线
2个回答
展开全部
用GDI+
Graphics gra;
Point firstpoint;
Point secondpoint;
private void Form1_Load(object sender, EventArgs e)
{
gra = this.CreateGraphics();
firstpoint = new Point(0, 0);
secondpoint = new Point(0, 0);
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
firstpoint.X = e.X;
firstpoint.Y = e.Y;
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
secondpoint.X = e.X;
secondpoint.Y = e.Y;
gra.DrawLine(Pens.Black, firstpoint, secondpoint);
}
Graphics gra;
Point firstpoint;
Point secondpoint;
private void Form1_Load(object sender, EventArgs e)
{
gra = this.CreateGraphics();
firstpoint = new Point(0, 0);
secondpoint = new Point(0, 0);
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
firstpoint.X = e.X;
firstpoint.Y = e.Y;
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
secondpoint.X = e.X;
secondpoint.Y = e.Y;
gra.DrawLine(Pens.Black, firstpoint, secondpoint);
}
追问
我想知道在画线的过程中,如何随着鼠标的移动而画线,当鼠标放下的时候,线画好了(只是一条线,而不是一大片扫过的部分)。
追答
要那个耀眼的效果啊,麻烦而且累人
再加一个控件,比如说PictureBox,用它的事件和graphic去画,加一个mousemove事件,再弄一个开关,画法与上面一样,最后画好了再清picturebox,画到form上去:
class Line
{
public Line(Point pt1, Point pt2)
{
this.pt1 = pt1;
this.pt2 = pt2;
}
public Point pt1;
public Point pt2;
}
Graphics grapic;
Point firstpoint;
Point secondpoint;
bool moving = false;
List lines = new List();
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
pictureBox1.Refresh();
pictureBox1.BackColor = Color.Transparent;
secondpoint.X = e.X;
secondpoint.Y = e.Y;
lines.Add(new Line(firstpoint, secondpoint));
foreach (Line line in lines)
{
grapic.DrawLine(Pens.Black, line.pt1, line.pt2);
}
moving = false;
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
pictureBox1.Refresh();
foreach (Line line in lines)
{
grapic.DrawLine(Pens.Black, line.pt1, line.pt2);
}
firstpoint.X = e.X;
firstpoint.Y = e.Y;
moving = true;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (moving)
{
pictureBox1.Refresh();
foreach (Line line in lines)
{
grapic.DrawLine(Pens.Black, line.pt1, line.pt2);
}
pictureBox1.BackColor = Color.Transparent;
grapic.DrawLine(Pens.Black, firstpoint, new Point(e.X, e.Y));
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |