Double Buffering technique in MFC for Flicker Free drawing

Visual Programming

Flickering is caused in a window,  by painting,  in two steps.

(1) Erasing the window contents in OnEraseBkgnd.  By default MFC does this.

(2) Painting new contents in OnPaint or OnDraw.

Double buffering eliminates this two-step process by overriding OnEraseBkgnd and including the erase in OnPaint/OnDraw to an off-screen image.

1. Overriding the OnEraseBkgnd function:

  • Add a message handler for WM_ERASEBKGND in your Windows application.
  • Once added it looks like the below one:
  • BOOL CExampleView::OnEraseBkgnd(CDC* pDC) 
    {
          // TODO: Add your message handler code here and/or call default
          return CView::OnEraseBkgnd(pDC);
    }
    
    Now change the return value to any non-zero value and comment the default proced-ure.
    BOOL CExampleView::OnEraseBkgnd(CDC* pDC) 
    {
          return TRUE;
    }

2. Now In the OnPaint/OnDraw do the following steps:

1.  Create a Memory DC for the View DC as below:

               CreateCompatibleDC(pDC);

2.   Create the bitmap in memory…

View original post 78 more words

About Khuram Ali

Programming... Programming and Programming...!!!

Posted on August 22, 2013, in Windows Programming. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: