改变MFC CCheckBox控件默认字体的颜色

#pragma once

/*
Title:改变MFC CCheckBox控件默认字体的颜色
Test Environment: VS2013Update3
Author: kagula
LastUpdateDate:2014-10-20
*/
class CCheckBox : public CButton
{
	DECLARE_DYNAMIC(CCheckBox)

public:
	CCheckBox();
	virtual ~CCheckBox();

protected:
	DECLARE_MESSAGE_MAP()
public:
	COLORREF m_clrFore;
	COLORREF m_clrBK;
	int      m_fontSize;
	afx_msg void OnPaint();
};


// CheckBox.cpp : implementation file
//

#include "stdafx.h"
#include "cat8637_brand.h"
#include "CheckBox.h"


// CCheckBox

IMPLEMENT_DYNAMIC(CCheckBox, CButton)

CCheckBox::CCheckBox()
{
	m_clrFore = RGB(127, 127, 127);
	m_clrBK = RGB(255, 255, 255);
	m_fontSize = 100;
}

CCheckBox::~CCheckBox()
{
}


BEGIN_MESSAGE_MAP(CCheckBox, CButton)
	ON_WM_PAINT()
END_MESSAGE_MAP()



// CCheckBox message handlers
void CCheckBox::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	// TODO: Add your message handler code here
	// Do not call CButton::OnPaint() for painting messages

	//Draw box
	RECT rect;
	GetClientRect(&rect);
	rect.right = rect.left + 20;
	CMFCVisualManager::GetInstance()->DrawCheckBox(
		&dc
		, rect
		, false                               // highlighted
		, GetCheck() == TRUE ? 1 : 0 // state
		, true                                // enabled
		, false                               // pressed
		);

	//draw text
	GetClientRect(&rect);
	rect.left += 20;

	CString text;
	GetWindowText(text);
	if (text.GetLength() > 0)
	{
		COLORREF oldClrFore = dc.SetTextColor(m_clrFore);
		COLORREF oldClrBK = dc.SetBkColor(m_clrBK);

		CFont *fontOld = nullptr;
		if (m_fontSize > 0)
		{
			CFont font;
			font.CreatePointFont(m_fontSize, L"宋体", &dc);

			fontOld = dc.SelectObject(&font);
		}

		CSize size = dc.GetTextExtent(text);
		rect.top = ((rect.bottom - rect.top) - size.cy) / 2 + rect.top;
		dc.DrawText(text.GetBuffer(), &rect, DT_LEFT);

		if (m_fontSize > 0 && fontOld != nullptr)
		{
			dc.SelectObject(fontOld);
		}

		dc.SetBkColor(oldClrBK);
		dc.SetTextColor(oldClrFore);
	}
}


你可能感兴趣的:(改变MFC CCheckBox控件默认字体的颜色)