HypernovaDesktop Components · docs
Binarymission
WinForms › Double Buffered Control Base › DoubleBufferedControlBase

DoubleBufferedControlBase

class.NET 6 / 8 / 10.NET Framework 4.8Binarymission.WinForms.Controls.BaseControls

A base class for owner-drawn controls that must paint without flicker – derive from it instead of Control and draw in OnPaint as usual.

public abstract class DoubleBufferedControlBase : Control

Inherits from / implements: Control

Remarks

What it is for

A control that draws a lot in OnPaint, or hosts many child controls, tends to flash as each piece reaches the screen. This class switches on the two Windows mechanisms that stop that, so a derived control gets them without having to remember either. It adds no drawing, properties or events of its own.

The two mechanisms

  • OptimizedDoubleBuffer, set in the constructor: the control's own painting goes to an off-screen buffer and the finished picture is copied to the screen in one step.
  • The WS_EX_COMPOSITED extended window style, added in CreateParams: the window and all its descendants are painted bottom-to-top off-screen, so a container full of children does not flash as each child paints in turn. Double buffering alone cannot do that, because it covers only one control's own drawing.
Tip

If a derived class overrides CreateParams, start from base.CreateParams so the composited style is kept. The style is read when the window is created, so it cannot be switched on later.

Examples

using System.Drawing;
using System.Windows.Forms;
using Binarymission.WinForms.Controls.BaseControls;

// A progress gauge that redraws many times a second without flicker.
public class UploadGauge : DoubleBufferedControlBase
{
    private int percentComplete;

    public int PercentComplete
    {
        get { return percentComplete; }
        set { percentComplete = value; Invalidate(); }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        e.Graphics.Clear(BackColor);
        int width = ClientSize.Width * percentComplete / 100;
        e.Graphics.FillRectangle(Brushes.SeaGreen, 0, 0, width, ClientSize.Height);
        TextRenderer.DrawText(e.Graphics, percentComplete + "%", Font, ClientRectangle, ForeColor);
    }
}

Members at a glance

Constructors
DoubleBufferedControlBaseInitialises the control with optimised double buffering switched on.
Properties
CreateParamsGets the parameters used to create the control's window, with the WS_EX_COMPOSITED extended style added so the window and its children are painted off-screen.

Constructors

DoubleBufferedControlBase

protected DoubleBufferedControlBase()

Initialises the control with optimised double buffering switched on.

Properties

CreateParams

protected override CreateParams CreateParams

Gets the parameters used to create the control's window, with the WS_EX_COMPOSITED extended style added so the window and its children are painted off-screen.

Value
The base class's parameters, with WS_EX_COMPOSITED (0x02000000) set in ExStyle.
Remarks

An override should call base.CreateParams and modify the result, so the composited style is kept.