MFC EDIT控件中改变背景色和文字颜色

这两天须要给MFC中的EDIT框改变一下背景颜色,并且因为框比较多,且每次须要变色的框也是随机的,可是个数是肯定的。在网上搜了好多,下面这个是介绍的比较清楚,并且能够用的一种方法。
因为本人用的vs2008,在对话框上右击没有添加事件处理函数一项,且对MFC也不是特别熟悉,因此开始只是在对话框类中重载了onctlcolor()函数,但添加时一直没有效果,最后发现出了只定义该函数外还须要在MAP中添加该函数的映射关系才能正常使用。另外要一次改变多个框的背景的话,须要开辟空间,先把这些框的ID存上,而后在onctlcolor()函数中一一比对。

MFC里画图了,颜色了的真抽象,没点基础好难理解啊,


转自:http://dendrites.blog.163.com/blog/static/165376178201010193214142/
这里介绍的改变文本编辑框的背景颜色的方法,不须要对CEdit生成新的类,步骤以下:
(1) 新建一个基于对话框的MFC应用程序,程序名称为Test

(2) 在对话框上添加两个文本框,ID分别为IDC_EDIT1和IDC_EDIT2

(3) 在CTestDlg的头文件中添加几个成员变量,以下所示:
  1. class CTestDlg : public CDialog  
  2. {  
  3. protected:  
  4. CBrush m_redbrush,m_bluebrush;  
  5. COLORREF m_redcolor,m_bluecolor,m_textcolor;  
  6. };  


(4) 在CTestDlg.cpp文件的BOOL CTestDlg::OnInitDialog()中添加如下代码:

  1. BOOL CTestDlg::OnInitDialog()  
  2. {  
  3. CDialog::OnInitDialog();  
  4.   
  5. // Set the icon for this dialog. The framework does this automatically  
  6. // when the application's main window is not a dialog  
  7. SetIcon(m_hIcon, TRUE);    // Set big icon  
  8. SetIcon(m_hIcon, FALSE);   // Set small icon  
  9.   
  10. m_redcolor=RGB(255,0,0);                      // 红色  
  11. m_bluecolor=RGB(0,0,255);                     // 蓝色  
  12. m_textcolor=RGB(255,255,255);                 // 文本颜色设置为白色  
  13. m_redbrush.CreateSolidBrush(m_redcolor);      // 红色背景色  
  14. m_bluebrush.CreateSolidBrush(m_bluecolor);    // 蓝色背景色  
  15. return TRUE; // return TRUE unless you set the focus to a control  
  16. }  


(5) 右击对话框空白面,选择Event,为WM_CTLCOLOR添加消息响应函数,编辑代码以下:

  1. HBRUSH CTestDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)  
  2. {  
  3. HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);  
  4.   
  5. // TODO: Change any attributes of the DC here  
  6. switch (nCtlColor) //对全部同一类型的控件进行判断  
  7. {  
  8.    // process my edit controls by ID.  
  9. case CTLCOLOR_EDIT:  
  10. case CTLCOLOR_MSGBOX://假设控件是文本框或者消息框,则进入下一个switch  
  11.    switch (pWnd->GetDlgCtrlID())//对某一个特定控件进行判断  
  12.    {      
  13.     // first CEdit control ID  
  14.    case IDC_EDIT1:         // 第一个文本框  
  15.     // here  
  16.     pDC->SetBkColor(m_bluecolor);    // change the background  
  17.     // color [background colour  
  18.     // of the text ONLY]  
  19.     pDC->SetTextColor(m_textcolor); // change the text color  
  20.     hbr = (HBRUSH) m_bluebrush;    // apply the blue brush  
  21.     // [this fills the control  
  22.     // rectangle]  
  23.     break;    
  24.     // second CEdit control ID  
  25.    case IDC_EDIT2:         // 第二个文本框  
  26.     // but control is still  
  27.     // filled with the brush  
  28.     // color!  
  29.     pDC->SetBkMode(TRANSPARENT);   // make background  
  30.     // transparent [only affects  
  31.     // the TEXT itself]  
  32.     pDC->SetTextColor(m_textcolor); // change the text color  
  33.     hbr = (HBRUSH) m_redbrush;     // apply the red brush  
  34.     // [this fills the control  
  35.     // rectangle]  
  36.     break;  
  37.    default:  
  38.     hbr=CDialog::OnCtlColor(pDC,pWnd,nCtlColor);  
  39.     break;  
  40.    }  
  41.    break;  
  42. }  
  43. // TODO: Return a different brush if the default is not desired  
  44. return hbr;  
  45. }  


注:case的类别有如下几种:
CTLCOLOR_BTN 按钮控件
CTLCOLOR_DLG 对话框
CTLCOLOR_EDIT 编辑框
CTLCOLOR_LISTBOX 列表框
CTLCOLOR_MSGBOX 消息框
CTLCOLOR_SCROLLBAR 滚动条
CTLCOLOR_STATIC 静态文本


以上代码能够改变控件的背景颜色,读者能够根据须要修改后变为己用。

做者:菜鸟学编程@点网
地址:http://www.node-net.org/read.php/278.htm
版权全部©转载时必须以连接形式注明做者和原始出处及本声明!
php