Click here to Skip to main content
15,881,380 members
Articles / Desktop Programming / MFC
Tip/Trick

CMFCEditBrowseCtrl Without Flickering

Rate me:
Please Sign up or sign in to vote.
5.00/5 (15 votes)
15 Apr 2018CPOL1 min read 16.3K   580   11   7
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:

Image 2

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.

C++
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)

C++
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:

C++
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)


Written By
Software Developer
Austria Austria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionproblem Pin
Michel Wassink7-Apr-18 11:49
Michel Wassink7-Apr-18 11:49 
AnswerRe: problem Pin
hofingerandi15-Apr-18 22:27
hofingerandi15-Apr-18 22:27 
GeneralRe: problem Pin
Michel Wassink18-Apr-18 9:50
Michel Wassink18-Apr-18 9:50 
GeneralA Point of Interest Pin
Rick York27-Mar-18 11:45
mveRick York27-Mar-18 11:45 
Questiongreat Pin
Michel Wassink27-Mar-18 11:12
Michel Wassink27-Mar-18 11:12 
QuestionAttached Application? Pin
Rick York27-Mar-18 6:32
mveRick York27-Mar-18 6:32 
AnswerRe: Attached Application? Pin
hofingerandi27-Mar-18 9:58
hofingerandi27-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.