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. |
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 |