private List<Line> listLine = new List<Line>();
private void Draw_MouseUp(object sender, MouseEventArgs e)
{
if (drawingLine == null)
{
return;
}
drawingLine.EndPoint = e.Location;
drawingLine = null;
} private void Draw_MouseMove(object sender, MouseEventArgs e)
{
if (drawingLine != null)
{
drawingLine.EndPoint = e.Location;
this.splitContainer1.Panel2.Refresh();
}
} void drawPanel_Paint(object sender, PaintEventArgs e)
{
Bitmap bp = new Bitmap(this.splitContainer1.Panel2.Width, this.splitContainer1.Panel2.Height);
// 用于缓冲输出的位图对象
Graphics g = Graphics.FromImage(bp);
// 消锯齿(可选项)
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.SmoothingMode = SmoothingMode.HighQuality; //高质量
g.PixelOffsetMode = PixelOffsetMode.HighQuality; //高像素偏移质量
this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
Pen p = new Pen(Color.Black);
foreach (Line line in listLine)
{
if (line == drawingLine)
{
// 当前绘制的线条是正在鼠标定位的线条
p.Color = Color.Blue;
}
else
{
p.Color = Color.Black;
}
g.DrawLine(p, line.StartPoint, line.EndPoint);
}
e.Graphics.DrawImage(bp, Point.Empty);
g.Dispose();
} private void Draw_MouseDown(object sender, MouseEventArgs e)
{
drawingLine = new Line(e.Location);
listLine.Add(drawingLine);
}
//其中drawingLine实例对应的类是Line,Line代码如下:
public class Line
{
public Point StartPoint = Point.Empty;
public Point EndPoint = Point.Empty; public Line() { } /// ﹤summary﹥
/// 建立线条对象,并设置起点
/// ﹤/summary﹥
/// ﹤param name="startPoint"﹥此线条的起点﹤/param﹥
public Line(Point startPoint)
{
StartPoint = startPoint;
EndPoint = startPoint;
}
}
附加一个小问题 http://topic.csdn.net/u/20100915/20/cc30bc01-9a2c-4223-a10a-457e397591db.html两天了都没有人回答,郁闷啊...
DoubleBuffered = true;
试试