2个回答
展开全部
1、将窗体的DoubleBuffered属性设置为true;
2、添加以下变量到窗体:
private bool notStart=true;
private Point tp,sp,ep;
private List<Point> ll=new List<Point>();
private List<List<Point>> l=new List<List<Point>>();
3、依次为窗体的MouseDown、MouseMove、Paint、MouseDoubleClick事件添加以下处理函数:
void MainFormMouseDown(object sender, MouseEventArgs e)
{
tp=new Point(e.X,e.Y);
ll.Add(tp);
if(MouseButtons.Left==e.Button)
{
sp=tp;
if(notStart)
{
l.Add(ll);
notStart=false;
}
}
else
{
if(!notStart)
{
ll=new List<Point>();
notStart=true;
}
}
Invalidate();
}
void MainFormMouseMove(object sender, MouseEventArgs e)
{
ep=new Point(e.X,e.Y);
Invalidate();
}
void MainFormPaint(object sender, PaintEventArgs e)
{
Point p;
Graphics g=e.Graphics;
for(int i=0;i<l.Count;i++)
{
p=l[i][0];
for(int j=1;j<l[i].Count;j++)
{
g.DrawLine(Pens.Red,p,l[i][j]);
p=l[i][j];
}
}
if(!notStart)
{
g.DrawLine(Pens.Red,sp,ep);
}
}
void MainFormMouseDoubleClick(object sender, MouseEventArgs e)
{
if(MouseButtons.Left==e.Button)
{
l.Clear();
ll.Clear();
notStart=true;
Invalidate();
}
}
2、添加以下变量到窗体:
private bool notStart=true;
private Point tp,sp,ep;
private List<Point> ll=new List<Point>();
private List<List<Point>> l=new List<List<Point>>();
3、依次为窗体的MouseDown、MouseMove、Paint、MouseDoubleClick事件添加以下处理函数:
void MainFormMouseDown(object sender, MouseEventArgs e)
{
tp=new Point(e.X,e.Y);
ll.Add(tp);
if(MouseButtons.Left==e.Button)
{
sp=tp;
if(notStart)
{
l.Add(ll);
notStart=false;
}
}
else
{
if(!notStart)
{
ll=new List<Point>();
notStart=true;
}
}
Invalidate();
}
void MainFormMouseMove(object sender, MouseEventArgs e)
{
ep=new Point(e.X,e.Y);
Invalidate();
}
void MainFormPaint(object sender, PaintEventArgs e)
{
Point p;
Graphics g=e.Graphics;
for(int i=0;i<l.Count;i++)
{
p=l[i][0];
for(int j=1;j<l[i].Count;j++)
{
g.DrawLine(Pens.Red,p,l[i][j]);
p=l[i][j];
}
}
if(!notStart)
{
g.DrawLine(Pens.Red,sp,ep);
}
}
void MainFormMouseDoubleClick(object sender, MouseEventArgs e)
{
if(MouseButtons.Left==e.Button)
{
l.Clear();
ll.Clear();
notStart=true;
Invalidate();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询