MFC:改变CEdit的颜色

时间:2022-12-31 00:07:01

Guys, can someone give me a brief run through of how to change the background colour of a CEdit control at runtime? I want to be able to change the background to red if the field is zero length and the normal white otherwise.

伙计们,有人可以简要介绍一下如何在运行时更改CEdit控件的背景颜色吗?如果字段长度为零,我希望能够将背景更改为红色,否则将正常白色更改为背景。

2 个解决方案

#1


7  

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

你不能用普通的CEdit来做,你需要覆盖几个位。

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

实现自己的ON_WM_CTLCOLOR_REFLECT处理程序,然后在处理程序中返回彩色CBrush:

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

#2


4  

This can also be done without deriving from CEdit:

这也可以在不从CEdit派生的情况下完成:

  1. Add ON_WM_CTLCOLOR() to your dialog's BEGIN_MESSAGE_MAP() code block.
  2. 将ON_WM_CTLCOLOR()添加到对话框的BEGIN_MESSAGE_MAP()代码块中。

  3. Add OnCltColor() to your dialog class:

    将OnCltColor()添加到对话框类:

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

    像这样实现OnCtlColor():

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

#1


7  

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

你不能用普通的CEdit来做,你需要覆盖几个位。

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

实现自己的ON_WM_CTLCOLOR_REFLECT处理程序,然后在处理程序中返回彩色CBrush:

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

#2


4  

This can also be done without deriving from CEdit:

这也可以在不从CEdit派生的情况下完成:

  1. Add ON_WM_CTLCOLOR() to your dialog's BEGIN_MESSAGE_MAP() code block.
  2. 将ON_WM_CTLCOLOR()添加到对话框的BEGIN_MESSAGE_MAP()代码块中。

  3. Add OnCltColor() to your dialog class:

    将OnCltColor()添加到对话框类:

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

    像这样实现OnCtlColor():

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