今天主要总结一下有关MFC 中静态编辑框(StaticEdit)、编辑框(Edit)和按钮(Button)的背景颜色、字颜色和字体。

我的程序运行结果如下:

由上图我们知道修改的地方有:1、把StaticEdit的背景颜色变成黄色,字体颜色变成蓝色;2、Edit的背景颜色变成黄色,字体变成红色,字体为华文楷体

3、Button的背景颜色为绿色,字体为红色。

1、对StaticEdit控件修改

在0106ChangeColorDlg.h中添加一个变量CBrush m_brush,用来保存控件的背景颜色;

对0106ChangeColorDlg添加一个响应WM_CTLCOLOR消息,在OnCtlColor函数中添加如下代码:

else if(pWnd->GetDlgCtrlID()==IDC_STA)//如果是静态编辑框
{
pDC->SetTextColor(RGB(,,));//修改字体的颜色
pDC->SetBkMode(TRANSPARENT);//把字体的背景变成透明的
return m_brush;//返回背景色
}

2、对Edit控件修改

在OnCtlColor函数中添加如下代码:

if(pWnd->GetDlgCtrlID()==IDC_EDIT1)//如果是编辑框
{
pDC->SetTextColor(RGB(,,));//设置编辑框字体的颜色 pDC->SetBkColor(RGB(,,));//设置字体背景颜色
CFont font;
font.CreatePointFont(,"华文楷体");
pDC->SelectObject(&font);//设置字体
return m_brush; }

3、对Button控件修改

对Button按钮修改需要通过重写DrawItem方法,所以写一个类CSXBtn,继承于CButton类。CSXBtn类实现了鼠标在button和不在button按钮时变换背景色功能。具体代码如下:

void CSXBtn::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
static int i=;
UINT uStyle = BS_DEFPUSHBUTTON; // This code only works with buttons.
ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON); // If drawing selected, add the pushed style to DrawFrameControl.
if (lpDrawItemStruct->itemState & ODS_SELECTED)
uStyle |= DFCS_PUSHED; // Draw the button frame.
::DrawFrameControl(lpDrawItemStruct->hDC, &lpDrawItemStruct->rcItem,
DFC_BUTTON, uStyle); CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC); // Get the button's text.
CString strText;
GetWindowText(strText);
// Draw the button text using the text color red.
CBrush B;
CRect focusRect;
focusRect.CopyRect(&lpDrawItemStruct->rcItem);
DrawFocusRect(lpDrawItemStruct->hDC, (LPRECT)&focusRect);
pDC->Draw3dRect(focusRect, ::GetSysColor(COLOR_BTNHILIGHT), ::GetSysColor(COLOR_BTNSHADOW));
if(m_flag)//判断鼠标是否在button按钮上
{
B.CreateSolidBrush(RGB(,,));
}
else
{
B.CreateSolidBrush(RGB(,,));
}
::FillRect(lpDrawItemStruct->hDC,&focusRect, (HBRUSH)B.m_hObject);
::SetBkMode(lpDrawItemStruct->hDC,TRANSPARENT);
COLORREF crOldColor = ::SetTextColor(lpDrawItemStruct->hDC, RGB(,,));
::DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(),
&lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
::SetTextColor(lpDrawItemStruct->hDC, crOldColor);
} void CSXBtn::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
m_flag=true;
TRACKMOUSEEVENT tme;
tme.cbSize=sizeof(tme);
tme.dwFlags=TME_LEAVE;
tme.hwndTrack=this->m_hWnd;
::_TrackMouseEvent(&tme);
CButton::OnMouseMove(nFlags, point);
Invalidate();
} void CSXBtn::OnMouseLeave()
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
m_flag=false; CButton::OnMouseLeave();
Invalidate();
UpdateWindow();
}

最后,关于WM_MOUSELEAVE的用法请参考:http://www.cnblogs.com/Simon-Sun1988/articles/4209104.html

最新文章

  1. phpcms二次开发中无法获取SESSION的值
  2. jquery ajax详解
  3. react使用过程记录
  4. Oracle中添加新用户并赋予权限
  5. HttpClient4.5 SSL访问工具类
  6. Android测试AsyncTask下载图片
  7. ACM -二分图题目小结
  8. Html笔记(十)XHTML XML
  9. CentOS 7下载地址(ISO文件)
  10. 用bat批处理程序通过DOS命令行删除所有的空文件夹
  11. go语言之行--golang操作redis、mysql大全
  12. C语言学习及应用笔记之四:C语言volatile关键字及其使用
  13. OpenGL——外部读档+异常报错
  14. python基础一之课后作业:编写登录接口
  15. layui 表格组件不能访问连续的属性的解决办法
  16. JavaScript常用,继承,原生JavaScript实现classList
  17. ECharts图表实战经验1:如何设置图表同序列不同数据点的独立颜色值
  18. html5随机背景颜色
  19. File 和 导出jar包
  20. ACE中对于不同类型数据的最大值最小值管理ACE_Numeric_Limits

热门文章

  1. 客户端JavaScript-如何执行
  2. SQL语法
  3. ionic 踩过的坑-基本布局
  4. C#拾遗-边边角角
  5. ueditor编辑器使用
  6. 《UML大战需求分析》阅读笔记04
  7. 掌握Thinkphp3.2.0----模型初步
  8. 高性能Server---Reactor模型
  9. python2.7安装PIL.Image模块
  10. 页面上使用 Thymeleaf 的内联js不当造成了 java.lang.StackOverflowError: null 问题