WinForm内移动、放大缩小控件出现闪屏问题的解决方法

来源:互联网 发布:制版软件 编辑:程序博客网 时间:2024/06/05 16:26
WinForm 对图像处理本身就不太友好,放大或缩小会刷新界面控件,当然有闪烁。

1、不使用图片作为背景,而用纯色替代,如果是这样的话,也就不会出现闪屏了,但如果UI上有特别要求的,这条路就行不通了。
2、重写Panel,然后在Panel中添加背景图片,而不是把背景图片添加到Form中,重写的Panel代码如下:

View Code 1 /// <summary>
2 /// 一个Panel类,当设置背景图片时,控制其不会闪屏
3 /// </summary>
4 public class BackgroundPanel : Panel
5 {
6 protected override void OnPaintBackground(PaintEventArgs e)
7 {
8 return;
9 }
10
11 protected override void OnPaint(PaintEventArgs e)
12 {
13
14 this.DoubleBuffered = true;
15 if (this.BackgroundImage != null)
16 {
17 e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
18 e.Graphics.DrawImage(this.BackgroundImage, new System.Drawing.Rectangle(0, 0, this.Width, this.Height),
19 0, 0, this.BackgroundImage.Width, this.BackgroundImage.Height,
20 System.Drawing.GraphicsUnit.Pixel);
21 }
22 base.OnPaint(e);
23 }
24 }
3、使用Form的双缓存可以减少闪屏,但效果不明显,可以在Form的Load事件里添加以下代码

View Code1 this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
2 this.SetStyle(ControlStyles.DoubleBuffer, true);
3 this.SetStyle(ControlStyles.UserPaint, true);
4 this.SetStyle(ControlStyles.ResizeRedraw, true);
阅读全文
0 0
原创粉丝点击