How do I double buffer a Panel?

T-Fox picture T-Fox · May 4, 2009 · Viewed 42.5k times · Source

I have a panel that has a roulette wheel on it, and I need to double buffer the panel, so that it stops flickering. Can anyone help me out?

EDIT:

Yes, I have tried that.

panel1.doublebuffered does not exist, only this.doublebuffered. And I don't need to buffer the Form, just the Panel.

Answer

user79755 picture user79755 · May 4, 2009

You need to derive from Panel or PictureBox.

There are ramifications to this depending on how you choose to enable the buffering.

If you set the this.DoubleBuffer flag then you should be ok.

If you manually update the styles then you have to paint the form yourself in WM_PAINT.

If you really feel ambitious you can maintain and draw your own back buffer as a Bitmap.


using System.Windows.Forms;

public class MyDisplay : Panel
{
    public MyDisplay()
    {
        this.DoubleBuffered = true;

        // or

        SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        UpdateStyles();
    }
}