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.

TXTWIDTH()

TXTWIDTH() returns the length in character columns that a string will take up on the current output window (or desktop), based on the specified font and style or the current default font.

Usage

nWidth = TXTWIDTH( cString [, cFont, nSize [, cStyle] ] )

Parameter

Value

Meaning

cString

Character

The string whose length is to be calculated.

cFont

Character

The name of the font in which the string should appear.

Omitted

Use the current output window's defined font.

nSize

Numeric

The size of the font in points.

Omitted

Use the current output window's defined font size.

cStyle

Character

A character expression specifying the style, or combination of styles, that the string will use (see WFONT() for a listing of styles).

Omitted

If both cFont and nSize are omitted, cStyle defaults to the current output window's defined style. If cFont and nSize are specified, the normal font style for that font is used. "Normal" is a bit misleading here, because it does not necessarily mean "plain." If a font is constructed with built-in attributes, the "normal" style for that font includes the attribute. Fox example, there is no difference in TXTWIDTH(), nor in appearance, between a display in "Arial Rounded MT Bold" when the bold attribute is set on or when it's left in its "normal" style.

nWidth

Numeric

Visual FoxPro's best guess on the size of the string, expressed in columns of the current output window's font.

TXTWIDTH() calculates the length a string will occupy in the current output window, expressed in terms of columns of the output window’s font (returned by WFONT()). This is more accurate than messing with FONTMETRIC(6) values for the average character width for both fonts involved.

TXTWIDTH() can be used to do FONTMETRIC() one better, by providing the length of each character within a font, a capability sadly lacking from the latter function. Rather than using FONTMETRIC’s feeble “average width,” TXTWIDTH() can be used when precision is needed.

When working with varied font types and sizes, TXTWIDTH() is usually the easiest to use. But when you’re working within a form and working in the default font, don’t overlook the possibility of using the TextWidth and TextHeight methods instead.

Example

? TXTWIDTH("Hello World!", "Arial",10,"B")

* The example in Visual FoxPro's Help file for centered text
* doesn't work. They added an extra space after the font name.
* This example centers the text within the current window,
* horizontally & vertically, displays overwriting in a variety
* of colors & sizes - neat effects for logos & splash screens.

cfontname = "Arial"
do Hello with 'Hello!',cFontName,36,"RB/W"
do Hello with 'Hello!',cFontName,30,"R/W"
do Hello with 'Hello!',cFontname,24,"RG+/W"
do Hello with 'Hello!',cFontName,18,"G/W"

procedure Hello (cWhatToSay, cFontName, nFontSize, cFontColor)
@ WROWS()/2, ;
  (WCOLS( )-TXTWIDTH(cWhatToSay, cFontName,nFontSize)* ;
   FONTMETRIC(6,cFontName,nFontSize)/FONTMETRIC(6))/2  ;
  SAY cWhatToSay ;
  FONT cFontName,nFontSize ;
  COLOR (cFontColor) ;
  STYLE "T"
return

See Also

FontMetric(), SysMetric(), TextWidth, TextHeight, WFont()