Loading...

CMFCEditBrowseCtrl Without Flickering

View: 216    Dowload: 0   Comment: 0   Post by: admin   Category: ASP.NET Framework   Fields: Other

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.

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);
...
}

Many methods of MFC-Feature Pack classes are declared virtual, so 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:
  virtual void OnDrawBrowseButton(CDC* pDC, CRect rect, BOOL bIsButtonPressed, BOOL bIsButtonHot)
  {
    if (!mFirstTime && mLastIsPressed == bIsButtonPressed && mLastIsHot == bIsButtonHot)
    {
      return;
    }
    mFirstTime = false;
    mLastIsPressed = bIsButtonPressed;
    mLastIsHot = bIsButtonHot;
    __super::OnDrawBrowseButton(pDC, rect, bIsButtonPressed, bIsButtonHot);
  }

protected:
  bool mFirstTime = true;
  BOOL mLastIsPressed = FALSE;
  BOOL mLastIsHot = FALSE;
};

This implementation remembers the state when the button was drawn last time, and redraws it only when necessary. The mFirstTime member is required, to ensure correct initialization of the other two variables. (More sophisticated implementation certainly is possible.)

CMFCEditBrowseCtrl Without Flickering

Workaround for a bug in the CMFCEditBrowseCtrl that causes heavy flickering (starting with Windows 7)

Posted on 29-03-2018 

Comment:

To comment you must be logged in members.

Files with category

 
File suggestion for you
Loading...
File top downloads
Loading...
Loading...
Codetitle - library source code to share, download the file to the community
Copyright © 2018. All rights reserved. codetitle Develope by Vinagon .Ltd