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:
[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世界:
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;
}
结果:
渲染的文字类似,除了后面有更宽的字符宽度,这些字体/字体之间是否有任何命名转换或链接,或者第一个是位图字体,后面是矢量字体?
答案 0 :(得分: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;
}
结果:
结论: @simsun是垂直字体,支持垂直文本; simsun不是垂直字体,不支持垂直文本。
所以simsun只是旋转文本,没有做任何转换,而@simsun也做了。