////////////////////////////// label.h ////////////////////
#pragma once
// Label
class Label : public CStatic
{
DECLARE_DYNAMIC(Label)
public:
Label();
virtual ~Label();
protected:
DECLARE_MESSAGE_MAP()
private:
CFont* m_pFont;
public:
CFont font;
COLORREF m_clrFont; // 字体颜色
COLORREF m_clrBack; // 背景颜色
BOOL m_bTransparent; // 是否透明
afx_msg void OnPaint();
void SetFont(CString _strFontName, UINT _nFontSize);
};
//////////////////////////////////////////////////////////////
//////////// label.cpp //////////////////
// Label.cpp : 实现文件
//
#include "stdafx.h"
#include "Label.h"
// Label
IMPLEMENT_DYNAMIC(Label, CStatic)
Label::Label()
: m_bTransparent(FALSE)
{
int nCount;
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT));
//设置字体样式
nCount = sizeof(lf.lfFaceName)/sizeof(TCHAR);
_tcscpy_s(lf.lfFaceName, nCount, TEXT("宋体"));
lf.lfHeight = 12;
lf.lfWeight = 2;
lf.lfCharSet = GB2312_CHARSET;
m_pFont = new CFont;
m_pFont->CreateFontIndirect(&lf);
}
Label::~Label()
{
}
BEGIN_MESSAGE_MAP(Label, CStatic)
ON_WM_PAINT()
END_MESSAGE_MAP()
// Label 消息处理程序
void Label::OnPaint()
{
CPaintDC dc(this);
RECT rect;
CString strCaption;
GetWindowText(strCaption);
GetClientRect(&rect);
dc.SetTextColor(m_clrFont);
if (m_bTransparent)
{
dc.SetBkMode(TRANSPARENT);
}
else
{
dc.SetBkColor(m_clrBack);
}
dc.SelectObject(*m_pFont);
dc.DrawText(strCaption, &rect, DT_LEFT);
}
void Label::SetFont(CString _strFontName, UINT _nFontSize)
{
int nCount;
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT));
//设置字体样式
nCount = sizeof(lf.lfFaceName)/sizeof(TCHAR);
_tcscpy_s(lf.lfFaceName, nCount, _strFontName);
lf.lfHeight = _nFontSize;
lf.lfWeight = 2;
lf.lfCharSet = GB2312_CHARSET;
delete m_pFont;
m_pFont = new CFont;
m_pFont->CreateFontIndirect(&lf);
}MFC可以设置字体名称、大小、字体前景色、背景色的Static控件
C++ Label 控制类实现
最新推荐文章于 2025-05-12 19:00:31 发布
本文详细介绍了 C++ 中 Label 控制类的实现过程,包括构造函数、析构函数、消息映射、字体设置和绘制功能。通过实例展示了如何使用 C++ 创建具有自定义字体、颜色和透明度的文本显示组件。
2878

被折叠的 条评论
为什么被折叠?



