From QB64 Wiki

Jump to: navigation, search

The _PRINTWIDTH function returns the width in pixels of the _FONT or text string that a program will print.


pixel_width = _PRINTWIDTH(text_to_print$[, destination_handle&])


  • text_to_print$ is any literal or variable STRING value.
  • If the destination_handle& is omitted, the current destination image or screen page is used.
  • Useful to find the width of the font print string before actually printing it.
  • Can be used with variable width fonts or no font at all unlike _FONTWIDTH which requires MONOSPACE fonts only.
  • _PRINTWIDTH cannot be used in SCREEN 0 in QB64-SDL version.
  • QB64-GL does support SCREEN 0 use of _PRINTWIDTH which will return the character length of text exactly as LEN(text$) would.

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

DO 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) END SUB TextSize (TextWidth&, TextHeight&) TextWidth& = _PRINTWIDTH("W") 'measure width of one font or text character TextHeight& = _FONTHEIGHT 'can measure normal text block heights also END SUB

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

See also:

Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page
Personal tools