C#..菜鸟问题,GDI+画直线,请进〉>>>
要求,在窗体中用鼠标来确定直线的起点和终点,当鼠标第一次单击窗体中的某个位置时,该位置为起点位置,第二次单击时为终点位置,要代码,谢谢!...
要求,在窗体中用鼠标来确定直线的起点和终点,当鼠标第一次单击窗体中的某个位置时,该位置为起点位置,第二次单击时为终点位置,要代码,谢谢!
展开
展开全部
其实总共就两个点,一个鼠标按下,一个是鼠标离开 ;
int startX; //获取鼠标起始点的X坐标
int startY; //获取鼠标起始点的Y坐标
Graphics g; //定义Graphics对象实例
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
g = this.CreateGraphics();
Pen p = new Pen(Color.Red, 4);
g.DrawLine(p, startX, startY, e.X, e.Y);
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
startX = e.X;
startY = e.Y;
}
int startX; //获取鼠标起始点的X坐标
int startY; //获取鼠标起始点的Y坐标
Graphics g; //定义Graphics对象实例
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
g = this.CreateGraphics();
Pen p = new Pen(Color.Red, 4);
g.DrawLine(p, startX, startY, e.X, e.Y);
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
startX = e.X;
startY = e.Y;
}
展开全部
这么写就行了,不过建议你在PictureBox上画,不要直接画在窗体上
bool draw = false;
Point start = Point.Empty;
Point end = Point.Empty;
private void Form1_Paint(object sender, PaintEventArgs e)
{
if (start != Point.Empty && end != Point.Empty)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.DrawLine(Pens.Black, start, end);
}
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (start != Point.Empty)
{
if (!draw)
{
start = e.Location;
end = Point.Empty;
draw = true;
}
else
{
end = e.Location;
draw = false;
}
this.Invalidate();
}
else
{
start = e.Location;
draw = true;
}
}
else//右键清除画的线
{
start = end = Point.Empty;
this.Invalidate();
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (draw && start != Point.Empty)
{
end = e.Location;
this.Invalidate();
}
}
bool draw = false;
Point start = Point.Empty;
Point end = Point.Empty;
private void Form1_Paint(object sender, PaintEventArgs e)
{
if (start != Point.Empty && end != Point.Empty)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.DrawLine(Pens.Black, start, end);
}
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (start != Point.Empty)
{
if (!draw)
{
start = e.Location;
end = Point.Empty;
draw = true;
}
else
{
end = e.Location;
draw = false;
}
this.Invalidate();
}
else
{
start = e.Location;
draw = true;
}
}
else//右键清除画的线
{
start = end = Point.Empty;
this.Invalidate();
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (draw && start != Point.Empty)
{
end = e.Location;
this.Invalidate();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
楼上的已经给出答案了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
1L+1
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询