1

I'm dealing with a very weird visual bug. Let me explain.

I have a C++/MFC application that displays a count in a window:

enter image description here

The count (text) is displayed via a CStatic control. The mechanism is very simple. I call it every 1000 ms and update the text as such:

void CTestCountdownFlickerDlg::RedrawCounter(LPCTSTR pText)
{
    CStatic* pTxtBox = (CStatic*)this->GetDlgItem(IDC_STATIC_COUNTER);
    ASSERT(pTxtBox);

    //Get previous text
    CString strPrevText;
    pTxtBox->GetWindowText(strPrevText);

    //Update only if different
    if(strPrevText.Compare(pText) != 0)
    {
        //Set next text
        pTxtBox->SetWindowText(pText);
    }
}

What happens is that the CStatic control updates without any issues on the OS with visual themes enabled, but if I run it on the OS with the Windows Classic theme (for instance, the screenshot above is from Windows 7) the CStatic control produces a visible flicker every time it updates the text.

Well, I understand that I'm nitpicking here, still I would really like to get rid of this flicker.

Here's what I tried:

  1. In my actual project I tried subclassing the CStatic control and removed the processing of WM_ERASEBACKGROUND by simply returning 1. That didn't help.

  2. In the same subclass for CStatic control I tried to override WM_PAINT, but that didn't work at all. So I'm not sure if I'm going too far with it at this point.

Here's the C++/MFC source code for my test project I made the screenshot above for.

c00000fd
  • 20,994
  • 29
  • 177
  • 400
  • Make a new test project that does only that - say it has a button that calls `CStatic::SetWindowText()`. If you can reproduce the flicker there as well, it's probably a bug in MFC itself. Also see http://stackoverflow.com/questions/7714034/mfc-how-to-avoid-flickering-in-child-control-updates. May be try `SetRedraw(FALSE)`, as they suggest in http://stackoverflow.com/a/7717589/492336 – sashoalm Nov 16 '13 at 06:16
  • @sashoalm: No, it is actually a (visual) "bug" in Windows, if you want to call it then. I was able to resolve it though by drawing the static control myself (by setting the `SS_OWNERDRAW` style) and not doing anything in `WM_ERASEBACKGROUND`. The flicker is produced because of drawing in two different messages... – c00000fd Nov 16 '13 at 06:58

0 Answers0