浙江财经大学
信工学院ACM集训队

[Win32]简便字体(直接指定TrueType字体)

本文由 Ocrosoft 于 2018-06-09 21:24:27 发表

这个程序的效果就是说明了两个字差的磅数小的时候,可能显示出来大小是一样的

1.1.ezfont.c

#include <Windows.h>
#include <math.h>
#include "ezfont.h"

// iDeciPtHeight: 磅值*10
// iDeciPtWidth: 全宽,通常为0或同iDeciPtHeight
HFONT EzCreateFont(HDC hdc, TCHAR *szFaceName, int iDeciPtHeight, int iDeciPtWidth, int iAttributes, BOOL fLogRes)
{
	FLOAT cxDpi, cyDpi;
	HFONT hFont;
	LOGFONT lf;
	POINT pt;
	TEXTMETRIC tm;

	SaveDC(hdc);

	SetGraphicsMode(hdc, GM_ADVANCED);
	ModifyWorldTransform(hdc, NULL, MWT_IDENTITY);
	SetViewportOrgEx(hdc, 0, 0, NULL);
	SetWindowOrgEx(hdc, 0, 0, NULL);

	if (fLogRes)
	{
		cxDpi = (FLOAT)GetDeviceCaps(hdc, LOGPIXELSX);
		cyDpi = (FLOAT)GetDeviceCaps(hdc, LOGPIXELSY);
	}
	else
	{
		cxDpi = (FLOAT)(25.4 * GetDeviceCaps(hdc, HORZRES) / GetDeviceCaps(hdc, HORZSIZE));
		cyDpi = (FLOAT)(25.4 * GetDeviceCaps(hdc, VERTRES) / GetDeviceCaps(hdc, VERTSIZE));
	}

	pt.x = (int)(iDeciPtWidth * cxDpi / 72);
	pt.y = (int)(iDeciPtHeight * cyDpi / 72);

	DPtoLP(hdc, &pt, 1);
	lf.lfHeight = -(int)(fabs(pt.y) / 10.0 + 0.5);
	lf.lfWidth = 0;
	lf.lfEscapement = 0;
	lf.lfOrientation = 0;
	lf.lfWeight = iAttributes & EZ_ATTR_BOLD ? 700 : 0;
	lf.lfItalic = iAttributes & EZ_ATTR_ITALIC ? 1 : 0;
	lf.lfUnderline = iAttributes & EZ_ATTR_UNDERLINE ? 1 : 0;
	lf.lfStrikeOut = iAttributes & EZ_ATTR_STRIKEOUT ? 1 : 0;
	lf.lfCharSet = DEFAULT_CHARSET;
	lf.lfOutPrecision = 0;
	lf.lfClipPrecision = 0;
	lf.lfQuality = 0;
	lf.lfPitchAndFamily = 0;

	lstrcpy(lf.lfFaceName, szFaceName);

	hFont = CreateFontIndirect(&lf);

	if (iDeciPtWidth != 0)
	{
		hFont = (HFONT)SelectObject(hdc, hFont);
		GetTextMetrics(hdc, &tm);
		DeleteObject(SelectObject(hdc, hFont));

		lf.lfWidth = (int)(tm.tmAveCharWidth * fabs(pt.x) / fabs(pt.y) + 0.5);
		hFont = CreateFontIndirect(&lf);
	}

	RestoreDC(hdc, -1);
	return hFont;
}

1.2.ezfont.h

#pragma once
HFONT EzCreateFont(HDC hdc, TCHAR *szFaceName, int iDeciPtHeight, int iDeciPtWidth, int iAttributes, BOOL fLogRes);
#define EZ_ATTR_BOLD		1
#define EZ_ATTR_ITALIC		2
#define EZ_ATTR_UNDERLINE	4
#define EZ_ATTR_STRIKEOUT	8

1.3.eztest.c

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

TCHAR szAppName[] = TEXT("EZTest");
TCHAR szTitle[] = TEXT("EZTest: Test of EZFONT");

void PaintRoutine(HWND hwnd, HDC hdc, int cxArea, int cyArea)
{
	HFONT hFont;
	int y, iPointSize;
	LOGFONT lf;
	TCHAR szBuffer[100];
	TEXTMETRIC tm;

	SetMapMode(hdc, MM_ANISOTROPIC);
	SetWindowExtEx(hdc, 1440, 1440, NULL);
	SetViewportExtEx(hdc, GetDeviceCaps(hdc, LOGPIXELSX), GetDeviceCaps(hdc, LOGPIXELSY), NULL);

	y = 0;

	// 从8磅到12磅,每次增加0.1磅
	for (iPointSize = 80; iPointSize <= 120; iPointSize++)
	{
		hFont = EzCreateFont(hdc, TEXT("Times New Roman"), iPointSize, 0, 0, TRUE);
		GetObject(hFont, sizeof(LOGFONT), &lf);
		SelectObject(hdc, hFont);
		GetTextMetrics(hdc, &tm);
		TextOut(hdc, 0, y, szBuffer, 
			wsprintf(szBuffer, TEXT("Times New Roman font of %i. %i points,")
			TEXT("Lf.lfHeight = %i, tm.tmHeight = %i"),
			iPointSize / 10, iPointSize % 10, lf.lfHeight, tm.tmHeight));

		DeleteObject(SelectObject(hdc, GetStockObject(SYSTEM_FONT)));
		y += tm.tmHeight;
	}
}

1.4.main.c

#include <windows.h>
#include "resource.h"
#include "ezfont.h"

// 指示编译器到其他.c文件中寻找定义
extern void PaintRoutine(HWND, HDC, int, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

HINSTANCE hInst;

extern TCHAR szAppName[];
extern TCHAR szTitle[];

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
	PSTR szCmdLine, int iCmdShow)
{
	TCHAR szResource[] = TEXT("FontDemo");
	HWND     hwnd;
	MSG      msg;
	WNDCLASS wndclass;

	hInst = hInstance;

	wndclass.style = CS_HREDRAW | CS_VREDRAW;
	wndclass.lpfnWndProc = WndProc;
	wndclass.cbClsExtra = 0;
	wndclass.cbWndExtra = 0;
	wndclass.hInstance = hInstance;
	wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wndclass.lpszMenuName = szResource;
	wndclass.lpszClassName = szAppName;

	if (!RegisterClass(&wndclass))
	{
		MessageBox(NULL, TEXT("This program requires Windows NT!"),
			szAppName, MB_ICONERROR);
		return 0;
	}

	hwnd = CreateWindow(szAppName, szTitle,
		WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
		CW_USEDEFAULT, CW_USEDEFAULT,
		CW_USEDEFAULT, CW_USEDEFAULT,
		NULL, NULL, hInstance, NULL);

	ShowWindow(hwnd, iCmdShow);
	UpdateWindow(hwnd);

	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	static int cxClient, cyClient;
	HDC hdc;
	PAINTSTRUCT ps;

	switch (message)
	{
	case WM_COMMAND:
		switch (wParam)
		{
		case ID_PRINT:
			MessageBox(hwnd, TEXT("Not available\n"), szAppName, MB_ICONINFORMATION | MB_OK);
			return 0;

		case ID_ABOUT:
			MessageBox(hwnd, TEXT("Font Demonstration Program\n")TEXT("(c)Charles Petzold, 1998"), szAppName, MB_ICONINFORMATION | MB_OK);
			return 0;
		}
		break;

	case WM_SIZE:
		cxClient = LOWORD(lParam);
		cyClient = HIWORD(lParam);
		return 0;

	case WM_PAINT:
		hdc = BeginPaint(hwnd, &ps);

		PaintRoutine(hwnd, hdc, cxClient, cyClient);

		EndPaint(hwnd, &ps);
		return 0;

	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}
	return DefWindowProc(hwnd, message, wParam, lParam);
}

欢迎转载,请保留出处与链接。Ocrosoft » [Win32]简便字体(直接指定TrueType字体)

点赞 (0)or拍砖 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址