本文结合实例介绍C#双缓冲的实现方法。 分享给大家,供大家参考,如下:
// 该调用是 Windows.Forms 窗体设计器所必需的。 InitializeComponent(); // TODO: 在 InitComponent 调用后添加任何初始化 this.SetStyle(ControlStyles.AllPaintingInWmPaint,true); //开启双缓冲 this.SetStyle(ControlStyles.DoubleBuffer,true); this.SetStyle(ControlStyles.UserPaint,true); this.SetStyle(ControlStyles.ResizeRedraw,true);
1.在内存中创建一个“虚拟画布”:
Bitmap bmp = new Bitmap(600, 600);
2.获取这个内存画布的引用:
Graphics g = Graphics.FromImage(bmp);
3. 在此记忆画布上绘制:
g.FillEllipse(brush, i * 10, j * 10, 10, 10);
4.将内存画布绘制到窗口
this.CreateGraphics().DrawImage(bmp, 0, 0);
还有其他方法
在构造函数中添加以下代码
代码一:
SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景. SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
代码二:
this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true); this.UpdateStyles();
对C#相关内容比较感兴趣的读者可以查看本站专题:《C#面向对象编程入门教程》、《C#常用控件使用教程》和《C#数据结构与算法教程》
希望这篇文章对大家的C#编程有所帮助。