Really (or at least more) Flicker Free

-->

On CodeGuru there are a lot of articles to be found on Flicker Free drawing. The sheer number of articles indicates that none of them is satisfying. Most of the articles and answers suggest to use SetRedraw() on each instance you want to control the drawing (see SomeFunction() below). One drawback of that method is that it doesn't always work. Inserting a column in a list view, for example, seems to ignore the SetRedraw flag. Another drawback is that when your code grows you can't keep track of all those SetRedraw() calls and end up with nested ones that neutralise each other:

void CMyTreeView::SomeFunction()
{
    SetRedraw(FALSE);
    ... do some stuff ...

    SetRedraw(TRUE);
    Invalidate();
}

void CMyTreeView::AnotherFunction()
{
    SetRedraw(FALSE);
    ... do some stuff ...
    SomeFunction();
    ... do more stuff ...  What about the redraw state here?
    SetRedraw(TRUE);
    Invalidate();
}

You get the point. To improve flicker free drawing I do two things: use the SetRedraw() only for the main window of the application and use an internal counter to track nested call and only call SetRedraw() on the first and last occasion of a series.

I implemented it just like CWaitCursor: declare a variable of class CWaitRedraw at the beginning of each function that causes flickering. When the variabele goes out of scope the destructor of CWaitRedraw will be called and handle restoring of the drawing state.

The steps to implement this are as follows:

1. Insert the class CWaitRedraw in your application
2. Put a statement like this at the beginning of each function which causes flickering:
    CWaitRedraw wait;

That's it!

WaitRedraw.h:
/////////////////////////////////////////////////////////////////////
// WaitRedraw.h - class for flicker free drawing
//

#ifndef __WAITREDRAW_H__
#define __WAITREDRAW_H__

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

// If not used in a DLL, remove AFX_EXT_CLASS
class AFX_EXT_CLASS CWaitRedraw
{
// Construction/Destruction
public:
    CWaitRedraw();
   ~CWaitRedraw();

// Operations
public:
   void Restore();

protected:
   static int m_nRedrawCount;
};

#endif // __WAITREDRAW_H__

WaitRedraw.cpp:
/////////////////////////////////////////////////////////////////////
// WaitRedraw.cpp - class for flicker free drawing
//
#include "stdafx.h"
#include "WaitRedraw.h"

int CWaitRedraw::m_nRedrawCount = 0;

CWaitRedraw::CWaitRedraw()
{
   CWnd * pMainWnd = AfxGetMainWnd();
   if (pMainWnd)
   {
      if (m_nRedrawCount == 0)
      pMainWnd->SetRedraw(FALSE);
      m_nRedrawCount++;
   }
}

CWaitRedraw::~CWaitRedraw()
{
    CWnd * pMainWnd = AfxGetMainWnd();
    if (pMainWnd)
    {
        if (m_nRedrawCount > 0)

            m_nRedrawCount--;
        if (m_nRedrawCount == 0)
        {
            pMainWnd->SetRedraw(TRUE);
            pMainWnd->Invalidate();
        }
    }
}

// Function to force redrawing
void CWaitRedraw::Restore()
{
   m_nRedrawCount = 0;
   CWnd * pMainWnd = AfxGetMainWnd();
   if (pMainWnd)
   {
      pMainWnd->SetRedraw(TRUE);
      pMainWnd->Invalidate();
   }
}



Download source - 1 KB



Comments

  • CWaitCursor disappears

    Posted by Legacy on 08/05/2002 12:00am

    Originally posted by: Jonathan Taylor

    I have been trying to implement the CWaitRedraw class but each time SetRedraw(FALSE) gets called it resets the CWaitCursor is there anyway round this as it's quite annoying and hindering the usefulness of CWaitRedraw.

    Thanks

    Reply
  • Thoughts

    Posted by Legacy on 11/19/1998 12:00am

    Originally posted by: None

    Any class that omits invalidate window after every call should not
    be called flicker free draw. this is far from flicker free.

    My two cents,
    None

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The open source cloud computing project OpenStack has come a long way since NASA and Rackspace launched it in 2010. Backed by leading technology infrastructure providers including Cisco, Dell, EMC, HP, IBM, Intel, and VMware, OpenStack underpins significant workloads at an increasingly diverse set of organizations, including BWM, CERN, Comcast, eBay, and Wal-Mart. For CIOs engaged in broader programs to win, serve, and retain customers -- and refocus business technology (BT) spend -- a planned and pragmatic …

  • On-demand EventEvent Date: March 21, 2017 With the rise in ransomware attacks, bank heists that reached new levels of sophistication, and extortion plots that were beyond anything we could have imagined, 2016 certainly was an eventful year for cybersecurity. Going into 2017, no business can afford to be uninformed about cybersecurity or unprepared for an attack. Watch this informative webinar, presented by Andrey Pozhogin, Senior B2B Product Marketing Manager at Kaspersky Lab, as he examines predictions for …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date