我正在尝试使用 GetTextExtentPoint32 获取文本字符串的大小。我多次阅读文档并做了一些研究,据我所知,下面的代码应该给我正确的 width
和height
文本的内容。
vFontFamily = "Segoe UI"; //font family
vFontSize = 26; //font size
HDC hdc = GetDC(hwnd);
HFONT hFont = CreateFont(
-MulDiv(vFontSize, GetDeviceCaps(hdc, LOGPIXELSY), 72), //calculate the actual cHeight.
0, 0, 0, // normal orientation
FW_NORMAL, // normal weight--e.g., bold would be FW_BOLD
false, false, false, // not italic, underlined or strike out
DEFAULT_CHARSET, OUT_OUTLINE_PRECIS, // select only outline (not bitmap) fonts
CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY, VARIABLE_PITCH | FF_SWISS, vFontFamily);
SIZE size;
HFONT oldfont = (HFONT)SelectObject(hdc, hFont);
GetTextExtentPoint32(hdc, L"This is Text", wcslen(L"This is Text"), &size);
width = size.cx; //get width
height = size.cy; //get height
SelectObject(hdc, oldfont); //don't forget to select the old.
DeleteObject(hFont); //always delete the object after creating it.
ReleaseDC(hwnd, hdc); //alway reelase dc after using.
不幸的是,它没有给我正确的尺寸。宽度过多至少 10%,高度过多至少 5%。准确的说是This is Text
文本中宽度的结果是 228
高度为62
而更准确的是有点接近 190 x 55
我想。
文本使用GDI+绘制.
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
Graphics g(hdc);
FontFamily theFontFamily(vFontFamily);
Font font(&theFontFamily, vFontSize, FontStyleRegular, UnitPixel);
SolidBrush brush(Color(255, R, G, B));
PointF pointF(0.0f, 0.0f);
TextRenderingHint hint = g.GetTextRenderingHint(); // Get the text rendering hint.
g.SetTextRenderingHint(TextRenderingHintAntiAlias); // Set the text rendering hint to TextRenderingHintAntiAlias.
g.DrawString(L"This is Text", strlen("This is Text"), &font, pointF, &brush);
EndPaint(hwnd, &ps);
return TRUE;
}
我想这可能与我绘制文本的方式有关,因为在绘制消息中,我使用了 Font
而不是HFONT
。但这没有任何意义,对吧?只要我之前设置了正确的字体族和字体大小GetTextExtentPoint32
那么它应该给我文本的确切宽度和高度。你觉得怎么样?
最佳答案
GDI+是GDI的改进,当你用它来渲染文本等时,两者之间存在差异。另一件事是DPI Awareness ,在测量文本时,您还需要确保处理缩放。与使用 GDI+ 绘制时的实际宽度和高度相比,如果您在测量中没有正确处理 DPI,您的方法将得到更大的结果。
尝试使用Graphics::MeasureString看看你是否得到了预期的宽度和高度。以与绘制字符串相同的方式进行操作,就像 AlanBirtles 一样。评论里说。
例如,
HDC hdc = GetDC(hwnd); //get dc of your handle.
Graphics graphics(hdc); //setup graphics.
FontFamily theFontFamily(vFontFamily); //setup your font family.
Font font(&theFontFamily, vFontSize, FontStyleRegular, UnitPixel); //create the font the same way how you do it on your paint message.
PointF pointF(0.0f, 0.0f); //use PointF instead of RectF since thats how you paint it.
RectF boundRect; //setup boundRect to get the width and height.
graphics.MeasureString(text, -1, &font, pointF, &boundRect); //Measure the text of the string
//or
//graphics.MeasureString(L"This is Text", strlen("This is Text"), pointF, &boundRect);
width = boundRect.Width; //get the width of text from boundRect.
height = boundRect.Height; //get the height of text from boundRect.
DeleteObject(&font); //delete the font.
ReleaseDC(LabelHandle, hdc); //always reelase dc after using.
如果您需要更多解释,以下内容可能会对您有所帮助。
How to automatically set the width and height of static control base on its content?
GDI+ font size difference under different processes on same machine
DPI-awareness is really needed?
关于C++ GetTextExtentPoint32 未给出正确的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66795957/