Click here to Skip to main content
13,730,905 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

6.1K views
191 downloads
8 bookmarked
Posted 27 Mar 2018
Licenced CPOL

CMFCEditBrowseCtrl Without Flickering

, 15 Apr 2018
Rate this:
Please Sign up or sign in to vote.
Workaround for a bug in the CMFCEditBrowseCtrl that causes heavy flickering (starting with Windows 7)

Introduction

In this article, I want to show a simple workaround for a bug in the MFC Feature Pack, and explain how I arrived at the solution.

Background

When you open the attached application and move your mouse, you will notice that the folder icon of the first edit control flickers heavily.

demo of the flicker

To find the cause of this problem, I used WinSpector, but Spy++ works as well. Open up Spy++ and show the messages sent to the first edit control. When you move the mouse above the control, you will see many WM_NCPAINT-messages:

So let's look up the MFC source code, to find out what the CMFCEditBrowseCtrl does with the WM_NCPAINT-Message. The code is located in the file afxeditbrowsectrl.cpp.

BEGIN_MESSAGE_MAP(CMFCEditBrowseCtrl, CEdit)
...
    ON_WM_NCPAINT()
...
END_MESSAGE_MAP()

...

void CMFCEditBrowseCtrl::OnNcPaint()
{
    CEdit::OnNcPaint();
...
    OnDrawBrowseButton(&dc, rect, m_bIsButtonPressed, m_bIsButtonHighlighted);
...
}

Stepping further into OnDrawBrowseButton we arive at CMFCVisualManagerWindows::OnDrawBrowseButton (located in afxvisualmanagerwindows.cpp)

BOOL CMFCVisualManagerWindows::OnDrawBrowseButton(...)
{
...
    pDC->FillRect(rect, &(GetGlobalData()->brWindow));
...
}

So everythime, the button is drawn via WM_NCPAINT, the visualmanager first erases the area, and then redraws everything. This causes the flickering.

Many methods of MFC-Feature Pack classes are declared virtual, so to workaround this issue, we can easily add our own, customized implementation instead.

Using the Code

To remove the flicker, replace the CMFCEditBrowseCtrl with this fixed implementation:

class CMFCEditBrowseCtrlNoFlicker : public CMFCEditBrowseCtrl
{
public:
  void OnDrawBrowseButton(CDC* pDC, CRect rect, BOOL bIsButtonPressed, BOOL bIsButtonHot) override
  {
    CMemDC dc(*pDC, rect);
    __super::OnDrawBrowseButton(&dc.GetDC(), rect, bIsButtonPressed, bIsButtonHot);
  }
};

In this implementation, we use a CMemDC, i.e., we buffer all drawing commands. So technically, the button is still erased and redrawn many times, but since only the final result is drawn on the screen, the flickering is gone.

History

  • 2018-04-03: First version (skip drawing when flags did not change)
  • 2018-04-16: Updated implementation with CMemDC (fixes missing redraw after minimizing the application)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

hofingerandi
Software Developer
Austria Austria
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
Questionproblem Pin
Michel Wassink7-Apr-18 11:49
memberMichel Wassink7-Apr-18 11:49 
AnswerRe: problem Pin
hofingerandi15-Apr-18 22:27
memberhofingerandi15-Apr-18 22:27 
GeneralRe: problem Pin
Michel Wassink18-Apr-18 9:50
memberMichel Wassink18-Apr-18 9:50 
GeneralA Point of Interest Pin
Rick York27-Mar-18 11:45
memberRick York27-Mar-18 11:45 
Questiongreat Pin
Michel Wassink27-Mar-18 11:12
memberMichel Wassink27-Mar-18 11:12 
QuestionAttached Application? Pin
Rick York27-Mar-18 6:32
memberRick York27-Mar-18 6:32 
AnswerRe: Attached Application? Pin
hofingerandi27-Mar-18 9:58
memberhofingerandi27-Mar-18 9:58 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180920.1 | Last Updated 16 Apr 2018
Article Copyright 2018 by hofingerandi
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid