visual-c++mfc

CMFCEditBrowseCtrl icon does not render as disabled when using EnableWindow(false). How to fix?


I am using the CMFCEditBrowseCtrl and display my own icon.

There are times when I want to disable the control (ie. m_control.EnableWindow(false);. But it does not behave as I expect:

enter image description here

It is disabled, but the icon does not grey out. And this looks weird I think. What is the right way to address this?

This is how I assign the image to use (during OnInitDialog):

m_control.SetBrowseButtonImage(IDB_PNG_IMAGE);

Solution

  • I decided to draw it myself:

    // Draws the browse button icon based on the current browse mode.
    void CDarkModeEditBrowseCtrl::OnDrawBrowseButton(CDC* pDC, CRect rect, BOOL bIsButtonPressed, BOOL bIsButtonHot)
    {
        // Ensure the image list is valid
        if (!m_ImageBrowse.GetSafeHandle())
        {
            TRACE("m_ImageBrowse is not initialized!\n");
            return;
        }
    
        const int iconSize = 16;
        const int x = rect.left + (rect.Width() - iconSize) / 2;
        const int y = rect.top + (rect.Height() - iconSize) / 2;
    
        UINT drawStyle = ILD_TRANSPARENT;
    
        // Check if the control is disabled
        if (!IsWindowEnabled())
        {
            drawStyle = ILD_BLEND50;  // Draw with 50% blend for a "disabled" effect
        }
    
        m_ImageBrowse.Draw(pDC, 0, CPoint(x, y), drawStyle);
    }