MFC定义圆角矩形按钮

使用仅需两步!

第一步:

CButton替换为CBtnNoImg。

第二步:

设置各个状态的颜色,和字体(字体设置可选)

	{
		CFont *pFont = new CFont();
		pFont->CreateFont(11, //以逻辑单位方式指定字体的高度
			0, //以逻辑单位方式指定字体中字符的平均宽度
			0, //指定偏离垂线和X轴在显示面上的夹角(单位:0.1度)
			0, //指定字符串基线和X轴之间的夹角(单位:0.1度)
			FW_NORMAL, //指定字体磅数
			FALSE, //是不是斜体
			FALSE, //加不加下划线
			0, //指定是否是字体字符突出
			ANSI_CHARSET, //指定字体的字符集
			OUT_DEFAULT_PRECIS, //指定所需的输出精度
			CLIP_DEFAULT_PRECIS, //指定所需的剪贴精度
			DEFAULT_QUALITY, //指示字体的输出质量
			DEFAULT_PITCH|FF_SWISS, //指定字体的间距和家族
			_T("宋体") //指定字体的字样名称
			);
		m_btnIWantCheck.Init(RGB(255,255,255),
			RGB(255,136,0),RGB(0,129,204),RGB(255,110,0),RGB(127,127,127));
		m_btnIWantCheck.SetFont(pFont);
	}


文件清单:

#pragma once


// CBtnNoImg
/*
测试环境:vs2010sp1
最后更新:2015-03-20 by kagula
*/

class CBtnNoImg : public CButton
{
	DECLARE_DYNAMIC(CBtnNoImg)

public:
	CBtnNoImg();
	virtual ~CBtnNoImg();

protected:
	DECLARE_MESSAGE_MAP()
public:
	void Init(COLORREF clrFont, COLORREF clrBKFocus, COLORREF clrBKUnfocus,COLORREF clrBKSelected, COLORREF clrBKDisable);
	void SetFont(CFont* pFont, BOOL bRedraw = TRUE);

	virtual BOOL Create(LPCTSTR lpszCaption, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID);
	virtual void DrawItem(LPDRAWITEMSTRUCT /*lpDrawItemStruct*/);
	afx_msg BOOL OnEraseBkgnd(CDC* pDC);
private:
	int         m_nCtrlState;  
	CFont*      m_pFont;  

	COLORREF    m_clrFont;  
	COLORREF    m_clrBKFocus;
	COLORREF    m_clrBKUnfocus;
	COLORREF    m_clrBKSelected;
	COLORREF    m_clrBKDisable;

	bool        m_bTracking;

	void        DrawRoundRect(CDC &dc, int radius, CRect rect, COLORREF clrBK);
public:
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
	afx_msg void OnMouseHover(UINT nFlags, CPoint point);
	afx_msg void OnMouseLeave();
	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
};

// BtnNoImg.cpp : implementation file
//

#include "stdafx.h"
#include "BtnNoImg.h"


#define CTRL_NOFOCUS  0x101  
#define CTRL_FOCUS    0x102
#define CTRL_SELECTED 0x103
// CBtnNoImg

IMPLEMENT_DYNAMIC(CBtnNoImg, CButton)

CBtnNoImg::CBtnNoImg()
{
	m_pFont = new CFont;
	m_pFont->CreateFont(15, //以逻辑单位方式指定字体的高度
		0, //以逻辑单位方式指定字体中字符的平均宽度
		0, //指定偏离垂线和X轴在显示面上的夹角(单位:0.1度)
		0, //指定字符串基线和X轴之间的夹角(单位:0.1度)
		FW_NORMAL, //指定字体磅数
		FALSE, //是不是斜体
		FALSE, //加不加下划线
		0, //指定是否是字体字符突出
		ANSI_CHARSET, //指定字体的字符集
		OUT_DEFAULT_PRECIS, //指定所需的输出精度
		CLIP_DEFAULT_PRECIS, //指定所需的剪贴精度
		DEFAULT_QUALITY, //指示字体的输出质量
		DEFAULT_PITCH|FF_SWISS, //指定字体的间距和家族
		_T("宋体") //指定字体的字样名称
		);

	m_clrFont = RGB(0, 0, 0);  
	m_bTracking = false;
}

CBtnNoImg::~CBtnNoImg()
{
	if (m_pFont != NULL)  
	{  
		m_pFont->DeleteObject();  

		delete m_pFont;  
		m_pFont = NULL;  
	}
}


BEGIN_MESSAGE_MAP(CBtnNoImg, CButton)
	ON_WM_ERASEBKGND()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEHOVER()
	ON_WM_MOUSELEAVE()
	ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()



// CBtnNoImg message handlers




BOOL CBtnNoImg::Create(LPCTSTR lpszCaption, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID)
{
	return CButton::Create(lpszCaption, dwStyle, rect, pParentWnd, nID);
}


void CBtnNoImg::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	CDC dc;
	dc.Attach(lpDrawItemStruct->hDC);       //按钮控件DC  

	COLORREF clrBK = RGB(255,255,255);
	switch(m_nCtrlState)  
	{  
	case CTRL_NOFOCUS:  
		clrBK = m_clrBKUnfocus;
		break;  
	case CTRL_FOCUS:  
		clrBK = m_clrBKFocus;
		break;  
	case CTRL_SELECTED:  
		clrBK = m_clrBKSelected;
		break;  
	default:  
		break;  
	}  

	if (IsWindowEnabled()==FALSE)
	{
		clrBK = m_clrBKDisable;
	}

	//draw background
	CRect rect(lpDrawItemStruct->rcItem);
	//dc.FillSolidRect(&rect,clrBK);
	DrawRoundRect(dc,5,rect,clrBK);

	//draw text
	CFont* pOldFont;
	if (m_pFont)
	{
		pOldFont = dc.SelectObject(m_pFont); 
	}
	COLORREF clrOld = dc.SetTextColor(m_clrFont);
	CString strText;
	GetWindowText(strText);
	int oldMode = dc.SetBkMode(TRANSPARENT);
	dc.DrawText(strText, -1, &lpDrawItemStruct->rcItem, DT_CENTER|DT_SINGLELINE|DT_VCENTER);  
	dc.SetBkMode(oldMode);
	dc.SetTextColor(clrOld);
	if (m_pFont)
	{
		dc.SelectObject(pOldFont);  
	}

	dc.Detach();
}


BOOL CBtnNoImg::OnEraseBkgnd(CDC* pDC)
{
	return TRUE;
	//return CButton::OnEraseBkgnd(pDC);
}

void CBtnNoImg::Init(COLORREF clrFont, 
	COLORREF clrBKFocus,
	COLORREF clrBKUnfocus,
	COLORREF clrBKSelected,
	COLORREF clrBKDisable)
{
	SetButtonStyle(BS_OWNERDRAW);

	m_clrFont = clrFont;

	m_clrBKFocus = clrBKFocus;
	m_clrBKUnfocus = clrBKUnfocus;
	m_clrBKSelected = clrBKSelected;
	m_clrBKDisable = clrBKDisable;


	m_nCtrlState = CTRL_NOFOCUS;  
}


void CBtnNoImg::OnLButtonDown(UINT nFlags, CPoint point)
{
	if(m_nCtrlState == CTRL_FOCUS)  
	{  
		m_nCtrlState = CTRL_SELECTED;  
		Invalidate();  
	}  

	CButton::OnLButtonDown(nFlags, point);
}


void CBtnNoImg::OnLButtonUp(UINT nFlags, CPoint point)
{
	if(m_nCtrlState == CTRL_SELECTED)  
	{  
		m_nCtrlState = CTRL_FOCUS;  
		Invalidate();  
	}

	CButton::OnLButtonUp(nFlags, point);
}


void CBtnNoImg::OnMouseHover(UINT nFlags, CPoint point)
{
	if (m_nCtrlState == CTRL_NOFOCUS)  
	{  
		m_nCtrlState = CTRL_FOCUS;  
		Invalidate();  
	}  

	m_bTracking = FALSE;  

	CButton::OnMouseHover(nFlags, point);
}


void CBtnNoImg::OnMouseLeave()
{
	if(m_nCtrlState != CTRL_NOFOCUS)  
	{  
		m_nCtrlState = CTRL_NOFOCUS;  
		Invalidate();  
	}  

	m_bTracking = FALSE;   

	CButton::OnMouseLeave();
}


void CBtnNoImg::OnMouseMove(UINT nFlags, CPoint point)
{
	if (!m_bTracking)    
	{
		TRACKMOUSEEVENT tme;    
		tme.cbSize = sizeof(tme);    
		tme.hwndTrack = m_hWnd;    
		tme.dwFlags = TME_LEAVE|TME_HOVER;    
		tme.dwHoverTime = 1;    
		m_bTracking = _TrackMouseEvent(&tme)==TRUE?true:false;    
	}

	CButton::OnMouseMove(nFlags, point);
}

void CBtnNoImg::DrawRoundRect( CDC &dc, int radius, CRect rect, COLORREF clrBK )
{
	POINT pt;
	pt.x =radius;
	pt.y =radius;

	int penSize = 1;
	CPen pen;
	pen.CreatePen(PS_SOLID,penSize,clrBK);
	CPen* hOldPen = dc.SelectObject(&pen);
	dc.RoundRect(&rect, pt);
	dc.SelectObject(hOldPen);
	rect.DeflateRect(CSize(penSize,penSize));//rect.DeflateRect(CSize(GetSystemMetrics(SM_CXEDGE), GetSystemMetrics(SM_CYEDGE)));
	CBrush brush(clrBK);
	dc.FillRect(rect,&brush );
	brush.DeleteObject();
}

void CBtnNoImg::SetFont( CFont* pFont, BOOL bRedraw)
{
	if (m_pFont)
	{
		delete m_pFont;
	}
	m_pFont = pFont;  

	if(bRedraw)  
		Invalidate();
}//end func



你可能感兴趣的:(MFC定义圆角矩形按钮)