1

I want port a MFC project to the current available resources.

I develop with Microsoft Visual Studio Community.

In the older project the Windows SDK Version is 10.0.15063.0

in the new project the Windows SDK Version is 10.0.17763.0

the older project uses ComCtrl32.dll Version 5.82

the new project uses ComCtrl32.dll Version 6.10

After the update with SetWindowTextW(textp) the used CEdit control shows a black control rectangle

If I move the cursor over the control it looks as expected.

ValEdit.h :

class ValEdit : public CEdit
{
public:
    ValEdit();
    virtual ~ValEdit();
    int ZeroMeansInactiv;

protected:

    afx_msg BOOL OnEraseBkgnd(CDC* pDC);
    afx_msg void OnUpdate();

    DECLARE_MESSAGE_MAP()

private:

    COLORREF    m_TextColor;

    HBRUSH m_hBackgroundBrush;
    HBRUSH m_hBackgrInactivBrush;

};

ValEdit.cpp :

ValEdit::ValEdit()
{
    ZeroMeansInactiv = 1;
    m_TextColor = Black;
    m_hBackgroundBrush = CreateSolidBrush(RGB(255, 255, 255));
    m_hBackgrInactivBrush = CreateSolidBrush(RGB(90, 90, 90));
}

ValEdit::~ValEdit()
{
}

BEGIN_MESSAGE_MAP(ValEdit, CEdit)
    ON_WM_ERASEBKGND()
    ON_CONTROL_REFLECT(EN_UPDATE, OnUpdate)
END_MESSAGE_MAP()

BOOL ValEdit::OnEraseBkgnd(CDC* pDC) 
{
    RECT rc;
    this->GetClientRect(&rc);

    SetMapMode(*pDC, MM_TEXT);
    FillRect(*pDC, &rc, !!ZeroMeansInactiv ? m_hBackgroundBrush : m_hBackgrInactivBrush );
    return TRUE;
}

void ValEdit::OnUpdate() 
{
    RedrawWindow();
}

Thank you for advice

Erhy

Danny_ds
  • 11,201
  • 1
  • 24
  • 46
Erhy
  • 92
  • 3
  • 8
  • Have a question... You specifically say SetWindowTextW() instead of SetWindowText(). Maybe it's nothing, but are you building an MBCS application and need to call SetWindowTextW() because SetWindowText() calls SetWindowTextA()? If so, the 6.0 version of common controls is not recommended for MBCS applications. If not, if you change Map Mode in OnEraseBkgnd, you should restore it. (Would have thought it was MM_TEXT by default and not needing to be set) – Joseph Willcoxson Mar 21 '20 at 18:55
  • now I updated to SetWindowText(). It runs also and if I click to "peek definition" there is shown #define SetWindowText SetWindowTextW – Erhy Mar 22 '20 at 13:28
  • As far as I'm concerned , you should ensure that your application is compatible with different targeted versions of a ComCtl32.dll file. If you do not define the _WIN32_IE macro in your project, it is automatically defined as 0x0500. I suggest you could refer to the link: https://learn.microsoft.com/zh-cn/windows/win32/controls/common-control-versions If you want the application to use version 6.10, I suggest you try to [enable visual style](https://learn.microsoft.com/en-us/windows/win32/controls/cookbook-overview) – Jeaninez - MSFT Mar 24 '20 at 02:27
  • in InitInstance of the App I coded INITCOMMONCONTROLSEX InitCtrls; InitCtrls.dwSize = sizeof(InitCtrls); InitCtrls.dwICC = ICC_WIN95_CLASSES; InitCommonControlsEx(&InitCtrls); – Erhy Mar 24 '20 at 13:21

1 Answers1

0

please, I need explanations!

I programmed the app step by step and found the code, which is responsible for the malfunction, that the CEdit control is not updated correctly.

HBRUSH CStyleToolkitDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    if (nCtlColor == CTLCOLOR_STATIC)
    {
        // We handle this message only if we have set the region
        BOOL bHandled = m_bIsRgnSet;
        if (bHandled)
        {
            HDC hDC = pDC->GetSafeHdc();
            SetBkMode(hDC, TRANSPARENT);
            return (HBRUSH)GetStockObject(HOLLOW_BRUSH); //causes the malfunction
        }
    }
    return hbr;
}

If I exclude CEdit with

if (!pWnd->IsKindOf(RUNTIME_CLASS(CEdit)))
            return (HBRUSH)GetStockObject(HOLLOW_BRUSH);

the CEdit Control is updated as expected.

Thank you for discussion

Erhy
  • 92
  • 3
  • 8
  • now I removed the OnCtlColor Event routine and the app works also. Many years before I looked for code examples in "Code Project" and adopted this detail. – Erhy Mar 31 '20 at 20:13