这是我在多年使用 Windows API 的经验中第一次遇到这样的情况,即我需要使用 Windows 当前的编程接口做一些我不能做的事情。
根据我的研究,字体“Arial Black”使用该文件arialblk.ttf
,并且没有字体“Arial Black Italic”的文件,也没有字体“Arial Black Bold”的文件,至少在我的装有 Windows 7 的计算机中。
我在一个程序下面插入了一个程序来显示几行文本,使用字体“Arial Black”本身,然后是斜体和粗体。令我惊讶的是,斜体文本被正常呈现,而粗体文本被呈现为好像它只是“Arial Black”。然后我意识到 MS Word 也会发生同样的事情。我还插入了 Word 文档的屏幕截图,与下面代码的输出叠加。这里发生了什么事 ?我是否必须猜测每种情况下使用的是哪个字体文件?显然 Windows API 没有给我答案的可能性。何为谜团?
#include <Windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, UINT, LONG);
int APIENTRY WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pszCmdLine, int nCmdShow)
{
WNDCLASSEX wndclassx;
wndclassx.cbSize = sizeof(WNDCLASSEX);
wndclassx.style = CS_HREDRAW | CS_VREDRAW;
wndclassx.lpfnWndProc = WndProc;
wndclassx.cbClsExtra = 0;
wndclassx.cbWndExtra = 0;
wndclassx.hInstance = hInstance;
wndclassx.hIcon = nullptr;
wndclassx.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclassx.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wndclassx.lpszMenuName = nullptr;
wndclassx.lpszClassName = L"WndProc";
wndclassx.hIconSm = nullptr;
if( !RegisterClassEx(&wndclassx) ) return 0;
HWND hWnd = CreateWindow(L"WndProc", nullptr, WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, nullptr, nullptr, hInstance, nullptr);
ShowWindow(hWnd, SW_MAXIMIZE);
UpdateWindow(hWnd);
MSG msg;
while( GetMessage(&msg, nullptr, 0, 0) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, UINT wParam, LONG lParam)
{
static HFONT s_hArialBlack, s_hArialBlackItalic, s_hArialBlackBold;
switch ( message )
{
case WM_CREATE:
{
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT));
lf.lfHeight = -MulDiv(20, 96, 72);
wcscpy_s(lf.lfFaceName, LF_FACESIZE, L"Arial Black");
if( !(s_hArialBlack = CreateFontIndirect(&lf)) ) return -1;
lf.lfItalic = true;
if( !(s_hArialBlackItalic = CreateFontIndirect(&lf)) )
{
DeleteObject(s_hArialBlack);
return -1;
}
lf.lfWeight = FW_BOLD;
lf.lfItalic = false;
if( !(s_hArialBlackBold = CreateFontIndirect(&lf)) )
{
DeleteObject(s_hArialBlackItalic);
DeleteObject(s_hArialBlack);
return -1;
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
BeginPaint(hwnd, &ps);
HFONT hFont = (HFONT)SelectObject(ps.hdc, s_hArialBlack);
TextOut(ps.hdc, 20, 10, L"Font Arial Black", 16);
SelectObject(ps.hdc, s_hArialBlackItalic);
TextOut(ps.hdc, 20, 50, L"Font Arial Black Italic", 23);
SelectObject(ps.hdc, s_hArialBlackBold);
TextOut(ps.hdc, 20, 90, L"Font Arial Black Bold", 21);
SelectObject(ps.hdc, hFont);
EndPaint(hwnd, &ps);
}
break;
case WM_DESTROY:
DeleteObject(s_hArialBlackBold);
DeleteObject(s_hArialBlackItalic);
DeleteObject(s_hArialBlack);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
这是我上面提到的屏幕截图: