当我使用 GetTextExtentExPoint 计算字符串的范围时出现问题
我创建了一个字体对象并将其选择到设备内容中,然后使用WIN32 API GetTextExtentExPoint计算字符串的范围,但我得到的范围是使用系统默认字体时的范围。 例如,当我使用系统默认字体时,字符串的范围是 36 像素宽度和 16 像素高度,而使用我创建的字体时,字符串的范围是 72 像素宽度和 24 像素高度。但是,无论使用系统默认字体还是我创建的字体,我总是得到 36 像素。 我的代码有什么问题?
代码:
HDC hDC = GetDC();
ATLASSERT(hDC);
HFONT _hFontTitle = 0;
HFONT hSysFont = (HFONT)GetCurrentObject(hDC, OBJ_FONT);
ATLASSERT(hSysFont);
LOGFONT lf;
if(0 == GetObject(hSysFont, sizeof(LOGFONT), &lf))
_hFontTitle = CreateFont(16, 12, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, FIXED_PITCH|FF_DONTCARE, _T("Fixedsys"));
else{
lf.lfHeight = 16;
lf.lfWidth = 12;
lf.lfWeight = FW_BOLD;
_hFontTitle = CreateFontIndirect(&lf);
ATLASSERT(_hFontTitle);
}
HFONT _hFontContent = 0;
HFONT hSysFont = (HFONT)GetCurrentObject(hDC, OBJ_FONT);
LOGFONT lf;
if(0 == GetObject(hSysFont, sizeof(LOGFONT), &lf))
_hFontContent = CreateFont(12, 9, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, FIXED_PITCH|FF_DONTCARE, _T("Fixedsys"));
else{
lf.lfHeight = 12;
lf.lfWidth = 9;
lf.lfWeight = FW_NORMAL;
_hFontContent = CreateFontIndirect(&lf);
ATLASSERT(_hFontContent);
}
SIZE sizeTitle = TextMetricsHelper::GetTextLayout(hDC, _szTitle.c_str(), _szTitle.size(), _hFontTitle);
SIZE sizeContent = TextMetricsHelper::GetTextLayout(hDC, _szContent.c_str(), _szContent.size(), _hFontContent);
虽然 GetTextLayout 是:
SIZE GetTextLayout(HDC hDC, LPCTSTR lpszText, unsigned int cbText, HFONT hFont)
{
//RECT rcText = {0, 0, 8, 10};
HFONT hOldFont = (HFONT)SelectObject(hDC, (HGDIOBJ)hFont);
SIZE textSize;
GetTextExtentPoint32(hDC, lpszText, cbText, &textSize);
//GetTextExtentExPoint(hDC, lpszText, cbText, 0, 0, 0, &sizeOfTitle);
//DrawText(hDC, lpszText, cbText, &rcText, DT_CALCRECT);
SelectObject(hDC, hOldFont);
return textSize;
}
I created a font object and selected it into the device content, then calculate the extent of the string using WIN32 API GetTextExtentExPoint, but the extent i got is the extent while using system default font.
For example, when i using system default font, the extent of the string is 36 pixel width and 16 pixel height, and 72 pixel width and 24 pixel height while using the font i created. But, i always got 36 pixel no mater using system default font or the font i created.
What's the problem with my codes?
Codes:
HDC hDC = GetDC();
ATLASSERT(hDC);
HFONT _hFontTitle = 0;
HFONT hSysFont = (HFONT)GetCurrentObject(hDC, OBJ_FONT);
ATLASSERT(hSysFont);
LOGFONT lf;
if(0 == GetObject(hSysFont, sizeof(LOGFONT), &lf))
_hFontTitle = CreateFont(16, 12, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, FIXED_PITCH|FF_DONTCARE, _T("Fixedsys"));
else{
lf.lfHeight = 16;
lf.lfWidth = 12;
lf.lfWeight = FW_BOLD;
_hFontTitle = CreateFontIndirect(&lf);
ATLASSERT(_hFontTitle);
}
HFONT _hFontContent = 0;
HFONT hSysFont = (HFONT)GetCurrentObject(hDC, OBJ_FONT);
LOGFONT lf;
if(0 == GetObject(hSysFont, sizeof(LOGFONT), &lf))
_hFontContent = CreateFont(12, 9, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, FIXED_PITCH|FF_DONTCARE, _T("Fixedsys"));
else{
lf.lfHeight = 12;
lf.lfWidth = 9;
lf.lfWeight = FW_NORMAL;
_hFontContent = CreateFontIndirect(&lf);
ATLASSERT(_hFontContent);
}
SIZE sizeTitle = TextMetricsHelper::GetTextLayout(hDC, _szTitle.c_str(), _szTitle.size(), _hFontTitle);
SIZE sizeContent = TextMetricsHelper::GetTextLayout(hDC, _szContent.c_str(), _szContent.size(), _hFontContent);
While GetTextLayout is:
SIZE GetTextLayout(HDC hDC, LPCTSTR lpszText, unsigned int cbText, HFONT hFont)
{
//RECT rcText = {0, 0, 8, 10};
HFONT hOldFont = (HFONT)SelectObject(hDC, (HGDIOBJ)hFont);
SIZE textSize;
GetTextExtentPoint32(hDC, lpszText, cbText, &textSize);
//GetTextExtentExPoint(hDC, lpszText, cbText, 0, 0, 0, &sizeOfTitle);
//DrawText(hDC, lpszText, cbText, &rcText, DT_CALCRECT);
SelectObject(hDC, hOldFont);
return textSize;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对创建字体等了解不多,但我注意到您的
CreateFont
行都将其结果分配给同一个变量_hFontTitle
。这是意图吗?Don't know much about creating fonts and so on, but I notice that both your
CreateFont
lines assign their result to the same variable_hFontTitle
. Is this the intention ?