To reduce flicker by double buffer: SetStyle vs. overriding CreateParam

做~自己de王妃 提交于 2020-01-12 07:16:57

问题


Can anybody explain the difference and relationship between

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

and

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?

Thanks!

Credits:

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.


回答1:


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

I would like to summarize all I got here.


Why we got flickers

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:

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.

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);

to obtain the identical effect, i.e. they are equivalent.

How to avoid them on Form Level

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.

This requires overriding CreateParams:

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

Summary

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

Credits:

@terrybozzlo, @Caramiriel, @HansPassant



来源:https://stackoverflow.com/questions/25872849/to-reduce-flicker-by-double-buffer-setstyle-vs-overriding-createparam

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!