c++ - 为什么函数 GetOutlineTextMetrics() 对于不存在的字体名称返回 0?

标签 c++ winapi fonts

我想知道 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;
}

输出

enter image description here

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

最佳答案

我的 Crystal 球表明在 DC 中选择的字体不是 TrueType 字体。

某些版本的 MSDN 说您可以调用 GetLastError 来找出函数失败的原因,但其他(更高版本?)的 MSDN 版本已经删除了这句话。

关于c++ - 为什么函数 GetOutlineTextMetrics() 对于不存在的字体名称返回 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11139359/

相关文章:

java - 如何从具有多个 TTF 文件的字体系列导入自定义 java.awt.Font? (包括一个例子)

java - itext 字体 UnsupportedCharsetException

c++ - 使用 getsockopt 读取 TCP_NODELAY 返回一个奇怪的值

winapi - Golang : using Windows 10 API/UWP/System. Windows 运行时?

c++ - Win32 事件与信号量

c++ - Critical Section 对象如何适用于多种方法

html - 如何在我的网站上使用字体

c++ - 调整 const 映射键

c++ - 这是内存泄漏吗?试图重置类型类的类成员。 C++

c++ 运行时库选项