Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
431 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...