Logo

Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.

TextHeight, TextWidth

The TextHeight and TextWidth methods return the size of a supplied text string, in pixels.

Usage

nHeight = objFormOrScreen.TextWidth( cString )
nWidth = objFormOrScreen.TextHeight( cString )

These methods are handier versions of the TXTWIDTH() and FONTMETRIC() functions. They return the size a text string will take up on a Form or on the _SCREEN, expressed in pixels. The calculation is based on the current output window’s text name, size and style.

TextWidth returns the length of the longest line of text, if carriage returns are included in the supplied string. TextHeight increases correctly as well, but only if the string includes linefeed characters. This is not the case with VFP 3.x, where TextWidth returns the width for the whole string, as if the carriage returns were printable characters. For VFP 3.x, use MEMLINES() and MLINE() to parse the string into individual lines and pass them to TextWidth(). With VFP 5.0 and later, this bug has been squashed.

Example

_SCREEN.FontName = "Arial"
_SCREEN.FontSize = 10
nHowWide = _SCREEN.TextWidth("How wide is my string?")   && 134
nHowHigh = _SCREEN.TextHeight("How high is my string?")  && 16

See Also

FontMetric, MemLines(), MLine(), TxtWidth(), ScaleMode