设置vc中MFC的Button颜色和字体,CButton继承类的三色和字体设置

// ButtonColor.cpp : implementation file
//

#include "stdafx.h"
#include "MyButtonFont.h"
#include "ButtonColor.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CButtonColor

CButtonColor::CButtonColor()
{
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;   
}

CButtonColor::~CButtonColor()
{
if ( p_Font )    delete p_Font;        //删除字体
}


BEGIN_MESSAGE_MAP(CButtonColor, CButton)
//{{AFX_MSG_MAP(CButtonColor)
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CButtonColor message handlers

void CButtonColor::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
ModifyStyle(0,BS_OWNERDRAW);
CButton::PreSubclassWindow();
}

void CButtonColor::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
UINT uStyle = DFCS_BUTTONPUSH;

// This code only works with buttons.
ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON);

// If drawing selected, add the pushed style to DrawFrameControl.
if (lpDrawItemStruct->itemState & ODS_SELECTED)
   uStyle |= DFCS_PUSHED;

// Draw the button frame.
::DrawFrameControl(lpDrawItemStruct->hDC, &lpDrawItemStruct->rcItem,
   DFC_BUTTON, uStyle);

// Get the button's text.
CString strText;
GetWindowText(strText);

// Draw the button text using the text color red.
COLORREF crOldColor = ::SetTextColor(lpDrawItemStruct->hDC, m_ForeColor);
::DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(),
   &lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
::SetTextColor(lpDrawItemStruct->hDC, crOldColor);

//===========
CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC );
    m_ButRect = lpDrawItemStruct->rcItem;    //获取按钮尺寸
   ASSERT_VALID(pDC);
    if( m_strText.IsEmpty())
{
        GetWindowText( m_strText );          //获取按钮文本
}

int nSavedDC = pDC->SaveDC();
    VERIFY( pDC );
DrawButtonColor(pDC);
pDC->RestoreDC(nSavedDC);
}
//设置文本颜色
void CButtonColor::SetFontColor(COLORREF color)
{
    m_ForeColor = color;
    Invalidate();
}

//设置按钮文本
void CButtonColor::SetText(CString str)
{
    m_strText = _T("");
    SetWindowText(str);
}

//设置背景颜色
void CButtonColor::SetBackColor(COLORREF color)
{
    m_BackColor = color;
    Invalidate();
}

//设置字体(字体高度、字体名)
void CButtonColor::SetTextFont(int FontHight,LPCTSTR FontName)
{
    if ( p_Font )   
   delete p_Font;    //删除旧字体
   
p_Font = new CFont;
int m_FontSize=0;
m_FontSize= FontHight*8;
    p_Font->CreatePointFont(m_FontSize, FontName );    //创建新字体

CFont nFont;
LOGFONT   gridTitleFont;

    SetFont(p_Font,TRUE);                //设置字体
}

void CButtonColor::DrawButtonColor(CDC *pDC)
{
//调整状态
    if( m_Style==3 )
{
   m_Style = 0;
}

    if( GetStyle() & WS_DISABLED )
    {
   m_Style = 3;   
}

//禁止状态
    //根据状态调整边框颜色和文字颜色
    COLORREF bColor;
COLORREF 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:

{
   CRect itemRect = m_ButRect;
   bColor = m_BackColor;
   //底色为白色
   fColor = GetSysColor(COLOR_3DSHADOW); //在后面会执行一次。
       
  
   //第二颜色为黑色
   pDC->SetTextColor( GetSysColor(COLOR_3DHILIGHT) );
   itemRect.top =m_ButRect.top +20;
   itemRect.left =m_ButRect.left +2;
   pDC->DrawText(m_strText,m_strText.GetLength(),itemRect, DT_WORDBREAK | DT_CENTER);
   break;   //锁定的按钮

    }
    //绘制按钮背景
    CBrush Brush;
    Brush.CreateSolidBrush( m_BackColor );    //背景刷
    //pDC->SelectObject( &Brush );

bColor = this->m_ForeColor;
if (m_Style==3)
{
       bColor=RGB(192,192,192);
}
    CPen Pen;
    Pen.CreatePen(PS_SOLID, 1, bColor );
    pDC->SelectObject( &Pen );
    pDC->RoundRect(&m_ButRect,CPoint(0,0));   //画圆角矩形

    //绘制按钮按下时的边框
    if( m_Style!=2 )
    {
        CRect Rect;
        Rect.SetRect( m_ButRect.left, m_ButRect.top, 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);
    //绘制拥有焦点按钮的虚线框
int bian=3.5;
    if(GetFocus()==this )
    {
        CRect Rect;
        Rect.SetRect(m_ButRect.left+bian, m_ButRect.top+bian, m_ButRect.right-bian, m_ButRect.bottom-bian );
        pDC->DrawFocusRect(&Rect);    //画拥有焦点的虚线框
    }

}

#if !defined(AFX_BUTTONCOLOR_H__6B15BD45_1EB9_4D3E_8274_0587D1AB2892__INCLUDED_)
#define AFX_BUTTONCOLOR_H__6B15BD45_1EB9_4D3E_8274_0587D1AB2892__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// ButtonColor.h : header file
//

/////////////////////////////////////////////////////////////////////////////
// CButtonColor window

class CButtonColor : public CButton
{
// Construction
public:
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;             //字体
CButtonColor();

// Attributes
public:
void SetText(CString str);
    void SetFontColor(COLORREF color);      //设置文本颜色
    void SetBackColor(COLORREF color);        //设置背景颜色
    void SetTextFont(int FontHight,LPCTSTR FontName);   //设置字体
void DrawButtonColor(CDC *pDC);
// Operations
public:

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CButtonColor)
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
protected:
virtual void PreSubclassWindow();
//}}AFX_VIRTUAL

// Implementation
public:
virtual ~CButtonColor();

// Generated message map functions
protected:
//{{AFX_MSG(CButtonColor)
//}}AFX_MSG

DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_BUTTONCOLOR_H__6B15BD45_1EB9_4D3E_8274_0587D1AB2892__INCLUDED_)

调用 方法。在Afx.h里面include文件

this->btFontSet.SetText("Hell,this is Supper Button");
this->btFontSet.SetTextFont(12,"Arial Black");
this->btFontSet.SetFontColor(RGB(0,0,0));

m_BTHand = AfxGetApp()->LoadCursor(IDC_Hand);

BOOL CMyButtonFontDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(pMsg-> message==WM_MOUSEMOVE)
{//鼠标移动消息
   //获得当前鼠标位置
   POINT   pt=pMsg-> pt;
   //转换为客户坐标系
   ScreenToClient(&pt);
   //获得当前位置的控件窗口指针
   CWnd*   pWnd=ChildWindowFromPoint(pt);
   //获得该窗口的ID
   UINT   nCtrlID=pWnd-> GetDlgCtrlID();
   switch(nCtrlID)
   {//判断窗口
   
   case   BT_FontSet:
    //显示帮助光标
    SetCursor(m_BTHand);
    break;

   default:
    break;
   }
}

return CDialog::PreTranslateMessage(pMsg);
}

展开阅读全文

没有更多推荐了,返回首页