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
版权所有©转载时必须以链接形式注明作者和原始出处及本声明!

  • 4
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
MFC,我们可以通过以下几种方式来修改Button件的字体、字体大小、背景背景图片。 1. 修改字体和字体大小: 通过Button件的SetFont函数可以设置字体和字体大小。举例如下: ``` CButton* pBtn = (CButton*)GetDlgItem(IDC_BUTTON1); CFont font; font.CreateFont(16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, _T("Arial")); pBtn->SetFont(&font); ``` 上述例子,创建了一个高度为16的Arial字体,并将其应用到ID为IDC_BUTTON1的Button。 2. 修改背景: 可以通过Button件的SetBkColor函数设置背景。举例如下: ``` CButton* pBtn = (CButton*)GetDlgItem(IDC_BUTTON1); pBtn->SetBkColor(RGB(255, 0, 0)); ``` 上述例子,将ID为IDC_BUTTON1的Button件的背景设置为红。 3. 修改背景图片: 可以通过Button件的SetBitmap函数设置背景图片。举例如下: ``` CButton* pBtn = (CButton*)GetDlgItem(IDC_BUTTON1); HBITMAP hBitmap = (HBITMAP)::LoadImage(AfxGetInstanceHandle(), _T("path_to_image.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION); pBtn->SetBitmap(hBitmap); ``` 上述例子,从文件加载一张位图图片,并将其设置为ID为IDC_BUTTON1的Button件的背景图片。 总结: 通过以上三种方法,我们可以在MFC方便地修改Button件的字体、字体大小、背景背景图片。注意在使用时,需将代码放在相应的初始化函数,如OnInitDialog()。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值