This is the first time that I have worked with the Windows API for many years, I came across a situation where I need to do something, which I can’t, with the current Windows programming interface.
According to my research, the “Arial Black” font uses the file arialblk.ttfand there is no file for the “Arial Black Italic” font file, nor for the “Arial Black Bold” font, at least on my Windows 7 computer.
I pasted the program below to show a few lines of text using the "Arial Black" font, by itself, and then italicized and bold. To my surprise, the italic text was rendered normally, and the bold text was made as if it were just “Arial Black”. Then I realized that the same thing happens with MS Word. I also added a screenshot of the Word document overlaid on the result from the code below. What's going on here? Do I have to guess which font file is used in each case? Apparently, the Windows API is not giving me the opportunity to respond. Why mystery?
#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;
}
This is the screenshot I mentioned above:
