H o m e

 

D o u b l e    B u f f e r i n g    D r a w i n g    O p e r a t i o n s    

By Michael McIntyre

mikemc@getdotnetcode.com

 

Double buffering drawing operations makes painting to the screen more responsive and eliminates flicker. When double buffering is enabled a second buffer for drawing operations is added. The second buffer prepares the next image while the current one is being painted on the screen.

 

The System.Windows.Forms SetControlStyles method is used to enable double buffering.  The method is used to set a bitwise combination of ControlStyles enumeration values.

 

The best place to set ControlStyles is right after IntializeComponent() call in a form’s Sub New() method. Example code is provided below.

 

#Region " Windows Form Designer generated code "

 

    Public Sub New()

        MyBase.New()

        'This call is required by the Windows Form Designer.

        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

 

        ' Set this form's DoubleBuffer ControlStyles bit to true.

        ' This enables double buffering for the FORM.

        Me.SetStyle(ControlStyles.DoubleBuffer, True)

 

        ' Set this form's AllPaintingInWmPaint ControlStyles bit to true.

        ' This prevents the erase phase of painting operationsw

        ' which is one cause of flicker.

        Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)

 

        ' Set the UserPaint ControlStyles bit to true.

        ' This enables double buffering for form CONTROLS that

        ' are double buffered.

        Me.SetStyle(ControlStyles.UserPaint, True)

    End Sub

 

This code provides a good general example that implements double buffering. See the documentation for the ControlStyles enumeration to learn what other options are available.

 

This code should not be used in memory challenged systems. In a system with memory contraints the code will actually degrade performance.

 

Copyright © 2001-2003 aZ Software Developers. All rights reserved.