Manfred_F's avatar
Manfred_F
Regular Contributor
9 years ago
Status:
New Idea

text width calculation method

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, TC team: please provide it as a standard method, e.g. in aqString..

3 Comments

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    Hi Manfred,

     

    Do you mean text width in pixels?

    What's your tested application type - desktop (C#, WPF, Java), web or mobile?

  • Manfred_F's avatar
    Manfred_F
    Regular Contributor

    Hi,

    Pixels would be fine. Our AUT is a windows application using a legadcy UI framework.

     

    Kind Regards

    Manfred

  • HKosova's avatar
    HKosova
    SmartBear 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