2012-06-21 2 views
0

Windows에서 알 수없는 "Blah"라는 글꼴 이름을 사용하는 "FamilyName"을 알고 싶습니다. 아래에서 볼 수 있듯이GetOutlineTextMetrics() 함수가 존재하지 않는 글꼴 facename에 대해 0을 반환하는 이유는 무엇입니까?

, 폰트가 정상적으로 생성 (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_CREATEs_hFont = (HFONT)SelectObject(hDC, s_hFont)이 있어야합니다. 출력을 변경하지 않았습니다. 그래도. 그러나 최종 결과는 동일합니다. GetOutlineTextMetrics() 함수는 0을 계속 반환합니다.

답변

0

GetOutlineTextMetrics()는 트루 타입 글꼴에서만 작동합니다. 문제는, 당신이 요구하지 않았다는 것입니다. 글꼴 매퍼는 트루 타입 글꼴로 사용자의 요청을 대체 할 이유가 없습니다.

은 매퍼는 항상 TT 글꼴을 반환 주장에 의해 문제를 해결 :

 wcscpy_s(lf.lfFaceName, LF_FACESIZE, L"Blah"); 
     lf.lfOutPrecision = OUT_TT_ONLY_PRECIS; 
+0

두 답변을 모두 수락했을 것입니다. 그러나 다른 하나가 먼저왔다. 답장을 보내 주셔서 감사합니다. – WaldB

2

내 수정 구슬은 DC에 선택한 글꼴이 트루 타입 글꼴 아닙니다 제안한다.

MSDN의 일부 버전에서는 GetLastError을 호출하여 기능이 실패한 이유를 알 수 있지만 MSDN의 다른 버전 (이후 버전)에서는 해당 문장이 제거되었습니다.

+0

나는 그것을 잊었다. 고마워. 내가 처음 왔기 때문에 나는 대답을 받아 들일 것이다. – WaldB

+0

실제로 다른 답변은 몇 초 전에 나왔습니다. (또한 더 많은 정보가 있습니다.) – ymett

+0

성실을 고맙게 생각합니다. 다른 대답을 수락합니다. – WaldB

관련 문제