0

我想知道 Windows 使用哪个“FamilyName”来创建具有未知名称“Blah”的字体。

如下所示,字体是正常创建的(WM_PAINT 用它来打印函数的返回值GetOutlineTextMetric()

#include <windows.h>
#include <iostream>

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(nullptr, 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_NORMAL);
    UpdateWindow(hWnd);

    MSG msg;
    while( GetMessage(&msg, nullptr, 0, 0) )
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    //  Retorna msg.wParam

    return (int)msg.wParam;
}


LRESULT CALLBACK WndProc (HWND hwnd, UINT message, UINT wParam, LONG lParam)
{
    static HFONT s_hFont;
    static int s_int = -1;
    static TEXTMETRIC s_tm;

    switch ( message )
    {
        case WM_CREATE:
        {
            HDC hDC;
            if( !(hDC = CreateIC(L"Display", nullptr, nullptr, nullptr)) ) return -1;

            LOGFONT lf;
            memset(&lf, 0, sizeof(LOGFONT));

            wcscpy_s(lf.lfFaceName, LF_FACESIZE, L"Blah");

            if( !(s_hFont = CreateFontIndirect(&lf)) )
            {
                DeleteDC(hDC);
                return -1;
            }

            s_hFont = (HFONT)SelectObject(hDC, s_hFont);
            GetTextMetrics(hDC, &s_tm);

            //   Call GetOutlineTextMetrics() with a null buffer address, to get the size of the buffer.
            s_int = GetOutlineTextMetrics(hDC, 0, nullptr);

            s_hFont = (HFONT)SelectObject(hDC, s_hFont);
            DeleteDC(hDC);
        }
        break;

        case WM_PAINT:
        {
            PAINTSTRUCT ps;
            BeginPaint(hwnd, &ps);
            s_hFont = (HFONT)SelectObject(ps.hdc, s_hFont);

            TextOut(ps.hdc, 20, 20, L"Value returned by GetOutlineTextMetrics() function", 50);

            wchar_t buffer[4];
            swprintf(buffer, 4, L"%3d", s_int);
            TextOut(ps.hdc, 20 + 55 * s_tm.tmAveCharWidth, 20, buffer, 3);
            EndPaint(hwnd, &ps);
        }
        break;

        case WM_DESTROY:
        DeleteObject(s_hFont);
        PostQuitMessage(0);
        break;

        default:
        return DefWindowProc(hwnd, message, wParam, lParam);
    }
    return 0;
}

输出

在此处输入图像描述

编辑:我更正的代码中有一个错误。而不是SelectObject(hDC, s_hFont)in WM_CREATE,我应该有s_hFont = (HFONT)SelectObject(hDC, s_hFont)。虽然没有改变输出。但是最终的结果是一样的,就是函数GetOutlineTextMetrics()继续返回0。

4

2 回答 2

2

我的水晶球表明选择到 DC 中的字体不是 TrueType 字体。

某些版本的 MSDN 说您可以调用GetLastError以找出功能失败的原因,但其他(稍后?)版本的 MSDN 已删除该语句。

于 2012-06-21T14:04:18.320 回答
0

GetOutlineTextMetrics() 只能用于 TrueType 字体。问题是,你没有要一个。字体映射器没有理由用 TrueType 字体替换您的请求。

通过坚持映射器始终返回 TT 字体来解决您的问题:

        wcscpy_s(lf.lfFaceName, LF_FACESIZE, L"Blah");
        lf.lfOutPrecision = OUT_TT_ONLY_PRECIS;
于 2012-06-21T14:03:47.080 回答