2014-12-02 46 views
0
HDC hdc = GetDC(NULL); 
    LOGFONT lf = { 0 }; 
    lf.lfCharSet = DEFAULT_CHARSET; 
    auto proc = [](const LOGFONT *lpelfe, const TEXTMETRIC *lpntme, 
     DWORD FontType, LPARAM lParam) { 
     s_faceNames_.push_back(lpelfe->lfFaceName); 
     return 1; 
    }; 
    EnumFontFamiliesEx(hdc, &lf, proc, NULL, 0); 
    ReleaseDC(NULL, hdc); 

然後將結果輸出facenames:Windows上的字體「終端」和「@Terminal」之間的連接是什麼?

[21:09:16:324] index = 0, facename: Terminal, lfweigt: 400, italic: false fullname:Terminal, charset:255 (simpletest.cpp:71:SimpleTest::testFontEnum) 
[21:09:16:324] index = 1, facename: @Terminal, lfweigt: 400, italic: false fullname:@Terminal, charset:134 (simpletest.cpp:71:SimpleTest::testFontEnum) 

要使用此兩種字體繪製的Hello World:

HFONT CreateFont(const std::wstring& fontName, int fontSize, int* charset = NULL) 
{ 
    LOGFONT lf; 
    memset(&lf, 0, sizeof(lf)); 
    StringCchCopy(lf.lfFaceName, LF_FACESIZE, fontName.c_str()); 
    if (charset) { 
     lf.lfCharSet = *charset; 
    } else { 
     lf.lfCharSet = DEFAULT_CHARSET; 
    } 

    auto proc = [](const LOGFONT *lpelfe, const TEXTMETRIC *lpntme, 
     DWORD FontType, LPARAM lParam) { 
     LPENUMLOGFONTEX fontEx = (LPENUMLOGFONTEX)(lpelfe); 
     PLOGFONT ret = (PLOGFONT)(lParam); 
     *ret = fontEx->elfLogFont; 
     return 0; 
    }; 
    HDC dc = GetDC(NULL); 
    EnumFontFamiliesEx(dc, &lf, proc, (LPARAM)&lf, 0); 
    ReleaseDC(NULL, dc); 
    lf.lfWidth = 0; 
    lf.lfHeight = -std::abs(fontSize); 
    if (charset) { 
     *charset = lf.lfCharSet; 
    } 
    return CreateFontIndirect(&lf); 
} 

WM_PAINT處理:

case WM_PAINT: 
{ 
    hdc = BeginPaint(hWnd, &ps); 

    WCHAR buff[MAX_PATH] = { 0 }; 
    { 
     int charset = DEFAULT_CHARSET; 
     HFONT hf = CreateFont(L"Terminal", 12, &charset); 
     HFONT hOld = (HFONT)SelectObject(hdc, hf); 
     StringCchPrintf(buff, MAX_PATH, L"hello, world charset: %d", charset); 
     TextOut(hdc, 0, 0, buff, wcslen(buff)); 
     SelectObject(hdc, hOld); 
    } 
    { 
     int charset = DEFAULT_CHARSET; 
     HFONT hf = CreateFont(L"@Terminal", 12, &charset); 
     HFONT hOld = (HFONT)SelectObject(hdc, hf); 
     StringCchPrintf(buff, MAX_PATH, L"hello, world charset: %d", charset); 
     TextOut(hdc, 0, 20, buff, wcslen(buff)); 
     SelectObject(hdc, hOld); 
    } 
    EndPaint(hWnd, &ps); 
    break; 
} 

結果: terminal_font_render_result

呈現的文字是相似的,除了後面有更寬的字符寬度,所以這些字體/字體之間是否有任何命名轉換或鏈接,或者第一個是位圖字體,後面是矢量字體?

+1

http://blogs.msdn.com/b/oldnewthing/archive/2012/07/19/10331400.aspx – 2014-12-02 14:35:09

+0

@HansPassant:thx。 – Jichao 2014-12-03 06:21:33

回答

0
void drawText(HDC hdc, int startx1, const std::wstring& fontName, const std::wstring& text) 
{ 
    int degree = 270; 
    int startx2 = startx1 + 200; 
    int starty = 200; 
    MoveToEx(hdc, startx1, 0, NULL); 
    LineTo(hdc, startx1, 700); 
    MoveToEx(hdc, startx2, 0, NULL); 
    LineTo(hdc, startx2, 700); 

    WCHAR buff[MAX_PATH] = { 0 }; 
    { 
     { 
      int charset = DEFAULT_CHARSET; 
      HFONT hf = CreateFont(fontName.c_str(), 12, degree, &charset); 
      FASSERT(hf); 
      HFONT hOld = (HFONT)SelectObject(hdc, hf); 
      auto str1 = fontName + text; 
      StringCchPrintf(buff, MAX_PATH, str1.c_str(), charset); 
      TextOut(hdc, startx1, starty, buff, wcslen(buff)); 
      SelectObject(hdc, hOld); 
     } 
      { 
       std::wstring fontName2 = L"@" + fontName; 
       int charset = DEFAULT_CHARSET; 
       HFONT hf = CreateFont(fontName2.c_str(), 12, degree, &charset); 
       FASSERT(hf); 
       auto str2 = fontName2.c_str() + text; 
       HFONT hOld = (HFONT)SelectObject(hdc, hf); 
       StringCchPrintf(buff, MAX_PATH, str2.c_str(), charset); 
       TextOut(hdc, startx2, starty, buff, wcslen(buff)); 
       SelectObject(hdc, hOld); 
      } 
    } 
} 

WM_PAINT處理:

case WM_PAINT: 
{ 
    hdc = BeginPaint(hWnd, &ps); 
    drawText(hdc, 200, L"SimSun", L"我跟你什麼仇什麼怨?"); 
    drawText(hdc, 600, L"Termainl", L"What hatred, what emity"); 
    EndPaint(hWnd, &ps); 
    break; 
} 

結果: enter image description here

結論: @simsun是垂直的字體,支持垂直文本; simsun不是垂直字體,不支持垂直文字。

所以simsun只是旋轉文本,沒有做任何轉換,而@simsun做到了。

相關問題