这篇文章主要介绍了C#双缓冲实现方法,结合实例形式分析了C#双缓冲的具体步骤与相关技巧,可实现防止闪屏的功能,需要的朋友可以参考下
本文实例讲述了C#双缓冲实现方法。分享给大家供大家参考,具体如下:
1
2
3
4
5
6
7
8
|
// 该调用是 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、在内存中建立一块“虚拟画布”:
1
|
Bitmap bmp =
new
Bitmap(600, 600);
|
2、获取这块内存画布的Graphics引用:
1
|
Graphics g = Graphics.FromImage(bmp);
|
3、在这块内存画布上绘图:
1
|
g.FillEllipse(brush, i * 10, j * 10, 10, 10);
|
4、将内存画布画到窗口中
1
|
this
.CreateGraphics().DrawImage(bmp, 0, 0);
|
还有的方式
在构造函数中加如下代码
代码一:
1
2
3
|
SetStyle(ControlStyles.UserPaint,
true
);
SetStyle(ControlStyles.AllPaintingInWmPaint,
true
);
// 禁止擦除背景.
SetStyle(ControlStyles.DoubleBuffer,
true
);
// 双缓冲
|
代码二:
1
2
|
this
.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint,
true
);
this
.UpdateStyles();
|