I need a method to calculate text width to check whether a text fits into a box. I tried to implement it in vbs, but for using the win API, I'd need records with arrays, which are not available. So...
text width calculation method
HKosova
10 years agoSmartBear Alumni (Retired)
For Windows apps, you can use the Windows API GetTextExtentPoint32 function to measure the text size. Here's a VBScript example. You need to specify the text and the UI object to get font parameters from:
Sub Main Dim str, obj
str = "Test"
Set obj = Sys.Process("notepad").Window("Notepad").Window("Edit")
Call Log.Message(GetTextWidth(str, obj)) End Sub ' Returns the width, in pixels, of the specified text if it was ' placed in the specified control. ' The function uses the control's current font to measure the text size. Function GetTextWidth(Str, Control) Dim dc, size dc = Win32API.GetDC(Control.Handle) Set size = Win32API.tagSIZE Call Win32API.GetTextExtentPoint32(dc, Str, Len(Str), size) Call Win32API.ReleaseDC(Control.Handle, dc) GetTextWidth = size.cx End Function
Related Content
- 20 days ago
- 6 years ago