⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cautofont.cpp

📁 简单的COM 实际例子 用法1
💻 CPP
字号:
//
// CAutoFont.cpp
//

#include <windows.h>
#include "CAutoFont.h"

////////////////////////////////////////////////////////////////////////////////
CAutoFont::CAutoFont()
{
	mDC      = NULL;
	mNewFont = NULL;
	mOldFont = NULL;
	DefaultFont();
}

CAutoFont::CAutoFont(LOGFONT inFont)
{
	mDC      = NULL;
	mNewFont = NULL;
	mOldFont = NULL;
	CreateFont(inFont);
}

CAutoFont::~CAutoFont()
{
	RestoreToDC();
}

void CAutoFont::DefaultFont(void)
{
	mLogFont.lfHeight      = -12;
	mLogFont.lfWidth       = 0;
	// Specifies the angle in tenths of degrees
	// If rotation font required, lfEscapement and lfOrientation must be the same
	mLogFont.lfEscapement  = 0;
	mLogFont.lfOrientation = 0;
	mLogFont.lfWeight      = FW_NORMAL;
	mLogFont.lfItalic      = 0;
	mLogFont.lfUnderline   = 0;
	mLogFont.lfStrikeOut   = 0;
	mLogFont.lfCharSet        = ANSI_CHARSET;
	mLogFont.lfOutPrecision   = OUT_DEFAULT_PRECIS;
	mLogFont.lfClipPrecision  = CLIP_DEFAULT_PRECIS;
	mLogFont.lfQuality        = PROOF_QUALITY;
	mLogFont.lfPitchAndFamily = VARIABLE_PITCH | FF_ROMAN;
	strcpy(mLogFont.lfFaceName, "Arial");
}

void CAutoFont::CreateFont(LOGFONT inFont)
{
	RestoreToDC();

	mLogFont = inFont;
	mNewFont = CreateFontIndirect(&mLogFont);
}

void CAutoFont::CreateFont(const char * inFaceName)
{
	RestoreToDC();

	strcpy(mLogFont.lfFaceName, inFaceName);
	mNewFont = CreateFontIndirect(&mLogFont);
}

void CAutoFont::CreateStockObject(int inIndex)
{
	RestoreToDC();

	// It is not necessary to delete stock objects by calling DeleteObject,
	// but it is not harmful.
	if (inIndex >= OEM_FIXED_FONT && inIndex <= DEFAULT_GUI_FONT)
	{
		mNewFont = (HFONT) GetStockObject(inIndex);
	}
}

void CAutoFont::SelectToDC(HDC inTargetDC)
{
	if (inTargetDC && mNewFont)
	{
		mDC      = inTargetDC;
		mOldFont = (HFONT) SelectObject(mDC, mNewFont);
	}	
}

void CAutoFont::RestoreToDC(void)
{
	if (mOldFont && mDC)
	{
		SelectObject(mDC, mOldFont);
		mOldFont = NULL;
	}
	if (mNewFont)
	{
		DeleteObject(mNewFont);
		mNewFont = NULL;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -