Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Winforms Double Buffering

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?

like image 541
Xenoprimate Avatar asked Sep 15 '10 14:09

Xenoprimate


People also ask

How do I turn on double buffering?

You can enable default double buffering in your forms and authored controls in two ways. You can either set the DoubleBuffered property to true , or you can call the SetStyle method to set the OptimizedDoubleBuffer flag to true .

What does double buffering do?

Double buffering is a term used to describe a device with two buffers. The usage of multiple buffers increases the overall throughput of a device and helps prevents bottlenecks. For example, with graphics, double buffering can show one image or frame while a separate frame is being buffered to be shown next.

What is the advantage of a double buffered graphics application?

Well, the main advantages of double-buffering are: The user does not see every pixel modification (so if the same pixel gets modified 5 times, the user will only see the last one). This helps preventing 'flickering' (or screen flashes).

What is single and double buffering?

Single buffer: when data is stored in a section of the system memory. Double buffer: allows for two buffers to be used. Circular buffering: uses a priority-based queue for when more than two buffers are needed.


1 Answers

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.

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

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.

like image 194
Hans Passant Avatar answered Oct 15 '22 00:10

Hans Passant