C++ 如何在 WIN32 中找到字符串的宽度(以像素为单位)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1126730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find the width of a String (in pixels) in WIN32
提问by Razvi
Can you measure the width of a string more exactly in WIN32 than using the GetTextMetrics function and using tmAveCharWidth*strSize?
与使用 GetTextMetrics 函数和使用 tmAveCharWidth*strSize 相比,您能否在 WIN32 中更精确地测量字符串的宽度?
回答by Nick Meyer
Try using GetTextExtentPoint32. That uses the current font for the given device context to measure the width and height of the rendered string in logical units. For the default mapping mode, MM_TEXT, 1 logical unit is 1 pixel.
尝试使用GetTextExtentPoint32。它使用给定设备上下文的当前字体以逻辑单位测量呈现字符串的宽度和高度。对于默认的映射模式,MM_TEXT,1 个逻辑单元为 1 个像素。
However, if you've changed the mapping mode for the current device context, a logical unit may not be the same as a pixel. You can read about the different mapping modes on MSDN. With the mapping mode, you can convert the dimensions returned to you by GetTextExtentPoint32 to pixels.
但是,如果您更改了当前设备上下文的映射模式,则逻辑单元可能与像素不同。您可以在 MSDN 上阅读不同的映射模式。使用映射模式,您可以将 GetTextExtentPoint32 返回给您的尺寸转换为像素。
回答by Evan Teran
I don't know for certain, but it seems that:
我不确定,但似乎:
HDC hDC = GetDC(NULL);
RECT r = { 0, 0, 0, 0 };
char str[] = "Whatever";
DrawText(hDC, str, strlen(str), &r, DT_CALCRECT);
might work.
可能工作。
回答by Kirill V. Lyadvinsky
VOID Example_MeasureString(HDC hdc) { Graphics graphics(hdc); // Set up the string. WCHAR string[] = L"Measure Text"; Font font(L"Arial", 16); RectF layoutRect(0, 0, 100, 50); RectF boundRect; // Measure the string. graphics.MeasureString(string, 12, &font, layoutRect, &boundRect); // Draw a rectangle that represents the size of the string. graphics.DrawRectangle(&Pen(Color(255, 0, 0, 0)), boundRect); }
VOID Example_MeasureString(HDC hdc) { Graphics graphics(hdc); // Set up the string. WCHAR string[] = L"Measure Text"; Font font(L"Arial", 16); RectF layoutRect(0, 0, 100, 50); RectF boundRect; // Measure the string. graphics.MeasureString(string, 12, &font, layoutRect, &boundRect); // Draw a rectangle that represents the size of the string. graphics.DrawRectangle(&Pen(Color(255, 0, 0, 0)), boundRect); }
回答by DeusAduro
Depending on how you are using this, you can use DrawText with DT_CALCRECT specified and it will (its always done it fairly accurately for me) calculate the size of the required rectangle based on the text/font/etc.
根据您使用它的方式,您可以使用指定了 DT_CALCRECT 的 DrawText,它会(它总是对我来说相当准确)根据文本/字体/等计算所需矩形的大小。
回答by Stanislav Krasimirov Georgiev
For Builder C++ first make new TLabel dynamicly and then change font attributes.Set your TLabel as autosize.Then you can get you TLabel width witch represents your string width in pixels.
对于 Builder C++,首先动态创建新的 TLabel,然后更改字体属性。将 TLabel 设置为自动大小。然后您可以获得 TLabel 宽度,以像素为单位表示您的字符串宽度。
int WidthPixels (String font, int size, String text)
{
TLabel* label = new TLabel(Form1); // dynamic TLabel
label->AutoSize = true;
label->Font->Name = font; // your font
label->Font->Size = size; // your font size
label->Caption = text; // your string
return label->Width;
}
int width = WidthPixels("Times New Roman", 19 , "Hey");