[.net] OptimizedDoubleBuffering VS custom doublebuffering

Started by
0 comments, last by Darrn 17 years, 8 months ago
hi! im writing a C# application and want to use double buffering to avoid flicker. i read the example at MSDN, but what confuses me is that it seems to use triple buffering. i reduced the example to this snippet:

using System;
using System.Drawing;
using System.Windows.Forms;

public class DoubleBufferTest : Form
{
    BufferedGraphicsContext context;
    BufferedGraphics backbuffer;

    public DoubleBufferTest()
    {
        this.Resize += new EventHandler( this.OnResize);

        // NOTE: no Controlstyles.OptimizedDoubleBuffer here!
        this.Setstyle( Controlstyles.AllPaintingInWmPaint | Controlstyles.UserPaint, true );
        
        // NOTE: remove comment to make flicker stop,
        // but it seems to use triple buffering then
        //this.Setstyle( Controlstyles.OptimizedDoubleBuffer | Controlstyles.AllPaintingInWmPaint | Controlstyles.UserPaint, true );

        context = BufferedGraphicsManager.Current;
        context.MaximumBuffer = new Size( this.Width + 1, this.Height + 1);

        backbuffer = context.Allocate( this.CreateGraphics(), new Rectangle( 0, 0, this.Width, this.Height));

        DrawToBuffer( backbuffer.Graphics);
    }

    private void OnResize( object sender, EventArgs e)
    {
        if ( backbuffer != null )
        {
            backbuffer.Dispose();
        }

        context.MaximumBuffer = new Size( this.Width + 1, this.Height + 1);
        backbuffer = context.Allocate( this.CreateGraphics(), new Rectangle( 0, 0, this.Width, this.Height));

        DrawToBuffer( backbuffer.Graphics);
        this.Refresh();
    }

    private void DrawToBuffer( Graphics g)
    {
        g.Clear( Color.Black);
        g.DrawString( "Resize me to make me flicker!", new Font( "Arial", 8), Brushes.White, 10, 10);
    }

    protected override void OnPaint( PaintEventArgs e)
    {
        // NOTE: Graphic backbuffer.Graphics and Graphic e.Graphics are two different Graphics objects,
        // so double buffering seems to be already implemented
        backbuffer.Render( e.Graphics);
    }

    [STAThread]
    public static void Main( string[] args)
    {
        Application.Run( new DoubleBufferTest());
    }
}
all this stuff with "BufferedGraphics" is used to implement "custom doublebuffering". corresponding to MSDN:
Quote:MSDN Remarks The BufferedGraphics class allows you to implement custom double buffering for your graphics. ...
but when you resize it, it still flickers... (why?) i could use "OptimizedDoubleBuffering" but what do i need "custom doublebuffering" for then? if i just override the onPaint method i could save the "custom doublebuffering" and it still doesn't flicker. if i use "custom doubebuffering" and "OptimizedDoubleBuffer" it seems to use triple buffering. 1 = backbuffer.Graphics ("custom doublebuffering") 2 = e.Graphics 3 = The "OptimizedDoubleBuffer" could please someone explain me what is wrong about my consideration!
Advertisement
i solved it:

this.Setstyle( Controlstyles.AllPaintingInWmPaint | Controlstyles.UserPaint | Controlstyles.Opaque, true );

if Opaque is omitted the control calls the PaintBackground method which causes the flicker.

thanks to: Jeremy Kuhne's Blog

This topic is closed to new replies.

Advertisement