从CStatic派生新类,形成具有可以改变字体、背景颜色 和字体颜色的控件类
2010-04-05 09:40
441 查看
// ColorText_Static.cpp : implementation file // #include "stdafx.h" #include "ColorText_Static.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CColorText_Static CColorText_Static::CColorText_Static() { b_FillBackColor = 0; m_Font = 0; m_DisBackColro = RGB(200, 200, 200); Set_BackColor(GetSysColor(COLOR_3DFACE)); Set_TextColor(0); } CColorText_Static::~CColorText_Static() { if(m_Font) { m_Font->DeleteObject(); delete m_Font; } m_BackBrush.DeleteObject(); m_DisBackBrush.DeleteObject(); } void CColorText_Static::SetFont(LOGFONT *LogFont, BOOL bRedraw) { if(m_Font) m_Font->DeleteObject(); if(m_Font == NULL) m_Font = new CFont(); if(m_Font) { if(!m_Font->CreatePointFontIndirect( LogFont )) { delete m_Font; m_Font = NULL; } } } void CColorText_Static::SetFont(CFont* pFont, BOOL bRedraw) { LOGFONT LogFont; pFont->GetLogFont(&LogFont); SetFont(&LogFont, bRedraw); } void CColorText_Static::SetFont(int nHeight, LPCTSTR fontName, BOOL bRedraw) { if(m_Font) m_Font->DeleteObject(); if(m_Font == NULL) m_Font = new CFont(); if(m_Font) { if(!m_Font->CreatePointFont(nHeight, fontName)) { delete m_Font; m_Font = NULL; } } if(bRedraw && GetSafeHwnd()) Invalidate(); } void CColorText_Static::Set_BackColor(COLORREF cr) { m_BackColor = cr; b_FillBackColor = true; m_BackBrush.DeleteObject(); m_BackBrush.CreateSolidBrush(m_BackColor); m_DisBackBrush.DeleteObject(); m_DisBackBrush.CreateSolidBrush(m_DisBackColro); if(GetSafeHwnd()) Invalidate(); } BEGIN_MESSAGE_MAP(CColorText_Static, CStatic) //{{AFX_MSG_MAP(CColorText_Static) ON_WM_CTLCOLOR_REFLECT() ON_WM_ERASEBKGND() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CColorText_Static message handlers HBRUSH CColorText_Static::CtlColor(CDC* pDC, UINT nCtlColor) { // TODO: Change any attributes of the DC here pDC->SetBkMode(TRANSPARENT); pDC->SetTextColor(m_TextColor); if(m_Font) pDC->SelectObject(m_Font); HBRUSH hBrush = (HBRUSH) m_BackBrush; if(!IsWindowEnabled()) hBrush = (HBRUSH) m_DisBackBrush; return ( hBrush ); // TODO: Return a non-NULL brush if the parent's handler should not be called return NULL; } BOOL CColorText_Static::OnEraseBkgnd(CDC* pDC) { // TODO: 在此添加消息处理程序代码和/或调用默认值 return TRUE; return CStatic::OnEraseBkgnd(pDC); }
//测试代码
//在对话框上添加静态文本框编辑ID为IDC_COLOR_STATIC
// 在头文件里加
#include "ColorText_Static.h"
//关联变量与控件 (在此建立的测试对话框类为CFfDlg)
//FfDlg.h
public:
CColorText_Static m_ColorText;
//FfDlg.cpp
void CFfDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFfDlg)
DDX_Control(pDX, IDC_COLOR_STATIC, m_ColorText);
//}}AFX_DATA_MAP
}
//初始化控件参数
BOOL CFfDlg::OnInitDialog()
{
CDialog::OnInitDialog();
m_ColorText.Set_BackColor(RGB(255,0,0)); //控件背景色
m_ColorText.Set_TextColor(RGB(0,255,0)); //字体颜色
m_ColorText.SetFont(100, _T("宋体")); //设置显示字体和大小
m_ColorText.SetWindowText(_T("测试控件")); //设置显示内容
return true;
}
相关文章推荐
- 从CStatic派生新类,形成具有可以改变字体、背景颜色 和字体颜色的控件类
- UITableViewCell删除按钮的背景颜色,字体大小可以改变吗?
- 根据绑定的值的不同改变gridview里某一单元格的字体或背景颜色
- RadioGroup的Radiobutton点击时改变字体颜色和背景颜色(采用Xml文件设置)
- ALV中背景和字体颜色改变的做法
- c#改变winform窗体 标题的字体大小 背景 和颜色
- 在shell或者perl中改变字体或背景的颜色
- 一个继承于CButton的按钮控件类,实现Button背景色与文字的共存与改变,可以自行设计背景色
- Eclipse调试:改变颜色, 背景与字体大小 和xml字体调整
- Eclipse调试:改变颜色, 背景与字体大小 和xml字体调整
- iOS中通过设置CSS改变WebView字体大小,颜色,背景颜色
- WPF DataGrid 改变单元格字体颜色或单元格背景颜色(一)
- Listview.Subitem.BackColor.ForeColor改变字体颜色和背景
- 动态改变Listview的item背景颜色和item中字体的颜色
- UIsearchBar背景颜色的改变,占位字符的颜色改变,占位字符字体大小改变
- C# winform中,简单实现Listview.Subitem.BackColor.ForeColor改变字体颜色,Listview.Subitem.BackColor 改变背景
- 安卓TextView部分字体改变颜色以及背景
- 改变 CListCtrl、CHeaderCtrl 高度、字体、颜色和背景
- ios - 可以改变系统弹框字体大小以及颜色的方法
- ASP.NET--TreeView中被选中Node改变背景或者字体颜色。