Click here to Skip to main content
13,398,199 members (33,676 online)
Click here to Skip to main content
Add your own
alternative version


19 bookmarked
Posted 10 Mar 2002

A Memory DC for WTL

, 10 Mar 2002
Rate this:
Please Sign up or sign in to vote.
An article detailing a simple memory-based device context for the WTL, to be used when flicker-free double-buffered drawing is desired
<!-- Download Links --> <!-- Article image -->

Sample Image - WTLMemDC.gif


A simple implementation of the memory device context for the WTL.


The ATL and the WTL are missing some advanced of the advanced drawing tools that are present in other libraries and frameworks. This article, and the included source code, provides one such advanced tool: a memory device context.


Developers quickly learn that drawing directly on the client area of a window causes flicker. Most professional applications use a technique known as "double buffered drawing". All drawing operations are performed on a plane that is invisible to the user. When the scene has been fully rendered on the hidden plane, its contents are copied to the plane seen by the user in a single pass.

The WTL does not currently support double buffered drawing. However, it is very simple to implement. The header file, "AtlGdi.h" contains a class that implements a Windows Device Context. There are several subclasses of the CDC object for use in a variety of situations. However, there is no "Memory Device Context" included in the WTL. This document describes how to perform double-buffering with the Win32 SDK, and demonstrates a WTL implementation, based on the CDC class.

Double-buffering with the SDK

While it is not necessary to understand how double-buffering is performed in the SDK, it is of interest to see how it works in relation to the CMemDC described by this article.

While the initialization code for the memory DC can be located in the WM_CREATE message handler of the window it will be used with, this does not work when an existing window is subclassed. This is because the window has to be created before it is subclassed, so the WM_CREATE message is never seen by the subclass. Therefore, it is usually desirable to add that code to the WM_PAINT message handler, with a check to see if the initialization has already occurred. An example of creating and using a memory DC:

int WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
  static HDC     hMemDC  = 0;
  static HBITMAP hBitmap = 0;

  int width, height;
  HDC hDC;

  switch( msg )
    case WM_PAINT:
      /* Initialize the memory DC */
      if( ! hMemDC )
        width   = GetSystemMetrics( SM_CXSCREEN );
        height  = GetSystemMetrics( SM_CYSCREEN );
        hDC     = GetDC( hWnd );
        hMemDC  = CreateCompatibleDC( hDC );
        hBitmap = CreateCompatibleBitmap( hDC, width, height );
        SelectBitmap( hMemDC, hBitmap );
        ReleaseDC( hWnd, hDC );

      /* Do all painting using hMemDC */

      /* Display the memory DC */
      BeginPaint( hWnd, &ps );
      BitBlt( ps.hdc,
              ps.rcPaint.right - ps.rcPaint.left,
              ps.rcPaint.bottom -,
              SRCCOPY );
      EndPaint( hWnd, &ps );

    case WM_DESTROY:
      DeleteDC( hMemDC );
      PostQuitMessage( 0 );

Double-buffering with the WTL

The class presented with this article uses virtually the same code, but is based upon the WTL CDC class. The WM_PAINT handler in the application creates an instance of CMemDC with the window handle as an argument:

void OnPaint( HDC )
  if( ! m_memDC )
    m_memDC = new CMemDC( *this );


The CMemDC constructor creates the memory DC and the bitmap internally:

CMemDC( HWND hWnd )
: CDC( )
, m_bitmap( 0 )
, m_hWnd( 0 )
  ATLASSERT( hWnd );
  m_hWnd = hWnd;
  int width  = ::GetSystemMetrics( SM_CXSCREEN );
  int height = ::GetSystemMetrics( SM_CYSCREEN );
  CClientDC dc( hWnd );
  CreateCompatibleDC( dc );
  m_bitmap.CreateCompatibleBitmap( dc, width, height );
  SelectBitmap( m_bitmap );

The application need only reference the memory DC object instead of creating a CPaintDC within the WM_PAINT handler:

void OnPaint( HDC )

  RECT cRect;
  GetClientRect( &cRect );
  m_memDC->FillSolidRect( &cRect, GetSysColor( COLOR_BTNFACE ) );
  m_memDC->DrawEdge( &cRect, EDGE_ETCHED, BF_ADJUST | BF_RECT );
  m_memDC->DrawText( _T( "I'm custom drawn."), 17, &cRect,


Then, when the invisible memory DC has been completely painted, its contents can be copied to the visible window simply by calling its Paint function:

void OnPaint( HDC )

  m_memDC->Paint( );

Also supplied is a Repaint function, which can be used to refresh the visible window when processing other messages.


Although the implementation of the Memory Device Context class is very simple, it may be one of the most useful classes in a programmer's toolbox. Without this class, or something similar, there is no way to draw on the screen without a great deal of flicker.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Paul A. Howes
Software Developer (Senior)
United States United States
Mr. Howes has been programming since he was first introduced to the Apple II+ at the tender age of ten. After briefly dabbling with circuit design as a combined CS/EE major, he came to his senses and completed a pure CS degree at RIT

All these years later, the programming bug still bites him each day and causes him to go to work, where he writes software for a living. Now he is even learning Cocoa for the Mac!

In his copious amounts of "spare" time, Mr. Howes is learning the art of cabinet-making, flies radio-controlled helicopters and airplanes, and mountain cycles. Next summer he would like to learn how to kayak.

Linked In Page:

You may also be interested in...

Comments and Discussions

GeneralSome bug Pin
StoneHead6-Dec-07 19:31
memberStoneHead6-Dec-07 19:31 
GeneralNice really, i wish an update of gdi+ Pin
johnthan4-Nov-02 19:45
memberjohnthan4-Nov-02 19:45 
GeneralRe: Nice really, i wish an update of gdi+ Pin
jandrhub25-Nov-02 2:24
memberjandrhub25-Nov-02 2:24 
GeneralRe: Nice really, i wish an update of gdi+ Pin
Anonymous27-Nov-02 3:33
sussAnonymous27-Nov-02 3:33 
GeneralPossible Resource Leaks Pin
Henry Jacobs12-Mar-02 11:00
memberHenry Jacobs12-Mar-02 11:00 
GeneralBitmap size... Pin
Paul A. Howes12-Mar-02 4:21
memberPaul A. Howes12-Mar-02 4:21 
GeneralRe: Bitmap size... Pin
jandrhub25-Nov-02 3:11
memberjandrhub25-Nov-02 3:11 
GeneralAnother (better?) version already exist Pin
Jean-Michel LE FOL11-Mar-02 22:49
memberJean-Michel LE FOL11-Mar-02 22:49 
GeneralRe: Another (better?) version already exist Pin
Paul A. Howes12-Mar-02 0:50
memberPaul A. Howes12-Mar-02 0:50 
GeneralRe: Another (better?) version already exist Pin
Daniel Bowen13-Mar-02 11:32
memberDaniel Bowen13-Mar-02 11:32 
GeneralRe: Another (better?) version already exist Pin
Ilushka8-Jul-03 23:30
memberIlushka8-Jul-03 23:30 
QuestionSize of the screen? Pin
Todd Smith11-Mar-02 20:08
memberTodd Smith11-Mar-02 20:08 
AnswerRe: Size of the screen? Pin
Paul A. Howes12-Mar-02 0:58
memberPaul A. Howes12-Mar-02 0:58 
GeneralRe: Size of the screen? Pin
Jörgen Sigvardsson22-Dec-02 4:39
memberJörgen Sigvardsson22-Dec-02 4:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180216.1 | Last Updated 11 Mar 2002
Article Copyright 2002 by Paul A. Howes
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid