修改BUTTON背景颜色.docx

上传人:b****6 文档编号:3727145 上传时间:2022-11-24 格式:DOCX 页数:10 大小:19.42KB
下载 相关 举报
修改BUTTON背景颜色.docx_第1页
第1页 / 共10页
修改BUTTON背景颜色.docx_第2页
第2页 / 共10页
修改BUTTON背景颜色.docx_第3页
第3页 / 共10页
修改BUTTON背景颜色.docx_第4页
第4页 / 共10页
修改BUTTON背景颜色.docx_第5页
第5页 / 共10页
点击查看更多>>
下载资源
资源描述

修改BUTTON背景颜色.docx

《修改BUTTON背景颜色.docx》由会员分享,可在线阅读,更多相关《修改BUTTON背景颜色.docx(10页珍藏版)》请在冰豆网上搜索。

修改BUTTON背景颜色.docx

修改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;   //锁定的按钮

    }

    //绘制按钮背景

    

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 职业规划

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1