MFC 按钮如何改变颜色
我们发现想改变对话框的背景颜色是很简单的,但是对话框的背景颜色改变了后,我们发现按钮的颜色没有改变,如下图。

这样做出来的对话框看起来,不是很自然,我们也想把按钮的颜色改变一下。这就用到了按钮的重绘。
按钮的重绘,主要原理是:一把按钮的重绘属性激活,而覆写DrawItem方法。
新建一个MFC类CCustButton类,继承CButton类。
主要代码如下:CustomButton.h
class CCustomButton : public CButton
{
DECLARE_DYNAMIC(CCustomButton) public:
CCustomButton();
virtual ~CCustomButton();
void SetButtonBgColor(COLORREF color);
void SetButtonTextColor(COLORREF color); private:
COLORREF m_bgColor;
COLORREF m_textColor;
BOOL m_bPressed; protected:
DECLARE_MESSAGE_MAP()
afx_msg void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
}; //CustomButton .cpp
IMPLEMENT_DYNAMIC(CCustomButton, CButton) CCustomButton::CCustomButton()
{
m_bgColor = RGB(, , );
m_textColor = RGB(, , );
m_bPressed = FALSE;
} CCustomButton::~CCustomButton()
{
} BEGIN_MESSAGE_MAP(CCustomButton, CButton)
ON_WM_DRAWITEM()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
END_MESSAGE_MAP() // CCustomButton 消息处理程序 void CCustomButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CRect rect;
GetClientRect(rect);
CDC dc;
dc.Attach(lpDrawItemStruct->hDC); UINT state = lpDrawItemStruct->itemState;
CRect focusRect(rect);
focusRect.DeflateRect(, , , );
if ((state & ODS_SELECTED) || (state & ODS_FOCUS)) {
CPen pen(PS_DASHDOTDOT, , RGB(, , ));
CBrush brush;
brush.CreateStockObject(NULL_BRUSH);
dc.SelectObject(&brush);
dc.SelectObject(&pen);
dc.FillSolidRect(rect,m_bgColor);
dc.DrawFocusRect(focusRect);
dc.Draw3dRect(rect, RGB(, , ), RGB(, , ));
} else {
CPen pen(PS_DOT, , m_bgColor);
CBrush brush;
brush.CreateStockObject(NULL_BRUSH);
dc.SelectObject(&brush);
dc.SelectObject(&pen);
dc.FillSolidRect(rect, m_bgColor);
dc.Rectangle(focusRect);
}
dc.DrawEdge(rect, BDR_RAISEDINNER | BDR_RAISEDOUTER, BF_BOTTOMLEFT | BF_TOPRIGHT);
if (m_bPressed) {
dc.DrawFocusRect(focusRect);
dc.DrawEdge(rect, BDR_RAISEDINNER | BDR_SUNKENOUTER, BF_BOTTOMLEFT | BF_TOPRIGHT);
dc.Draw3dRect(rect, RGB(, , ), RGB(, , ));
}
CString strText;
GetWindowText(strText);
dc.SetBkMode(TRANSPARENT);
dc.SetTextColor(m_textColor);
if (state & ODS_DISABLED) {
dc.SetTextColor(RGB(, , ));
}
dc.DrawText(strText, rect, DT_CENTER | DT_SINGLELINE | DT_VCENTER);
dc.Detach();
}
//按钮被按下
void CCustomButton::OnLButtonDown(UINT nFlags, CPoint point)
{
m_bPressed = TRUE; CButton::OnLButtonDown(nFlags, point);
} //按钮被释放
void CCustomButton::OnLButtonUp(UINT nFlags, CPoint point)
{
m_bPressed = FALSE; CButton::OnLButtonDown(nFlags, point);
}
//设置按钮背景的颜色
void CCustomButton::SetButtonBgColor(COLORREF color)
{
m_bgColor = color;
} //设置按钮字体的颜色
void CCustomButton::SetButtonTextColor(COLORREF color)
{
m_textColor = color;
}
将按钮的自绘属性设为true
给按钮添加一个变量
CButton m_bt_OpenFile;
将CButton改成CCustomButton 变成
CCustomButton m_bt_OpenFile;
在dialog里调用
//设置按钮背景色
m_bt_OpenFile.SetButtonBgColor(m_BgColor);
//刷新窗口
UpdateWindow();即可
m_BgColor 为对话框的背景色。
效果如下:

MFC 按钮如何改变颜色的更多相关文章
- VS2010中新控件的编程------颜色按钮类和颜色对话框
(1) 颜色按钮类和颜色对话框 1) 颜色对话框 MFC提供了颜色对话框类CMFCColorDialog进行颜色的选择,系统可以利用DoModal()调用,然后选择相应的颜色. CMFCCo ...
- Atitit 动态按钮图片背景颜色与文字组合解决方案
Atitit 动态按钮图片背景颜色与文字组合解决方案 转换背景颜色,setFont("cywe_img", fontScale, 50, 5) 设置文字大小与坐标 文字分拆,使用字 ...
- android中自定义view---实现竖直方向的文字功能,文字方向朝上,同时提供接口,判断当前touch的是哪个字符,并改变颜色
android自定义view,实现竖直方向的文字功能,文字方向朝上,同时提供接口,判断当前touch的是哪个字符,并改变颜色. 由于时间比较仓促,因此没有对代码进行过多的优化,功能远远不如androi ...
- css 鼠标移动到按钮图片改变;图片换层;鼠标放上透明度改变直到隐藏;
css 鼠标移动到按钮图片改变: 方法一: <style> .pp a { width:575px; height:157px; background:url(1.jpg);/*图片地址* ...
- iOS7 修改导航系统默认返回按钮文字及颜色
//iOS7 修改系统默认返回按钮文字及颜色 UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"返" ...
- 设置datagridview中button按钮的背景颜色
问题:DataGridViewButtonColumn()在datagridview中创建按钮列,如何设置按钮的背景颜色(不是单元格的背景颜色). 回答:可以在dataGridView1_CellPa ...
- UIAlertController 简单修改title以及按钮的字体颜色
苦逼的开发者,最终败给了一个任性的UI,系统原生UIAlertController的按纽颜色必须改.于是,开始了不归路.之前的版本是自己用view写的一个仿系统UIActionSheet,动画感觉都挺 ...
- TextView设置动态改变颜色
通过TextView的setTextColor方法进行文本颜色的设置, 这里可以有3种方式进行设置: 第1种:tv.setTextColor(android.graphics.Color.RED);/ ...
- <hr/>标签改变颜色注意事项
1.css改变颜色 <hr style="border:0;background-color:#093;height:1px;"> 注意: 如果不加border:0 ...
随机推荐
- bzoj3157国王奇遇记(秦九韶算法+矩乘)&&bzoj233AC达成
bz第233题,用一种233333333的做法过掉了(为啥我YY出一个算法来就是全网最慢的啊...) 题意:求sigma{(i^m)*(m^i),1<=i<=n},n<=10^9,m ...
- CGI, FastCGI, WSGI, uWSGI, uwsgi简述
CGI 通用网关接口(Common Gateway Interface/CGI)是一种重要的互联网技术,可以让一个客户端,从网页浏览器向执行在网络服务器上的程序请求数据.CGI描述了服务器和请求处理程 ...
- CTP程序化系统开发(C++ && PHP)
2016-12-13 11:03:52 借助CTP的DEMO(上海期货交易公司提供的), 需要自己在 http://www.simnow.com.cn 上注册账号, 再者,需要下载[博易大师]软件, ...
- TThread.CreateAnonymousThread() 匿名线程对象的应用
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...
- Bootstrap学习笔记博客
本片博客用于记录之后要用到Bootstrap的学习笔记 概括: Bootstrap 是一个用于快速开发 Web 应用程序和网站的前端框架.Bootstrap 是基于 HTML.CSS.JAVASC ...
- maven报brors occurred during the build
原因分析: 此问题一般发生在eclipse保存文件并自动部署时候.本人在写项目的时候,还没等部署好,关闭了了eclipse,结果出现了这种情况.有一种产生此错误的原因是因为此项目不不是由eclipse ...
- mysqldump数据库同步遇到的问题
1.同步数据是遇到 没有 lock database权限,报 " mysqldump: Got error: 1044: Access denied for user 'spider_dat ...
- BZOJ2292——【POJ Challenge 】永远挑战
1.题意:dijkstra模板题,存点模板 #include <queue> #include <cstdio> #include <cstdlib> #inclu ...
- Android的Intent.FLAG_ACTIVITY_CLEAR_TOP无效
转载:http://blog.csdn.net/u011361576/article/details/48626237 今天写代码遇到了一个问题: 当 B - A - B 跳转的时候,使用Intent ...
- PHP语言中使用JSON和将json还原成数组
从5.2版本开始,PHP原生提供json_encode()和json_decode()函数,前者用于编码,后者用于解码. 一.json_encode() 1 2 3 4 <?php $arr = ...