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 ...
随机推荐
- css随笔1
1.简单清除浏览器样式 *{ padding: 0px; margin: 0px; } 2.得到屏幕范围的div html,body{ width: 1 ...
- POJ1390Blocks(DP+好题+抽空再来理解理解)
Blocks Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4744 Accepted: 1930 Descriptio ...
- 创建Mat对象的几种方法
1.Mat的构造函数 Mat M(行数,列数,数据类型,通道数) eg:M(2,2, CV_8UC3, Scalar(0,0,255)). 2.利用Mat的Create()函数.Mat M; M.cr ...
- linux下libevent安装
wget http://monkey.org/~provos/libevent-1.4.13-stable.tar.gz tar –xzvf libevent-1.4.13-stable.tar.gz ...
- 12月15日下午Smarty模板函数
1.{$var=...} 这是{assign}函数的简写版,你可以直接赋值给模版,也可以为数组元素赋值. <{$a = 10}><!--赋值语句--> <{$a}> ...
- ORA-01438: 值大于为此列指定的允许精度
Number的数据声明如下:表示 作用 说明Number(p, s) 声明一个定点数 p(precision)为精度,s(scale)表示小数点 ...
- github怎么退出组织和删除自己创建的组织
1. 点击头像,进入settings 2. 点击左侧菜单中的 Organizations 切换到Origanizations后,右侧面板中会出现所有的oragnizations,我这里只有一个,是我自 ...
- Linux平台开发指南
声明:以下内容摘自http://www.me115.com/post/25.html 以下技术和工具是Linux平台下工作的基础,熟练掌握: C++ 工作语言,重要性不言而喻: 入门: <C++ ...
- jaee开发起步:tomcat服务器的配置
1.将下载下来的apache-tomcat-6.0.13.zip解压到任意文件夹. (打开tomcat官网,选择下载tomcat6.x.zip版本的tomcat不需要安装,直接解压并配置一下环境变量就 ...
- Reverse Integer LeetCode Java
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 public cl ...