MFC: Changing the colour of CEdit

ⅰ亾dé卋堺 提交于 2019-11-28 11:33:41

You cannot do it with a plain CEdit, you need to override a few bits.

Implement your own ON_WM_CTLCOLOR_REFLECT handler, then return your coloured CBrush in the handler:

(roughly, you'll need to put the usual resource management in there, rememebr to delete your brush in the destructor)

class CColorEdit : public CEdit
{
  ....
  CBrush   m_brBkgnd;
  afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor)
  {
    m_brBkgnd.DeleteObject();
    m_brBkgnd.CreateSolidBrush(nCtlColor);
  }
}
amolbk

This can also be done without deriving from CEdit:

  1. Add ON_WM_CTLCOLOR() to your dialog's BEGIN_MESSAGE_MAP() code block.
  2. Add OnCltColor() to your dialog class:

    afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
    
  3. Implement OnCtlColor() like so:

    HBRUSH CMyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
        if ((CTLCOLOR_EDIT == nCtlColor) &&
            (IDC_MY_EDIT == pWnd->GetDlgCtrlID()))
        {
            return m_brMyEditBk; //Create this brush in OnInitDialog() and destroy in destructor
        }
        return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    }
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!