Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[FUNCTION] MeasureText(Str[, FontOpts[, FontName]])


  • Please log in to reply
5 replies to this topic
just me
  • Members
  • 1496 posts
  • Last active: Nov 03 2015 04:32 PM
  • Joined: 28 May 2011
As the name implies, this function measures the height and width of a given text having regard to font settings:

#NoEnv
FontSizes := [8, 10, 12, 14, 16]
FontName := "Tahoma"
Str := "I Button!"
For Each, Size in FontSizes {
   T := MeasureText(Str, "s" . Size, FontName)
   MsgBox, 0, "%Str%"
         , % "FontName = " . FontName . "`nFontSize = " . Size . "`nTextHeight = " . T.H . "`nTextWidth = " . T.W
}
ExitApp

; ======================================================================================================================
; Function:       Measures the single-line width and height of the passed text.
; AHK version:    1.1.05+
; Language:       English
; Tested on:      Win XPSP3 (U32)
; Version:        1.0.00.00/2012-06-28/just me
; Parameters:
;     Str         -  String to measure.
;     Optional       ---------------------------------------------------------------------------------------------------
;     FontOpts    -  Options used with the Gui, Font, ... command.
;                    Default: "" - Default GUI font options
;     FontName    -  Font name used with the Gui, Font, ... command.
;                    Default: "" - Default GUI font
; Return value:   Object containing two key/value pairs:
;                    W  -  measured width.
;                    H  -  measured height.
; ======================================================================================================================
MeasureText(Str, FontOpts = "", FontName = "") {
   Static DT_FLAGS := 0x0520 ; DT_SINGLELINE = 0x20, DT_NOCLIP = 0x0100, DT_CALCRECT = 0x0400
   Static WM_GETFONT := 0x31
   Size := {}
   Gui, New
   If (FontOpts <> "") || (FontName <> "")
      Gui, Font, %FontOpts%, %FontName%
   Gui, Add, Text, hwndHWND
   SendMessage, WM_GETFONT, 0, 0, , ahk_id %HWND%
   HFONT := ErrorLevel
   HDC := DllCall("User32.dll\GetDC", "Ptr", HWND, "Ptr")
   DllCall("Gdi32.dll\SelectObject", "Ptr", HDC, "Ptr", HFONT)
   VarSetCapacity(RECT, 16, 0)
   DllCall("User32.dll\DrawText", "Ptr", HDC, "Str", Str, "Int", -1, "Ptr", &RECT, "UInt", DT_FLAGS)
   DllCall("User32.dll\ReleaseDC", "Ptr", HWND, "Ptr", HDC)
   Gui, Destroy
   Size.W := NumGet(RECT,  8, "Int")
   Size.H := NumGet(RECT, 12, "Int")
   Return Size
}

Prefer ahkscript.org for the time being.


G. Sperotto
  • Members
  • 539 posts
  • Last active: Jun 20 2015 04:54 PM
  • Joined: 12 Dec 2011
Very nice! :D

Congratulations on the work and thanks for sharing. This will solve a lot of issues on window art planning :)

"What is a suitable automation? Whatever saves your day for the greater matters."
Barcoder - Create QR Codes and other Barcodes using only Autohotkey !!


Cephei1
  • Members
  • 396 posts
  • Last active: Jan 22 2014 08:19 PM
  • Joined: 04 Aug 2008
Amazing! Thank you.

Ahk_L (Unicode 32-bit)


CodeEater
  • Members
  • 83 posts
  • Last active: Jun 06 2013 03:42 PM
  • Joined: 17 Jun 2012

Hello,just me

Could you add an option to restrict the width/height of the string.

for example: i'd like to know the height of the text with the given width.



just me
  • Members
  • 1496 posts
  • Last active: Nov 03 2015 04:32 PM
  • Joined: 28 May 2011
Hello CodeEater,

here's a "non-magic" version to get the height for a given width:
 
MeasureTextHeight(Str, Width, FontOpts = "", FontName = "") {
   Gui, New
   If (FontOpts <> "") || (FontName <> "")
      Gui, Font, %FontOpts%, %FontName%
   Gui, Add, Text, w%Width% hwndHWND, %Str%
   WinGetPos, , , , Height, ahk_id %HWND%
   Gui, Destroy
   Return Height
}

Prefer ahkscript.org for the time being.


CodeEater
  • Members
  • 83 posts
  • Last active: Jun 06 2013 03:42 PM
  • Joined: 17 Jun 2012

@@ OMG!!!

 

I used it in the past.. :D

anyway, thanks for this solution :)