www.codeguru.com/cpp/misc/misc/graphics/article.php/c15709/

Back to Article

Home >> Visual C++ / C++ >> Miscellaneous >> Miscellaneous >> Graphics


Using Windows Vista Built-In Double Buffering
Buffered Painting in Windows Vista
Rating: none

Marc Gregoire (view profile)
December 1, 2008

Environment:  Vista

Go to page: Prev  1  2  

The fourth parameter to BeginBufferedPaint is a pointer to a BP_PAINTPARAMS structure. This can be NULL if you don't need it. Because the buffered painting technique will keep off-screen buffers alive and will reuse them in future buffered painting calls, it is important to remember that the contents of the off-screen buffer is not erased by default. So, either you fill the off-screen buffer yourself with a certain color to erase the previous drawing (as I did in my example above), or you can use the BP_PAINTPARAMS structure to force BeginBufferedPaint to clear the off-screen buffer with ARGB = {0,0,0,0}. A third way is to use the function BufferedPaintClear; this will clear a specific rectangle on the off-screen buffer to ARGB={0,0,0,0}. This function is faster than using GDI because it will access the bits directly, but you cannot specify a color. You also can change clipping behaviour on the off-screen buffer and specify blending parameters. The blending parameters are used to control blending operations when EndBufferedPaint copies the contents of the off-screen buffer to the destination.


(continued)




When you would run the code above, you'll notice there is still some flickering. This is because Windows is still erasing the background before painting. Because you are rendering the entire window off-screen and EndBufferedPaint will copy the off-screen buffer in one go, you can bypass the erasing of the background. This can be done by handling WM_ERASEBKGND as follows:

case WM_ERASEBKGND:
   return 1;

There are two more functions that you might find useful with buffered painting. BufferedPaintSetAlpha sets all alpha values in the buffer to a certain value, 0-255, where 0 is fully transparent and 255 is fully opaque. This alpha value is used to control the amount of transparency when the off-screen buffer is copied to the destination. To set all alpha values to 255, you also can use the BufferedPaintMakeOpaque macro.

You can use GetBufferedPaintBits to get direct access to the bits of the off-screen buffer. This only works when the off-screen buffer is a device independent buffer (DIB).

The example that comes with this article shows the effect of using double buffering. Use the "Mode" menu to switch between buffered and unbuffered painting. To see the effect, resize the window and see how the window gets updated. Because buffered painting is available only since Windows Vista, the example application will not run on Windows XP or lower.

Windows Vista also supports buffered animations, but that's the topic for my next article.

About the Author
Marc graduated from the Catholic University Leuven, Belgium, with a degree in "Burgerlijk ingenieur in de computer wetenschappen" (equivalent to Master of Science in Engineering in Computer Science) in 2003. In 2004 he got the cum laude degree of Master In Artificial Intelligence at the same university. In 2005 he started working for a big software consultancy company. His main expertise is C/C++ and specifically Microsoft VC++ and the MFC framework. Next to C/C++, he also likes C# and uses PHP for creating webpages. Besides his main interest for Windows development, he also has experience in developing C++ programs running 24x7 on Linux platforms and in developing critical 2G,3G software running on Solaris for big telecom operators.

Go to page: Prev  1  2  

Downloads

  • BufferedPaintExample_demo.zip - Demo application
  • BufferedPaintExample_src.zip - Demo application source code

    Tools:
    Add www.codeguru.com to your favorites
    Add www.codeguru.com to your browser search box
    IE 7 | Firefox 2.0 | Firefox 1.5.x
    Receive news via our XML/RSS feed






  • internet.commediabistro.comJusttechjobs.comGraphics.com

    Search:

    WebMediaBrands Corporate Info

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs