欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#双缓冲实现方法(可防止闪屏)

程序员文章站 2023-02-22 19:54:04
本文实例讲述了c#双缓冲实现方法。分享给大家供大家参考,具体如下: // 该调用是 windows.forms 窗体设计器所必需的。 initializ...

本文实例讲述了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引用:

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#程序设计有所帮助。