Raz*_*zvi 17 c++ windows winapi
你能否在WIN32中更精确地测量字符串的宽度,而不是使用GetTextMetrics函数并使用tmAveCharWidth*strSize?
Nic*_*yer 22
尝试使用GetTextExtentPoint32.它使用给定设备上下文的当前字体来以逻辑单位测量呈现的字符串的宽度和高度.对于默认映射模式,MM_TEXT,1个逻辑单位是1个像素.
但是,如果您已更改当前设备上下文的映射模式,则逻辑单元可能与像素不同.您可以在MSDN上阅读有关不同映射模式的信息.使用映射模式,您可以将GetTextExtentPoint32返回的尺寸转换为像素.
Eva*_*ran 15
我不确定,但似乎:
HDC hDC = GetDC(NULL);
RECT r = { 0, 0, 0, 0 };
char str[] = "Whatever";
DrawText(hDC, str, strlen(str), &r, DT_CALCRECT);
Run Code Online (Sandbox Code Playgroud)
可能有用.
Run Code Online (Sandbox Code Playgroud)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); }