Click here to Skip to main content
11,413,548 members (74,385 online)
Click here to Skip to main content

Further discussions on flicker free drawing

, 23 May 2003
Rate this:
Please Sign up or sign in to vote.
An article on how to solve flicker problems
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS--><!-- Add the rest of your HTML here -->

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

Share

About the Author

TeleStar

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

 
GeneralGDI+ Pin
Alex Voronyansky at 26-Apr-09 13:19
memberAlex Voronyansky26-Apr-09 13:19 
QuestionHow to update listview ONLY when new data is avilable? Pin
method007 at 9-May-07 15:58
membermethod0079-May-07 15:58 
GeneralWM_PAINT Pin
mark pinnuck at 3-Apr-07 13:46
membermark pinnuck3-Apr-07 13:46 
QuestionIneffective methods?? Pin
Douglas R. Keesler at 29-Dec-06 19:54
memberDouglas R. Keesler29-Dec-06 19:54 
GeneralGDI+ with CMemDC slow ! Pin
Lord Darius at 28-May-06 2:00
memberLord Darius28-May-06 2:00 
GeneralRe: GDI+ with CMemDC slow ! Pin
TeleStar at 6-Sep-06 23:33
memberTeleStar6-Sep-06 23:33 
QuestionFlickers with WS_EX_LAYOUTRTL Pin
_Stilgar_ at 30-Mar-06 22:16
member_Stilgar_30-Mar-06 22:16 
AnswerRe: Flickers with WS_EX_LAYOUTRTL Pin
TeleStar at 6-Sep-06 23:27
memberTeleStar6-Sep-06 23:27 
Generalwhere is the original article Pin
ivan françois at 17-Jan-06 8:17
memberivan françois17-Jan-06 8:17 
Generalhttp://www.codeproject.com/gdi/flickerfree.asp Pin
TeleStar at 27-Jan-06 14:06
memberTeleStar27-Jan-06 14:06 
QuestionHow to use in a dialog-based app? Pin
Hawks at 22-Aug-05 8:52
memberHawks22-Aug-05 8:52 
Answerwhy need to use CMemDC in a dialog-based app? Pin
TeleStar at 22-Aug-05 19:03
memberTeleStar22-Aug-05 19:03 
GeneralBetter code for CTreeCtrl Pin
Adam Tegen at 16-Aug-04 7:12
sussAdam Tegen16-Aug-04 7:12 
GeneralBetter code for CListCtrl Pin
Adam Tegen at 16-Aug-04 6:59
sussAdam Tegen16-Aug-04 6:59 
Generalyou are right, and I think that's what they do in BCG and XTPro Pin
AI2004 at 17-Aug-04 7:44
memberAI200417-Aug-04 7:44 
GeneralRe: Better code for CListCtrl Pin
Anonymous at 4-Mar-05 12:18
sussAnonymous4-Mar-05 12:18 
GeneralAwesoma powa!:) Pin
abloy at 25-Aug-07 5:01
memberabloy25-Aug-07 5:01 
GeneralStop Flicker Alternative Pin
C@ssidy at 15-Apr-09 6:09
memberC@ssidy15-Apr-09 6:09 
QuestionReduce flicker in ListView ? Pin
_skidrow_vn_ at 11-Nov-03 8:34
member_skidrow_vn_11-Nov-03 8:34 
AnswerRe: Reduce flicker in ListView ? Pin
xxxyyyzzz at 1-Dec-03 21:01
memberxxxyyyzzz1-Dec-03 21:01 
AnswerRe: Reduce flicker in ListView ? Pin
Endemoniada at 24-Mar-04 16:49
memberEndemoniada24-Mar-04 16:49 
GeneralIf you use SetRedraw(), Pin
xxxyyyzzz at 26-Mar-04 11:38
memberxxxyyyzzz26-Mar-04 11:38 
GeneralPossible to use with owner draw List Control Pin
tlee at 4-Nov-03 11:17
membertlee4-Nov-03 11:17 
GeneralRe: Possible to use with owner draw List Control Pin
xxxyyyzzz at 4-Nov-03 12:32
memberxxxyyyzzz4-Nov-03 12:32 
GeneralRe: Possible to use with owner draw List Control Pin
tlee at 4-Nov-03 13:10
membertlee4-Nov-03 13:10 
GeneralRe: Possible to use with owner draw List Control Pin
xxxyyyzzz at 5-Nov-03 7:56
memberxxxyyyzzz5-Nov-03 7:56 
GeneralRe: Possible to use with owner draw List Control Pin
tlee at 5-Nov-03 13:07
membertlee5-Nov-03 13:07 
GeneralRe: Possible to use with owner draw List Control Pin
xxxyyyzzz at 5-Nov-03 14:36
memberxxxyyyzzz5-Nov-03 14:36 
GeneralRe: Possible to use with owner draw List Control Pin
tlee at 6-Nov-03 4:47
membertlee6-Nov-03 4:47 
GeneralCTreeCtrl experience using CMemDC Pin
Kennon at 4-Jan-04 20:04
memberKennon4-Jan-04 20:04 
GeneralThanks a lot for your kind comments! Here is my response. Pin
xxxyyyzzz at 5-Jan-04 7:53
memberxxxyyyzzz5-Jan-04 7:53 
GeneralCScrollView and GDI+ Pin
Salvador Dali at 30-Oct-03 5:35
memberSalvador Dali30-Oct-03 5:35 
GeneralRe: CScrollView and GDI+ Pin
xxxyyyzzz at 2-Nov-03 15:36
memberxxxyyyzzz2-Nov-03 15:36 
GeneralRe: CScrollView and GDI+ Pin
Salvador Dali at 2-Nov-03 22:54
memberSalvador Dali2-Nov-03 22:54 
GeneralRe: CScrollView and GDI+ Pin
xxxyyyzzz at 4-Nov-03 7:36
memberxxxyyyzzz4-Nov-03 7:36 
GeneralRe: CScrollView and GDI+ Pin
Salvador Dali at 5-Nov-03 1:14
memberSalvador Dali5-Nov-03 1:14 
GeneralRe: CScrollView and GDI+ Pin
xxxyyyzzz at 5-Nov-03 7:48
memberxxxyyyzzz5-Nov-03 7:48 
GeneralRe: CScrollView and GDI+ Pin
Salvador Dali at 7-Nov-03 2:46
memberSalvador Dali7-Nov-03 2:46 
GeneralRe: CScrollView and GDI+ Pin
xxxyyyzzz at 7-Nov-03 7:51
memberxxxyyyzzz7-Nov-03 7:51 
GeneralRe: CScrollView and GDI+ Pin
EqualsP at 3-Mar-04 19:15
memberEqualsP3-Mar-04 19:15 
GeneralRe: CScrollView and GDI+ Pin
xxxyyyzzz at 14-Mar-04 11:16
memberxxxyyyzzz14-Mar-04 11:16 
GeneralIt works on the Pocket PC... Pin
João Paulo Figueira at 18-Sep-03 2:29
memberJoão Paulo Figueira18-Sep-03 2:29 
GeneralRe: It works on the Pocket PC... Pin
xxxyyyzzz at 19-Sep-03 20:57
memberxxxyyyzzz19-Sep-03 20:57 
GeneralNice and crispy Pin
Nishant S at 18-Sep-03 1:46
editorNishant S18-Sep-03 1:46 
GeneralRe: Nice and crispy Pin
xxxyyyzzz at 19-Sep-03 20:46
memberxxxyyyzzz19-Sep-03 20:46 
GeneralFlicker on Header of List view Pin
dilipm@danlawinc.com at 3-Jun-03 21:37
memberdilipm@danlawinc.com3-Jun-03 21:37 
GeneralRe: Flicker on Header of List view Pin
xxxyyyzzz at 4-Jun-03 13:18
memberxxxyyyzzz4-Jun-03 13:18 
GeneralCommon controls Pin
Paolo Messina at 27-May-03 12:34
memberPaolo Messina27-May-03 12:34 
GeneralRe: Common controls Pin
xxxyyyzzz at 28-May-03 10:00
memberxxxyyyzzz28-May-03 10:00 
GeneralGood stuff Pin
LordLiverpool at 26-May-03 4:23
memberLordLiverpool26-May-03 4:23 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150427.1 | Last Updated 24 May 2003
Article Copyright 2003 by TeleStar
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid