2015-07-01 116 views
2

我正在將舊的控制檯應用程序轉換爲Win32,並且想要從控制檯複製字體。現有的代碼庫迫使我使用C/C++。我正在嘗試使用CreateFont和CreateFontIndirect構造一個等價物。在Win32應用程序中複製DOS控制檯字體(CP437)

控制檯字體設置爲:

我想我明白,光柵字體不TTF,而不是直接支持,感謝這個帖子,How to use DOS font in WinForms application和仲裁者的答案。

我想構造一個固定的字體,匹配12像素高度和8像素寬度。

以下是我嘗試過的一些代碼。

HDC hdc = GetDC(w_child); 
// "A 12-point font is 16 pixels tall." -- https://msdn.microsoft.com/en-us/library/windows/desktop/ff684173(v=vs.85).aspx 
// "An n-point font is 4/3*n pixels tall"? 
// I want 12 pixels tall, so 9-point, right? 
int PointSize = 9; 
int nHeight = -MulDiv(PointSize, GetDeviceCaps(hdc, LOGPIXELSY), 72); 
ReleaseDC(w_child, hdc); 

HFONT hf = CreateFont(
    -12,  //nHeight,   // Logical height 
    0,  //nHeight * 2/3, // Logical avg character width 
    0,       // Angle of escapement (0) 
    0,       // Baseline angle (0) 
    FW_DONTCARE,    // Weight (0) 
    FALSE,      // Italic (0) 
    FALSE,      // Underline (0) 
    FALSE,      // Strikeout (0) 
    ANSI_CHARSET,    // Character set identifier ?? 
    OUT_DEFAULT_PRECIS,   // Output precision 
    CLIP_DEFAULT_PRECIS,  // Clip precision (0) 
    DEFAULT_QUALITY,   // Output quality 
    FIXED_PITCH,    // Pitch and family 
    "Lucida Console"   // Pointer to typeface name string 
    //"Terminal" 
    //"Courier New" 
); 
*/ 

// Getting stock font, creating an indirect as a logical modification, 
// seems to work better. 

// ANSI_FIXED_FONT, with lf.lfHeight = 10, results in something clear and 
// readable, but a little too large. 
// And changing lfHeight seems to have no impact. 
HFONT hf = (HFONT)GetStockObject(ANSI_FIXED_FONT); 

// SYSTEM_FIXED_FONT at lfHeight = 10 is way too big 
HFONT hf = (HFONT)GetStockObject(SYSTEM_FIXED_FONT); 

// DEVICE_DEFAULT_FONT at lfHeight = 8 is way too big. 
HFONT hf = (HFONT)GetStockObject(DEVICE_DEFAULT_FONT); 

LOGFONT lf; 
GetObject(hf, sizeof(LOGFONT), &lf); 
lf.lfHeight = 12; 
HFONT nf = CreateFontIndirect(&lf); 
SendMessage(w_child, WM_SETFONT, (WPARAM)nf, TRUE); 
+0

如果你的系統字體,則* w_child *窗口可能不處理WM_SETFONT消息。這並不罕見,只有內置的Windows控件才能處理它。在窗口過程的WM_PAINT消息處理程序中使用SelectObject()在繪製之前選擇字體。 –

+0

@HansPassant:我正在使用內置的編輯器。我非常希望不必滾動自己的控件並編寫自己的WM_PAINT。但我擔心EDIT控件會忽略我的WM_SETFONT –

+0

如果你想讓某人爲你調試你的代碼,那麼你將不得不發佈更好的repro代碼。 –

回答

0

好像在我的處境成功:

CreateFont(12, 8, 0, 0, 0, FALSE, 0, 0, OEM_CHARSET, OUT_RASTER_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FIXED_PITCH, L"System"); 
相關問題