開発環境 Microsoft Visual C++ 2010 Express (SP1)
実行環境 Microsoft Windows XP Home Edition (SP3)
プロジェクトの種類 Win32 プロジェクト
プロジェクト名 FontTest
アプリケーションの種類 Windows アプリケーション
追加のオプション 空のプロジェクト
文字セット Unicode

tmExternalLeading 行間最小の高さ
tmInternalLeading 発音符の高さ(tmHeightとtmAscentに含まれる)
tmHeight フォントの高さ tmAscent + tmDescent
tmAscent gjpqyで下にはみ出ない、発音符を含む高さ
tmDescent gjpqyで下にはみ出る高さ

フォント作成時に指定するフォントの高さ
正 = 発音符を含む高さ(tmHeight)
負 = 発音符を含まない高さ(tmHeight - tmInternalLeading)

推奨される最小の行の高さ = tmExternalLeading + tmHeight

FACE_NAME=Arial
FONT_HEIGHT=100
tmHeight=100
tmAscent=80
tmDescent=20
tmInternalLeading=11
tmExternalLeading=3

FACE_NAME=Arial
FONT_HEIGHT=-100
tmHeight=112
tmAscent=90
tmDescent=22
tmInternalLeading=12
tmExternalLeading=3

FACE_NAME=MS 明朝
FONT_HEIGHT=100
tmHeight=100
tmAscent=86
tmDescent=14
tmInternalLeading=0
tmExternalLeading=0

FACE_NAME=MS 明朝
FONT_HEIGHT=-100
tmHeight=100
tmAscent=86
tmDescent=14
tmInternalLeading=0
tmExternalLeading=0

FACE_NAME=メイリオ
FONT_HEIGHT=100
tmHeight=100
tmAscent=71
tmDescent=29
tmInternalLeading=33
tmExternalLeading=0

FACE_NAME=メイリオ
FONT_HEIGHT=-100
tmHeight=150
tmAscent=106
tmDescent=44
tmInternalLeading=50
tmExternalLeading=0

FontTest.cpp
// Unicode
#include <Windows.h>
#include <stdio.h>
 
#define APP_NAME TEXT("FontTest")
//#define FACE_NAME TEXT("Arial")
//#define FACE_NAME TEXT("MS 明朝")
#define FACE_NAME TEXT("メイリオ")
//#define STRING TEXT("Wgjpqy")
#define STRING TEXT("Wgjpqy漢字")
#define FONT_HEIGHT -100
 
// 関数プロトタイプ宣言
void Trace(LPCTSTR format, ...);
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
void OnPaint(HWND hWnd);
void OnCreate(void);
 
// 外部変数構造体
static struct {
	HFONT hFont;
	int nY[3];
} g;
 
//==============================================================================
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)
{
	WNDCLASSEX wcx;
	HWND hWnd;
	MSG msg;
 
	// ウィンドウクラスの登録
	ZeroMemory(&wcx, sizeof wcx);
	wcx.cbSize		= sizeof wcx;
	wcx.style		= CS_HREDRAW | CS_VREDRAW;
	wcx.lpfnWndProc		= WndProc;
	wcx.hInstance		= hInstance;
	wcx.hCursor		= LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW));
	wcx.hbrBackground	= (HBRUSH)(COLOR_WINDOW + 1);
	wcx.lpszClassName	= APP_NAME;
	if (RegisterClassEx(&wcx) == 0) {
		return 0;
	}
 
	// ウィンドウの作成
	hWnd = CreateWindow(
		APP_NAME, APP_NAME,
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, 0,
		CW_USEDEFAULT, 0,
		NULL, NULL, hInstance, NULL);
	if (hWnd == NULL) {
		return 0;
	}
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);
 
	// メッセージループ
	while (GetMessage(&msg, NULL, 0, 0)) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
 
	return msg.wParam;
}
 
//------------------------------------------------------------------------------
void Trace(LPCTSTR format, ...)
{
	va_list arg_ptr;
	TCHAR buffer[256];
	int size;
 
	va_start(arg_ptr, format);
	size = _vsnwprintf_s(buffer, _TRUNCATE, format, arg_ptr);
	va_end(arg_ptr);
	OutputDebugString(buffer);
	if (size < 0) {
		OutputDebugString(TEXT("...\n"));
	}
}
 
//------------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg) {
	case WM_PAINT:
		OnPaint(hWnd);
		return 0;
	case WM_CREATE:
		OnCreate();
		return 0;
	case WM_DESTROY:
		DeleteObject(g.hFont);
		PostQuitMessage(0);
		return 0;
	}
	return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
 
//------------------------------------------------------------------------------
void OnPaint(HWND hWnd)
{
	PAINTSTRUCT ps;
	HDC hdc = BeginPaint(hWnd, &ps);
 
	RECT rc;
	GetClientRect(hWnd, &rc);
	SetBkMode(hdc, TRANSPARENT);
 
	// 補助線
	HPEN hPen = CreatePen(PS_SOLID, 0, RGB(255,0,0));
	HGDIOBJ hPenOld = SelectObject(hdc, hPen);
	for (int n = 0; n < 3; n++) {
		MoveToEx(hdc, 0, g.nY[n], NULL);
		LineTo(hdc, rc.right, g.nY[n]);
	}
	SelectObject(hdc, hPenOld);
	DeleteObject(hPen);
 
	// テスト文字列
	HGDIOBJ hFontOld = SelectObject(hdc, g.hFont);
	TextOut(hdc, 0, 0, STRING, wcslen(STRING));
	SelectObject(hdc, hFontOld);
 
	EndPaint(hWnd, &ps);
}
 
//------------------------------------------------------------------------------
void OnCreate(void)
{
	// フォントの作成
	LOGFONT lf;
	ZeroMemory(&lf, sizeof lf);
	lf.lfHeight = FONT_HEIGHT;
	lf.lfCharSet = DEFAULT_CHARSET;
	wcscpy_s(lf.lfFaceName, FACE_NAME);
	g.hFont = CreateFontIndirect(&lf);
 
	// フォント情報の取得
	HDC hdc = GetDC(NULL);
	HGDIOBJ hFontOld = SelectObject(hdc, g.hFont);
	TEXTMETRIC tm;
	GetTextMetrics(hdc, &tm);
	SelectObject(hdc, hFontOld);
	ReleaseDC(NULL, hdc);
 
	Trace(TEXT("FACE_NAME=%s\n"), FACE_NAME);
	Trace(TEXT("FONT_HEIGHT=%d\n"), FONT_HEIGHT);
	Trace(TEXT("tmHeight=%d\n"), tm.tmHeight);
	Trace(TEXT("tmAscent=%d\n"), tm.tmAscent);
	Trace(TEXT("tmDescent=%d\n"), tm.tmDescent);
	Trace(TEXT("tmInternalLeading=%d\n"), tm.tmInternalLeading);
	Trace(TEXT("tmExternalLeading=%d\n"), tm.tmExternalLeading);
 
	g.nY[0] = tm.tmInternalLeading;
	g.nY[1] = tm.tmHeight - tm.tmDescent;
	g.nY[2] = tm.tmHeight;
}
 
最終更新:2012年11月17日 09:51