修改BUTTON背景颜色.doc
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 修改 BUTTON 背景 颜色
- 资源描述:
-
//定义色彩 const COLORREF CLOUDBLUE = RGB(128, 184, 223); const COLORREF WHITE = RGB(255, 255, 255); const COLORREF BLACK = RGB(1, 1, 1); const COLORREF DKGRAY = RGB(128, 128, 128); const COLORREF LTGRAY = RGB(192, 192, 192); const COLORREF YELLOW = RGB(255, 255, 0); const COLORREF DKYELLOW = RGB(128, 128, 0); const COLORREF RED = RGB(255, 0, 0); const COLORREF DKRED = RGB(128, 0, 0); const COLORREF BLUE = RGB(0, 0, 255); const COLORREF DKBLUE = RGB(0, 0, 128); const COLORREF CYAN = RGB(0, 255, 255); const COLORREF DKCYAN = RGB(0, 128, 128); const COLORREF GREEN = RGB(0, 255, 0); const COLORREF DKGREEN = RGB(0, 128, 0); const COLORREF MAGENTA = RGB(255, 0, 255); const COLORREF DKMAGENTA = RGB(128, 0, 128); //在.h文件定义彩色按钮 CColorButton m_btnUp; //在.cpp文件调用函数着色 VERIFY(m_btnUp.Attach(IDC_BUTTON1, this, RED, WHITE, DKRED)); //CColorButton 类原型 //colorbtn.h #ifndef __COLORBTN_H__ #define __COLORBTN_H__ class CColorButton : public CButton { DECLARE_DYNAMIC(CColorButton) public: CColorButton(); virtual ~CColorButton(); BOOL Attach(const UINT nID, CWnd* pParent, const COLORREF BGColor = RGB(192, 192, 192), // gray button const COLORREF FGColor = RGB(1, 1, 1), // black text const COLORREF DisabledColor = RGB(128, 128, 128), // dark gray disabled text const UINT nBevel = 2 ); protected: virtual void DrawItem(LPDRAWITEMSTRUCT lpDIS); void DrawFrame(CDC *DC, CRect R, int Inset); void DrawFilledRect(CDC *DC, CRect R, COLORREF color); void DrawLine(CDC *DC, CRect EndPoints, COLORREF color); void DrawLine(CDC *DC, long left, long top, long right, long bottom, COLORREF color); void DrawButtonText(CDC *DC, CRect R, const char *Buf, COLORREF TextColor); COLORREF GetFGColor() { return m_fg; } COLORREF GetBGColor() { return m_bg; } COLORREF GetDisabledColor() { return m_disabled; } UINT GetBevel() { return m_bevel; } private: COLORREF m_fg, m_bg, m_disabled; UINT m_bevel; }; #endif //colorbtn.cpp #include "stdafx.h " #include "colorbtn.h " #ifdef _DEBUG #undef THIS_FILE static char BASED_CODE THIS_FILE[] = __FILE__; #endif #ifdef CColorButton #undef CColorButton CColorButton #endif IMPLEMENT_DYNAMIC(CColorButton, CButton) CColorButton::CColorButton() { #if (_MFC_VER < 0x0250) hwndOwner = NULL; #endif } CColorButton::~CColorButton() { } BOOL CColorButton::Attach(const UINT nID, CWnd* pParent, const COLORREF BGColor, const COLORREF FGColor, const COLORREF DisabledColor, const UINT nBevel) { if (!SubclassDlgItem(nID, pParent)) return FALSE; m_fg = FGColor; m_bg = BGColor; m_disabled = DisabledColor; m_bevel = nBevel; return TRUE; } void CColorButton::DrawItem(LPDRAWITEMSTRUCT lpDIS) { CDC* pDC = CDC::FromHandle(lpDIS-> hDC); UINT state = lpDIS-> itemState; CRect focusRect, btnRect; focusRect.CopyRect(&lpDIS-> rcItem); btnRect.CopyRect(&lpDIS-> rcItem); focusRect.left += 4; focusRect.right -= 4; focusRect.top += 4; focusRect.bottom -= 4; const int bufSize = 512; TCHAR buffer[bufSize]; GetWindowText(buffer, bufSize); DrawFilledRect(pDC, btnRect, GetBGColor()); DrawFrame(pDC, btnRect, GetBevel()); DrawButtonText(pDC, btnRect, buffer, GetFGColor()); if (state & ODS_FOCUS) { DrawFocusRect(lpDIS-> hDC, (LPRECT)&focusRect); if (state & ODS_SELECTED){ DrawFilledRect(pDC, btnRect, GetBGColor()); DrawFrame(pDC, btnRect, -1); DrawButtonText(pDC, btnRect, buffer, GetFGColor()); DrawFocusRect(lpDIS-> hDC, (LPRECT)&focusRect); } } else if (state & ODS_DISABLED) { DrawButtonText(pDC, btnRect, buffer, GetDisabledColor()); } } void CColorButton::DrawFrame(CDC *DC, CRect R, int Inset) { COLORREF dark, light, tlColor, brColor; int i, m, width; width = (Inset < 0)? -Inset : Inset; for (i = 0; i < width; i += 1) { m = 255 / (i + 2); dark = PALETTERGB(m, m, m); m = 192 + (63 / (i + 1)); light = PALETTERGB(m, m, m); if ( width == 1 ) { light = RGB(255, 255, 255); dark = RGB(128, 128, 128); } if ( Inset < 0 ) { tlColor = dark; brColor = light; } else { tlColor = light; brColor = dark; } DrawLine(DC, R.left, R.top, R.right, R.top, tlColor); // Across top DrawLine(DC, R.left, R.top, R.left, R.bottom, tlColor); // Down left if ( (Inset < 0) && (i == width - 1) && (width > 1) ) { DrawLine(DC, R.left + 1, R.bottom - 1, R.right, R.bottom - 1, RGB(1, 1, 1));// Across bottom DrawLine(DC, R.right - 1, R.top + 1, R.right - 1, R.bottom, RGB(1, 1, 1)); // Down right } else { DrawLine(DC, R.left + 1, R.bottom - 1, R.right, R.bottom - 1, brColor); // Across bottom DrawLine(DC, R.right - 1, R.top + 1, R.right - 1, R.bottom, brColor); // Down right } InflateRect(R, -1, -1); } } void CColorButton::DrawFilledRect(CDC *DC, CRect R, COLORREF color) { CBrush B; B.CreateSolidBrush(color); DC-> FillRect(R, &B); } void CColorButton::DrawLine(CDC *DC, CRect EndPoints, COLORREF color) { CPen newPen; newPen.CreatePen(PS_SOLID, 1, color); CPen *oldPen = DC-> SelectObject(&newPen); DC-> MoveTo(EndPoints.left, EndPoints.top); DC-> LineTo(EndPoints.right, EndPoints.bottom); DC-> SelectObject(oldPen); newPen.DeleteObject(); } void CColorButton::DrawLine(CDC *DC, long left, long top, long right, long bottom, COLORREF color) { CPen newPen; newPen.CreatePen(PS_SOLID, 1, color); CPen *oldPen = DC-> SelectObject(&newPen); DC-> MoveTo(left, top); DC-> LineTo(right, bottom); DC-> SelectObject(oldPen); newPen.DeleteObject(); } void CColorButton::DrawButtonText(CDC *DC, CRect R, const char *Buf, COLORREF TextColor) { COLORREF prevColor = DC-> SetTextColor(TextColor); DC-> SetBkMode(TRANSPARENT); DC-> DrawText(Buf, strlen(Buf), R, DT_CENTER and DT_VCENTER and DT_SINGLELINE); DC-> SetTextColor(prevColor); } 能够实现的代码: 要想修改CButton类按钮背景颜色和文字颜色,必须利用自绘方法对按钮进行重新绘制。这可以通过定义一个以CButton为基类的新按钮类来实现。以下为具体的实现方法: 加入一个新类,类名:CMyButton,基类:CButton。 在头文件 MyButton.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(COLORREF color); //设置文本颜色 void SetBkColor(COLORREF color); //设置背景颜色 void SetTextFont(int FontHight,LPCTSTR FontName); //设置字体 在 MyButton.cpp 的构造函数中初始化变量: CMyButton::CMyButton() { 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 CMyButton::PreSubclassWindow() { ModifyStyle( 0, BS_OWNERDRAW ); //设置按钮属性为自画式 CButton::PreSubclassWindow(); } PreSubclassWindow()在按钮创建前自动执行,所以我们可以在其中做一些初始工作。这里我只做了一项工作,就是为按钮设置属性为“自绘”式,这样,用户在添加按钮后,就不需设置“Owner draw”属性了。 void CMyButton::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 CMyButton::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 CMyButton::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 CMyButton::OnLButtonDown(UINT nFlags, CPoint point) { m_Style = 2; Invalidate(); //重绘按钮 CButton::OnLButtonDown(nFlags, point); } OnLButtonDown()函数是单击鼠标左键时的消息函数。这里只是重新绘制按钮,具体的单击响应应该在拥有按钮的对话框或视图中进行。 void CMyButton::OnLButtonUp(UINT nFlags, CPoint point) { m_Style = 1; Invalidate(); //重绘按钮 CButton::OnLButtonUp(nFlags, point); } OnLButtonUp()函数是单击鼠标左键后弹起时的消息函数。这里也只是重绘按钮,这样能使按钮在按下和弹起时有所不同,使按钮看上去有动态效果。 接口函数是用 CMyButton类 定义的按钮修改颜色、字体和按钮文字的接口,由以下函数组成: //设置按钮文本 void CMyButton::SetText(CString str) { m_strText = _T(""); SetWindowText(str); } //设置文本颜色 void CMyButton::SetForeColor(COLORREF color) { m_ForeColor = color; Invalidate(); } //设置背景颜色 void CMyButton::SetBkColor(COLORREF color) { m_BackColor = color; Invalidate(); } //设置字体(字体高度、字体名) void CMyButton::SetTextFont(int FontHight,LPCTSTR FontName) { if ( p_Font ) delete p_Font; //删除旧字体 p_Font = new CFont; p_Font->CreatePointFont( FontHight, FontName ); //创建新字体 SetFont( p_Font ); //设置字体 } 由于新字体由 new 生成,必须显式回收,这项工作可以在 CMyButton类 的析构函数中进行: CMyButton::~CMyButton() { if ( p_Font ) delete p_Font; //删除字体 } 这样一个可设置颜色、字体的按钮类就做好了。使用时,先在对话框中放置好按钮,再用 ClassWizard 为按钮添加控制变量,并且将变量的类型设置为 CMyButton。之后,可以用该变量调用接口函数设置按钮颜色和字体。展开阅读全文
咨信网温馨提示:1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:0574-28810668;投诉电话:18658249818。




修改BUTTON背景颜色.doc



实名认证













自信AI助手
















微信客服
客服QQ
发送邮件
意见反馈



链接地址:https://www.zixin.com.cn/doc/2667409.html