CCheckBox控件默认背景和字体颜色

  1. #pragma once  
  2.   
  3. /*  
  4. Title:改变MFC CCheckBox控件默认字体的颜色  
  5. Test Environment: VS2013Update3  
  6. Author: kagula  
#pragma once

/*
Title:改变MFC CCheckBox控件默认字体的颜色
Test Environment: VS2013Update3
Author: kagula
  1. LastUpdateDate:2014-10-20  
  2. */  
  3. class CCheckBox : public CButton  
  4. {  
  5.     DECLARE_DYNAMIC(CCheckBox)  
  6.   
  7. public:  
  8.     CCheckBox();  
  9.     virtual ~CCheckBox();  
  10.   
  11. protected:  
  12.     DECLARE_MESSAGE_MAP()  
  13. public:  
  14.     COLORREF m_clrFore;  
  15.     COLORREF m_clrBK;  
  16.     int      m_fontSize;  
  17.     afx_msg void OnPaint();  
  18. };  
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();
};


  1. // CheckBox.cpp : implementation file  
  2. //  
  3.   
  4. #include “stdafx.h”  
  5. #include “cat8637_brand.h”  
  6. #include “CheckBox.h”  
  7.   
  8.   
  9. // CCheckBox  
  10.   
  11. IMPLEMENT_DYNAMIC(CCheckBox, CButton)  
  12.   
  13. CCheckBox::CCheckBox()  
  14. {  
  15.     m_clrFore = RGB(127, 127, 127);  
  16.     m_clrBK = RGB(255, 255, 255);  
  17.     m_fontSize = 100;  
  18. }  
  19.   
  20. CCheckBox::~CCheckBox()  
  21. {  
  22. }  
  23.   
  24.   
  25. BEGIN_MESSAGE_MAP(CCheckBox, CButton)  
  26.     ON_WM_PAINT()  
  27. END_MESSAGE_MAP()  
  28.   
  29.   
  30.   
  31. // CCheckBox message handlers  
  32. void CCheckBox::OnPaint()  
  33. {  
  34.     CPaintDC dc(this); // device context for painting  
  35.     // TODO: Add your message handler code here  
  36.     // Do not call CButton::OnPaint() for painting messages  
  37.   
  38.     //Draw box  
  39.     RECT rect;  
  40.     GetClientRect(&rect);  
  41.     rect.right = rect.left + 20;  
  42.     CMFCVisualManager::GetInstance()->DrawCheckBox(  
  43.         &dc  
  44.         , rect  
  45.         , false                               // highlighted  
  46.         , GetCheck() == TRUE ? 1 : 0 // state  
  47.         , true                                // enabled  
  48.         , false                               // pressed  
  49.         );  
  50.   
  51.     //draw text  
  52.     GetClientRect(&rect);  
  53.     rect.left += 20;  
  54.   
  55.     CString text;  
  56.     GetWindowText(text);  
  57.     if (text.GetLength() > 0)  
  58.     {  
  59.         COLORREF oldClrFore = dc.SetTextColor(m_clrFore);  
  60.         COLORREF oldClrBK = dc.SetBkColor(m_clrBK);  
  61.   
  62.         CFont *fontOld = nullptr;  
  63.         if (m_fontSize > 0)  
  64.         {  
  65.             CFont font;  
  66.             font.CreatePointFont(m_fontSize, L”宋体”, &dc);  
  67.   
  68.             fontOld = dc.SelectObject(&font);  
  69.         }  
  70.   
  71.         CSize size = dc.GetTextExtent(text);  
  72.         rect.top = ((rect.bottom - rect.top) - size.cy) / 2 + rect.top;  
  73.         dc.DrawText(text.GetBuffer(), &rect, DT_LEFT);  
  74.   
  75.         if (m_fontSize > 0 && fontOld != nullptr)  
  76.         {  
  77.             dc.SelectObject(fontOld);  
  78.         }  
  79.   
  80.         dc.SetBkColor(oldClrBK);  
  81.         dc.SetTextColor(oldClrFore);  
  82.     }  
  83. }  
// 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); } }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值