QB64 is a modern extended BASIC programming language that retains QBasic/QuickBASIC 4.5 compatibility and compiles native binaries for Windows, Linux, and macOS.

The _PRINTWIDTH function returns the width in pixels of the text STRING specified.


pixelWidth% = _PRINTWIDTH(textToPrint$[, destinationHandle&])



SUB returns font or screen mode’s text block size using _PRINTWIDTH and _FONTHEIGHT without a handle parameter.

  INPUT "Enter Screen mode 1, 2 or 7 to 13: ", scr$
  mode% = VAL(scr$)
LOOP UNTIL mode% > 0 
SCREEN mode%
INPUT "Enter first name of TTF font to use or hit enter for text size: ", TTFont$
IF LEN(TTFont$) THEN INPUT "Enter font height: ", hi$
height& = VAL(hi$)
IF height& > 0 THEN _FONT _LOADFONT("C:\Windows\Fonts\" + TTFont$ + ".ttf", height&, style$)

TextSize wide&, high&       'get the font or current screen mode's text block pixel size

_PRINTSTRING (20, 100), CHR$(1) + STR$(wide&) + " X" + STR$(high&) + " " + CHR$(2)


SUB TextSize (TextWidth&, TextHeight&)
TextWidth& = _PRINTWIDTH("W")     'measure width of one font or text character
TextHeight& = _FONTHEIGHT         'can measure normal text block heights also   

Note: The SUB procedure does not need the font handle for font sizes after _FONT enables one.

See Also