Measure the size of a text string

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
User avatar
JRL
Automation Wizard
Posts: 3497
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Measure the size of a text string

Post by JRL » Sun Jun 24, 2012 6:48 am

Ever need to know the length and height in pixels of a text string? Here is an easy method using a borderless, autosizing dialog.

Set a label in the dialog to the string you wish to measure, display the dialog, then get the size of the dialog window. Your text is measured for you.

Sample below requires v12 or better.

Code: Select all

Let>vText=This is the text to be measured...
Let>vFont=Arial
Let>vSize=12
Let>vStyle=1
Let>vColor=0

Dialog>Dialog1
object Dialog1: TForm
  AutoSize = True
  BorderStyle = bsNone
  object Label1: TLabel
    Left = 0
    Top = 0
  end
end
EndDialog>Dialog1

SetDialogProperty>Dialog1,label1,Caption,vText
SetDialogObjectFont>Dialog1,label1,vFont,vSize,vStyle,vColor
Let>WIN_USEHANDLE=1

MoveWindow>Dialog1.handle,-32000,0
Show>Dialog1
GetWindowSize>Dialog1.handle,TextSizeX,TextSizeY
MDL>%TextSizeX% X %TextSizeY%

EnderFFX
Pro Scripter
Posts: 92
Joined: Mon Mar 08, 2004 6:17 am

Post by EnderFFX » Wed Aug 29, 2012 2:52 pm

This does not seem to be working for my v. 12, I'm going to have to try it for my v. 13 I have on my laptop.

User avatar
JRL
Automation Wizard
Posts: 3497
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Aug 29, 2012 2:56 pm

Could you be more specific? What is happening? Do you not see a Macro Scheduler message with a size displayed?

EnderFFX
Pro Scripter
Posts: 92
Joined: Mon Mar 08, 2004 6:17 am

Post by EnderFFX » Wed Aug 29, 2012 2:58 pm

JRL wrote:Could you be more specific? What is happening? Do you not see a Macro Scheduler message with a size displayed?
I keep getting the number 708x365 no matter what text I put in place or what variables I change. (font size for example)

User avatar
JRL
Automation Wizard
Posts: 3497
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Aug 29, 2012 3:03 pm

Remark out the MoveWindow> line. You should then see the dialog. Seeing it might provide a clue.

What settings are you using for the variables defined at the top of the script?

EnderFFX
Pro Scripter
Posts: 92
Joined: Mon Mar 08, 2004 6:17 am

Post by EnderFFX » Wed Aug 29, 2012 3:06 pm

JRL wrote:Remark out the MoveWindow> line. You should then see the dialog. Seeing it might provide a clue.

What settings are you using for the variables defined at the top of the script?
That made it work!

And BTW - perfect timing on that script, I have an exact use for that exact script! Thank you!

User avatar
JRL
Automation Wizard
Posts: 3497
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Aug 29, 2012 3:27 pm

Apparently your computer doesn't recognize a window size properly if the window is not within the boundaries of the desktop. Another easy way to prevent the window from displaying is to set its alphablend value to zero.

Edit:
In the years since posting this I've discovered that the dialog does not need to display at all. If you use a resizewindow> the dialog will autosize around the text without needing to be displayed. So the movewindow and the alphablend are both unneeded. In the script below I've remarked out the movewindow and the show but left the alphablend.

Code: Select all

Let>vText=This is the text to be measured...
Let>vFont=Arial
Let>vSize=12
Let>vStyle=1
Let>vColor=0

Dialog>Dialog1
object Dialog1: TForm
  AlphaBlend = True
  AlphaBlendValue = 0
  AutoSize = True
  BorderStyle = bsNone
  object Label1: TLabel
  end
end
EndDialog>Dialog1

SetDialogProperty>Dialog1,label1,Caption,vText
SetDialogObjectFont>Dialog1,label1,vFont,vSize,vStyle,vColor

Let>WIN_USEHANDLE=1
ResizeWindow>Dialog1.handle,0,0
//MoveWindow>Dialog1.handle,-32000,0
//Show>Dialog1
GetWindowSize>Dialog1.handle,TextSizeX,TextSizeY
Let>WIN_USEHANDLE=0
MDL>%TextSizeX% X %TextSizeY%

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts