主场迎战的SetStyle压倒一切CreateParam:通过双缓冲减少闪烁 [英] To reduce flicker by double buffer: SetStyle vs. overriding CreateParam

查看:329
本文介绍了主场迎战的SetStyle压倒一切CreateParam:通过双缓冲减少闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能解释

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

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



他们被要求减少闪烁,但何时以及如何正确地使用它们?可它们可以单独使用,或者必须成对使用,以及什么是对什么原因呢?

They are required to reduce flickers, but when and how to use them correctly? Can they be used individually, or must be used in pairs, and what's the reason for that?

谢谢!

积分

第一个代码段是从的 MSDN页面;第二个代码段在如何解决在用户控件闪烁发现,原作者是@HansPassant。

The first code snippet was cited from MSDN page; the second code snippet was found on How to fix the flickering in User controls, the original author is @HansPassant.

推荐答案

感谢@terrybozzlo的解释和@Caramiriel的伟大页澄清这个问题。

Thanks to @terrybozzlo for explanation and @Caramiriel for the great page that clarifies the problem.

我想总结一下我的一切在这里。

I would like to summarize all I got here.

闪烁,通常会出现在你的窗体或容器控件,如面板,包含了太多的控制(当 WS_CLIPCHILDREN 开启,这是默认的情况下)。据@HansPassant:

Flickers usually occur when your form, or a container control, such as a Panel, contains too many controls (and when WS_CLIPCHILDREN is turned on, which is the case by default). According to @HansPassant:

它绘制和backgroundImage,留洞那里的孩子控制窗口去。然后,每个子控件获得的消息油漆本身,他们将填补其窗口内容的洞。当你有很多的控制,这些孔给用户一段时间可见。他们通常是白色的,用的BackgroundImage对比不好当它是黑暗的。或者,他们可以是黑色,如果窗体上有其不透明度或TransparencyKey属性集,以公正的东西对立得很厉害。

It draws the BackgroundImage, leaving holes where the child control windows go. Each child control then gets a message to paint itself, they'll fill in the hole with their window content. When you have a lot of controls, those holes are visible to the user for a while. They are normally white, contrasting badly with the BackgroundImage when it is dark. Or they can be black if the form has its Opacity or TransparencyKey property set, contrasting badly with just about anything.

您应该控制的 DoubleBuffered 属性设置为真正。要做到这一点,你需要从基本类型派生的控制(如果它不是一个用户控件),并将其设置在构造函数中。

How to avoid them on Control Level

You should set the Control's DoubleBuffered property to true. To do this, you need to derive the control (if it's not a user control) from the basic type and set it in the constructor.

例如,要获得面板双缓冲,你需要做的:

For example, to get a Panel double buffered, you need to do:

public class BufferedPanel : Panel
{
    public BufferedPanel()
    {
        DoubleBuffered = true;
    }
}



另外,你可以使用:

Alternatively, you can use:

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



获得的相同的效果,即它们是等价的

以上技术将减少对控制闪烁的水平,这意味着当表单得到重绘,所有控件都不会忽悠了。但最终的解决方案是从形式层面减少闪烁:当表单得到重绘,形式和它所有的孩子都是双缓冲

The above technique will reduce the flicker on control level, which means when the form get redrawn, all controls won't flicker any more. But the ultimate solution is to reduce flicker from the form level: when the form get redrawn, the form and all its children are double buffered.

这需要重写的CreateParams

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



摘要



的SetStyle 确实对控制水平的工作, CreateParam 表格水平,并实现了双缓冲。所有控制表单内

Summary

SetStyle does the job on control level, and CreateParam on Form level, and achieves double buffer to all control inside the form.

@terrybozzlo,@Caramiriel,@HansPassant

@terrybozzlo, @Caramiriel, @HansPassant

这篇关于主场迎战的SetStyle压倒一切CreateParam:通过双缓冲减少闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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