CButtonEx的实现
要想修改CButton类按钮背景颜色和文字颜色,必须利用自绘方法对按钮进行重新绘制。这可以通过定义一个以CButton为基类的新按钮类来实现。以下为具体的实现方法:
方法一:
加入一个新类,类名:CButtonEx,基类:CButton。
在头文件 CButtonEx.h 中加入以下变量和函数定义:
private:
int m_Style; //按钮形状(0-正常,1-当前,2-按下,3-锁定)
BOOL b_InRect; //鼠标进入标志
CString m_strText; //按钮文字
COLORREF m_ForeColor; //文本颜色
COLORREF m_BackColor; //背景色
COLORREF m_LockForeColor; //锁定按钮的文字颜色
CRect m_ButRect; //按钮尺寸
CFont* p_Font; //字体
void DrawButton(CDC*pDC); //画正常的按钮
// 接口函数
public:
void SetText(CString str);
void SetForeColor(COLORREFcolor); //设置文本颜色
void SetBkColor(COLORREFcolor); //设置背景颜色
void SetTextFont(int FontHight,LPCTSTRFontName); //设置字体
在 CButtonEx.cpp 的构造函数中初始化变量:
CButtonEx::CButtonEx()
{
m_Style =0; //按钮形状风格
b_InRect =false; //鼠标进入标志
m_strText =_T(""); //按钮文字(使用默认文字)
m_ForeColor = RGB(0,0,0); //文字颜色(黑色)
m_BackColor =RGB(243,243,243); //背景色(灰白色)
m_LockForeColor =GetSysColor(COLOR_GRAYTEXT); //锁定按钮的文字颜色
p_Font =NULL; //字体指针
}
用ClassWizard添加下列消息函数:
PreSubclassWindow();
DrawItem();
onMouseMove();
OnLButtonDown();
OnLButtonUp();
在各函数内加入代码:
void CButtonEx::PreSubclassWindow()
{
ModifyStyle( 0, BS_OWNERDRAW); //设置按钮属性为自画式
CButton::PreSubclassWindow();
}
PreSubclassWindow()在按钮创建前自动执行,所以我们可以在其中做一些初始工作。这里我只做了一项工作,就是为按钮设置属性为“自绘”式,这样,用户在添加按钮后,就不需设置“Owner draw”属性了。
void CButtonEx::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC *pDC = CDC::FromHandle( lpDrawItemStruct->hDC);
m_ButRect =lpDrawItemStruct->rcItem; //获取按钮尺寸
if( m_strText.IsEmpty() )
GetWindowText( m_strText); //获取按钮文本
int nSavedDC = pDC->SaveDC();
VERIFY( pDC );
DrawButton( pDC); //绘制按钮
pDC->RestoreDC( nSavedDC );
}
DrawItem()函数是一个关键函数,按钮的绘制工作就在这里进行,它的作用相当于对话框中的OnPaint()函数和视图中的OnDraw()函数。
这里我做了三项工作:获取按钮尺寸、获取按钮文本、绘制按钮。其中绘制工作在自定义函数DrawButton()中完成。以下就是绘制过程:
void CButtonEx::DrawButton(CDC *pDC)
{
//调整状态
if( m_Style==3 ) m_Style = 0;
if( GetStyle() & WS_DISABLED )
m_Style =3; //禁止状态
//根据状态调整边框颜色和文字颜色
COLORREF bColor, fColor; //bColor为边框颜色,fColor为文字颜色
switch( m_Style )
{
case 0: bColor = RGB(192,192,192); fColor =m_ForeColor; break; //正常按钮
case 1: bColor = RGB(255,255,255); fColor =m_ForeColor; break; //鼠标进入时按钮
case 2: bColor = RGB(192,192,192); fColor =m_ForeColor; break; //按下的按钮
case 3: bColor = m_BackColor; fColor =m_LockForeColor; break; //锁定的按钮
}
//绘制按钮背景
CBrush Brush;
Brush.CreateSolidBrush( m_BackColor); //背景刷
pDC->SelectObject( &Brush );
CPen Pen;
Pen.CreatePen(PS_SOLID, 1, bColor );
pDC->SelectObject( &Pen );
pDC->RoundRect(&m_ButRect,CPoint(5,5)); //画圆角矩形
//绘制按钮按下时的边框
if( m_Style!=2 )
{
CRect Rect;
Rect.SetRect(m_ButRect.left+2, m_ButRect.top+1, m_ButRect.right, m_ButRect.bottom );
pDC->DrawEdge( &Rect,BDR_RAISEDINNER, BF_RECT ); //画边框
}
//绘制按钮文字
pDC->SetTextColor( fColor); //画文字
pDC->SetBkMode( TRANSPARENT );
pDC->DrawText( m_strText, &m_ButRect,DT_SINGLELINE | DT_CENTER
| DT_VCENTER |DT_END_ELLIPSIS);
//绘制拥有焦点按钮的虚线框
if( GetFocus()==this )
{
CRect Rect;
Rect.SetRect(m_ButRect.left+3, m_ButRect.top+2, m_ButRect.right-3, m_ButRect.bottom-2 );
pDC->DrawFocusRect(&Rect ); //画拥有焦点的虚线框
}
}
变量 m_Style 表征当前按钮状态,它的取值为:0-正常,1-当前,2-按下,3-锁定。不同状态下按钮的边框颜色和文字颜色有所不同。m_Style 的值在鼠标响应函数中进行修改。
绘制工作主要利用CDC类的绘图函数完成,主要注意在 m_Style 不同取值下表现出来的差别。
void CButtonEx::onMouseMove(UINT nFlags, CPoint point)
{
if( !b_InRect || GetCapture()!=this) //鼠标进入按钮
{
b_InRect =true; //设置进入标志
SetCapture(); //捕获鼠标
m_Style =1; //设置按钮状态
Invalidate(); //重绘按钮
}
else
{
if (!m_ButRect.PtInRect(point) ) //鼠标离开按钮
{
b_InRect = false; //清除进入标志
ReleaseCapture(); //释放捕获的鼠标
m_Style = 0; //设置按钮状态
Invalidate(); //重绘按钮
}
}
CButton::onMouseMove(nFlags, point);
}
onMouseMove()函数是鼠标移动消息函数,用于判定当前鼠标指针是否在按钮上。b_InRect是个标志,为true表示鼠标指针进入了按钮区域,此时要捕获鼠标,让鼠标命令传送给按钮。当鼠标指针离开按钮时,要清除b_InRect标志,并且释放捕获的鼠标,让其它窗口可以接收鼠标命令。
Invalidate()函数用于更新按钮,它会自动调用DrawItem()函数重新绘制按钮。
设置条件的目的是仅在鼠标指针进入按钮和离开按钮时更新按钮,这样可以防止鼠标在按钮上移动时发生闪烁。
void CButtonEx::OnLButtonDown(UINT nFlags, CPoint point)
{
m_Style = 2;
Invalidate(); //重绘按钮
CButton::OnLButtonDown(nFlags, point);
}
OnLButtonDown()函数是单击鼠标左键时的消息函数。这里只是重新绘制按钮,具体的单击响应应该在拥有按钮的对话框或视图中进行。
void CButtonEx::OnLButtonUp(UINT nFlags, CPoint point)
{
m_Style = 1;
Invalidate(); //重绘按钮
CButton::OnLButtonUp(nFlags, point);
}
OnLButtonUp()函数是单击鼠标左键后弹起时的消息函数。这里也只是重绘按钮,这样能使按钮在按下和弹起时有所不同,使按钮看上去有动态效果。
接口函数是用 CButtonEx类 定义的按钮修改颜色、字体和按钮文字的接口,由以下函数组成:
//设置按钮文本
void CButtonEx::SetText(CString str)
{
m_strText = _T("");
SetWindowText(str);
}
//设置文本颜色
void CButtonEx::SetForeColor(COLORREF color)
{
m_ForeColor = color;
Invalidate();
}
//设置背景颜色
void CButtonEx::SetBkColor(COLORREF color)
{
m_BackColor = color;
Invalidate();
}
//设置字体(字体高度、字体名)
void CButtonEx::SetTextFont(int FontHight,LPCTSTR FontName)
{
if ( p_Font )
deletep_Font; //删除旧字体
p_Font = new CFont;
p_Font->CreatePointFont( FontHight, FontName); //创建新字体
SetFont( p_Font); //设置字体
}
由于新字体由 new 生成,必须显式回收,这项工作可以在 CButtonEx类 的析构函数中进行:
CButtonEx::~CButtonEx()
{
if ( p_Font )
deletep_Font; //删除字体
}
这样一个可设置颜色、字体的按钮类就做好了。使用时,先在对话框中放置好按钮,再用 ClassWizard 为按钮添加控制变量,并且将变量的类型设置为 CButtonEx。之后,可以用该变量调用接口函数设置按钮颜色和字体。
以上测试过,太复杂。又生一类。
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
方法二:
添加dlg类的WM_DRAWITEM消息处理函数
void CBtncolorDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCTlpDrawItemStruct)
{
// TODO: Add your message handler code here and/or call default
if(nIDCtl==IDC_BUTTON1) //checking for the button
{
CDC dc;
RECT rect;
dc.Attach(lpDrawItemStruct ->hDC); // Get theButton DC to CDC
rect = lpDrawItemStruct->rcItem; //Store the Button rect to our local rect.
dc.Draw3dRect(&rect,RGB(255,255,255),RGB(0,0,0));
dc.FillSolidRect(&rect,RGB(100,100,255));//Here youcan define the required color to appear on the Button.
UINT state=lpDrawItemStruct->itemState; //Thisdefines the state of the Push button either pressed or not.
if((state & ODS_SELECTED))
{
dc.DrawEdge(&rect,EDGE_SUNKEN,BF_RECT);
}
else
{
dc.DrawEdge(&rect,EDGE_RAISED,BF_RECT);
}
dc.SetBkColor(RGB(100,100,255)); //Settingthe Text Background color
dc.SetTextColor(RGB(255,0,0)); //Setting the Text Color
TCHARbuffer[MAX_PATH]; //To store the Caption of the button.
ZeroMemory(buffer,MAX_PATH ); //Intializing the buffer to zero
::GetWindowText(lpDrawItemStruct->hwndItem,buffer,MAX_PATH); //Get theCaption of Button Window
dc.DrawText(buffer,&rect,DT_CENTER|DT_VCENTER|DT_SINGLELINE);//Redrawthe Caption of Button Window
dc.Detach(); // Detach the Button DC
}
CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);
}
http://www.lai18.com/content/931475.html
CButtonEx的实现的更多相关文章
- MFC之ListCtrl动态添加按钮
先上效果图: 图中用了一个CListCtrl插件,隐藏了网格线(LVS_EX_GRIDLINES). 添加了“删除”按钮(按钮上贴了图片),选中哪一行则显示在哪一行. 有两种方式创建按钮,一种是直接根 ...
随机推荐
- Linux下通过rm -f删除大量文件时提示"-bash: /bin/rm: Argument list too long"的解决方法
Linux下通过rm -f删除/var/spool/postfix/maildrop/中大量的小文件时提示: "-bash: /bin/rm: Argument list too long& ...
- <转> 30 个有关 Python 的小技巧
目录[+] 1.1 拆箱 1.2 拆箱变量交换 1.3 扩展拆箱(只兼容python3) 1.4 负数索引 1.5 切割列表 1.6 负数索引切割列表 1.7指定步长切割列表 1.8 负数步长切割列表 ...
- 全栈project师的悲与欢
从小米辞职出来创业的两个多月里,通过猎头或自己投简历,先后面试了知乎,今日头条,豌豆荚,美团,百度,App Annie,去哪儿,滴滴打车等技术团队,一二面(技术面)差点儿都轻松的过了,三面却没有毕业那 ...
- LDF文件过大的解决办法
检查扎兰屯服务器的时候,发现其中一个分区的原空间有300多个G,但只余下了80多个G.检查了一下,发现某库ldf文件过大,竟然达到了280多个G. 这如何得了,再这样下去,硬盘怎能受得了? 尝试用收缩 ...
- BZOJ 1025: [SCOI2009]游戏( 背包dp )
显然题目要求长度为n的置换中各个循环长度的lcm有多少种情况. 判断一个数m是否是满足题意的lcm. m = ∏ piai, 当∑piai ≤ n时是满足题意的. 最简单我们令循环长度分别为piai, ...
- ModelAndView
我给你改一下public ModelAndView showDept(HttpServletRequest req,HttpServletResponse resp,ModelMap model){ ...
- Android 它们的定义View它BounceProgressBar
转载请注明出处:http://blog.csdn.net/bbld_/article/details/41246247 [Rocko's blog] 之前几天下载了非常久没用了的桌面版酷狗来用用的时候 ...
- PHP - 数学运算
第4章 数学运算 学习要点: 1.数值数据类型 2.随机数 3.格式化数据 4.数学函数 在大多数程序设计语言中,数值运算都是最基本的元素之一.数值运算允许程序员完成加法到高级计算等各种操作.尽管PH ...
- MSSQL - SqlDataReader
DataReader对象: ·DataReader对象是一个读取行的只读流的方式,绑定数据时比使用数据集方式性能要高,因为他是只读的,所以如果要对数据库中的数据进行修改就需要借助 将所做的修改保存到数 ...
- ios9 之后 配置百度地图出现的错误
链接地址:http://blog.csdn.net/u011303663/article/details/48596315 错误 ld: '/Users/xxx/Documents/xxx/Baidu ...