// 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控件
最新推荐文章于 2021-10-26 14:35:14 发布