您的位置:首页 > 编程语言 > C语言/C++

MFC可以设置字体名称、大小、字体前景色、背景色的Static控件

2013-12-19 18:36 477 查看
////////////////////////////// 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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ label 字体
相关文章推荐