Winforms 双缓冲 [英] Winforms Double Buffering

查看:30
本文介绍了Winforms 双缓冲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将此添加到表单的构造函数代码中:

I added this to my form's constructor code:

this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);

但它在加载控件时,无论何时更改(表单及其组件经常更改(需要更新))仍会显示难看的工件.

But it still shows ugly artifacts when it loads the controls, whenever they change (the form and its components change (need updating) often).

我需要做什么不同的事情?

What do I need to do differently?

推荐答案

这只对表单本身有影响,对子控件没有影响.如果你有很多他们,那么他们需要轮流给自己画画的时间就会变得很明显,它会留下一个矩形孔,在那里控件去的地方不会被填满,直到子控件开始转动.

This only has an effect on the form itself, not the child controls. If you have a lot of them then the time they need to take turns painting themselves becomes noticeable, it leaves a rectangular hole where the control goes that doesn't get filled up until the child control gets it turn.

您需要做的是双缓冲整个表单控件.这是自 Windows XP 以来可用的一个选项,它使 WS_EX_COMPOSITED 样式标志可用.将此粘贴到您的表单中以将其打开:

What you'd need to combat this is double-buffering the entire form and the controls. That's an option available since Windows XP which made the WS_EX_COMPOSITED style flag available. Paste this into your form to turn it on:

protected override CreateParams CreateParams {
  get {
    CreateParams cp = base.CreateParams;
    cp.ExStyle |= 0x02000000;  // Turn on WS_EX_COMPOSITED
    return cp;
  }
}

它根本不会加快绘画速度,但表单会在延迟后捕捉到屏幕上.消除可见的油漆伪影.真正修复延迟不需要使用控件.您可以通过使用 OnPaint 方法绘制控件"并使 OnMouseClick 事件智能地了解用户单击了什么控件"来实现.

It doesn't speed up the painting at all, but the form snaps onto the screen after a delay. Eliminating the visible paint artifacts. Really fixing the delay requires not using controls. Which you'd do by using the OnPaint method to draw the 'controls' and making the OnMouseClick event smart about what 'control' got clicked by the user.

这篇关于Winforms 双缓冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆