Click here to Skip to main content
15,886,137 members
Articles / Desktop Programming / MFC
Article

Further discussions on flicker free drawing

Rate me:
Please Sign up or sign in to vote.
4.59/5 (16 votes)
23 May 20032 min read 201.2K   54   51
An article on how to solve flicker problems

Introduction

This article aims to show some useful codes for solving flicker problems in CTreeCtrl, CListCtrl and GDI+. And it could be viewed as an extend of Keith Rule's great article "Flicker Free Drawing In MFC".

Background

The only background to this article is that you have read the great article of Keith Rule. Thanks him for his nice code. We will use the class CMemDC proposed by him.

CTreeCtrl

For a CTreeCtrl derived class, if we want to avoid flicker, we could carry out the following steps.

  • Add the file memdc.h in your project.
  • Add the line #include "memdc.h" to stdafx.h or the desired .h file.
  • Add a public CRect variable like CRect m_rectClient;
  • Add a windows message handler for WM_ERASEBKGND, WM_SIZE, and WM_PAINT.

Then we should add the code as following:

BOOL CTreeCtrl::OnEraseBkgnd(CDC* pDC) 
{
    // TODO: Add your message handler code here and/or call default
    UNUSED_ALWAYS(pDC);

    //return CTreeCtrl::OnEraseBkgnd(pDC);
    return TRUE;
}

void CTreeCtrl::OnSize(UINT nType, int cx, int cy) 
{
    CTreeCtrl::OnSize(nType, cx, cy);

    GetClientRect(m_rectClient);
}

void CTreeCtrl::OnPaint() 
{
    CPaintDC dc(this);

    // Paint to a memory device context to reduce screen flicker.
    CMemDC memDC(&dc, &m_rectClient);

    ......

    // Let the window do its default painting...
    CWnd::DefWindowProc( WM_PAINT, (WPARAM)memDC.m_hDC, 0 );
}

Then you would get flicker free effect.

CListCtrl

For a CListCtrl derived class, all of the procedures are the same expect the OnSize function should be modified as following to allow the CHeadCtrl (if in report model) to display:

void CListCtrl::OnSize(UINT nType, int cx, int cy) 
{
    CListCtrl::OnSize(nType, cx, cy);

    GetClientRect(m_rectClient);

    CHeaderCtrl* pHC;
    pHC = GetHeaderCtrl();
    if (pHC != NULL)
    {
        CRect rectHeader;
        pHC->GetItemRect( 0, &rectHeader );
        m_rectClient.top += rectHeader.bottom;
    }
}

Then you could get flicker free effect.

GDI+

I have found several articles talking about flicker free drawing in GDI+. However, I want to extend Keith Rule's code to do the same job and avoid to re-implement the double buffer mechanism with CashedBitmap. Thus I use the following method.

  • Add the file memdc.h in your project.
  • Add the line #include "memdc.h" to stdafx.h or the desired .h file.
  • Add a public CRect variable likeCRect m_rectClient; Add a windows message handler for WM_ERASEBKGND, WM_SIZE, and WM_PAINT.

Then we should add the code as following:

BOOL CView::OnEraseBkgnd(CDC* pDC) 
{
    // TODO: Add your message handler code here and/or call default
    UNUSED_ALWAYS(pDC);

    //return CView::OnEraseBkgnd(pDC);
    return TRUE;
}

void CView::OnSize(UINT nType, int cx, int cy) 
{
    CView::OnSize(nType, cx, cy);

    GetClientRect(m_rectClient);
}

void CView::OnPaint() 
{
    CPaintDC dc(this);

    // Paint to a memory device context to reduce screen flicker.
    CMemDC memDC(&dc, &m_rectClient);
    Graphics graphics(memDC);

    ......
}

Then you could get flicker free effect. This method could also do well in CScrollView. Ok, finally, there is still an interesting question left for you: which method will be faster, this method or the double buffer implemented by CashedBitmap?

History

  • Initial release 2003-05-24.

License

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


Written By
China China
I am now a faculty in Engineering. But I am also a programmer who use VC from VC1.5 to VC8.0. However, now I prefer to use C# and VB.Net in my job.

Comments and Discussions

 
GeneralRe: Possible to use with owner draw List Control Pin
TeleStar5-Nov-03 6:56
TeleStar5-Nov-03 6:56 
GeneralRe: Possible to use with owner draw List Control Pin
tlee5-Nov-03 12:07
tlee5-Nov-03 12:07 
GeneralRe: Possible to use with owner draw List Control Pin
TeleStar5-Nov-03 13:36
TeleStar5-Nov-03 13:36 
GeneralRe: Possible to use with owner draw List Control Pin
tlee6-Nov-03 3:47
tlee6-Nov-03 3:47 
GeneralCTreeCtrl experience using CMemDC Pin
Kennon4-Jan-04 19:04
Kennon4-Jan-04 19:04 
GeneralThanks a lot for your kind comments! Here is my response. Pin
TeleStar5-Jan-04 6:53
TeleStar5-Jan-04 6:53 
GeneralCScrollView and GDI+ Pin
Salvador Dali30-Oct-03 4:35
Salvador Dali30-Oct-03 4:35 
GeneralRe: CScrollView and GDI+ Pin
TeleStar2-Nov-03 14:36
TeleStar2-Nov-03 14:36 
GeneralRe: CScrollView and GDI+ Pin
Salvador Dali2-Nov-03 21:54
Salvador Dali2-Nov-03 21:54 
GeneralRe: CScrollView and GDI+ Pin
TeleStar4-Nov-03 6:36
TeleStar4-Nov-03 6:36 
GeneralRe: CScrollView and GDI+ Pin
Salvador Dali5-Nov-03 0:14
Salvador Dali5-Nov-03 0:14 
GeneralRe: CScrollView and GDI+ Pin
TeleStar5-Nov-03 6:48
TeleStar5-Nov-03 6:48 
GeneralRe: CScrollView and GDI+ Pin
Salvador Dali7-Nov-03 1:46
Salvador Dali7-Nov-03 1:46 
GeneralRe: CScrollView and GDI+ Pin
TeleStar7-Nov-03 6:51
TeleStar7-Nov-03 6:51 
GeneralRe: CScrollView and GDI+ Pin
EqualsP3-Mar-04 18:15
EqualsP3-Mar-04 18:15 
GeneralRe: CScrollView and GDI+ Pin
TeleStar14-Mar-04 10:16
TeleStar14-Mar-04 10:16 
GeneralIt works on the Pocket PC... Pin
João Paulo Figueira18-Sep-03 1:29
professionalJoão Paulo Figueira18-Sep-03 1:29 
GeneralRe: It works on the Pocket PC... Pin
TeleStar19-Sep-03 19:57
TeleStar19-Sep-03 19:57 
GeneralNice and crispy Pin
Nish Nishant18-Sep-03 0:46
sitebuilderNish Nishant18-Sep-03 0:46 
GeneralRe: Nice and crispy Pin
TeleStar19-Sep-03 19:46
TeleStar19-Sep-03 19:46 
GeneralFlicker on Header of List view Pin
Member 1639183-Jun-03 20:37
Member 1639183-Jun-03 20:37 
GeneralRe: Flicker on Header of List view Pin
TeleStar4-Jun-03 12:18
TeleStar4-Jun-03 12:18 
GeneralCommon controls Pin
Paolo Messina27-May-03 11:34
professionalPaolo Messina27-May-03 11:34 
GeneralRe: Common controls Pin
TeleStar28-May-03 9:00
TeleStar28-May-03 9:00 
GeneralGood stuff Pin
David Pritchard26-May-03 3:23
David Pritchard26-May-03 3:23 

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.