在Win32应用程序中复制DOS控制台字体(CP437)

时间:2015-07-01 14:38:11

标签: c++ winforms winapi fonts

我正在将旧的控制台应用程序转换为Win32,并希望从控制台复制字体。现有的代码库迫使我使用C / C ++。我试图使用CreateFont和CreateFontIndirect来构造一个等效的。

控制台字体设置为:

我想我理解光栅字体不是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);

1 个答案:

答案 0 :(得分:1)

似乎在我的情况下取得了成功:

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