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

来源:互联网 发布:增量内部收益率的算法 编辑:程序博客网 时间:2024/05/19 00:42
[cpp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. #pragma once  
  2.   
  3. /*  
  4. Title:改变MFC CCheckBox控件默认字体的颜色  
  5. Test Environment: VS2013Update3  
  6. Author: kagula  
#pragma once/*Title:改变MFC CCheckBox控件默认字体的颜色Test Environment: VS2013Update3Author: kagula
[cpp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  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();};


[cpp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  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"// CCheckBoxIMPLEMENT_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 handlersvoid 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 0