Microsoft KB Archive/48013

From BetaArchive Wiki
Knowledge Base

Article ID: 48013

Article Last Modified on 1/8/2003


  • Microsoft QuickBasic Compiler for Macintosh 1.0

This article was previously published under Q48013


This article describes how to center a text string in the current output window. This information applies to Microsoft QuickBASIC Version 1.00 for the Apple Macintosh.


You can invoke the WIDTH(x$) function to automatically determine the width in pixels of any text string (x$) in the current font. You can divide this width by 2 to find the center of any string to be printed.

The WINDOW(2) function determines the width of the current output window in pixels. You can divide this width by 2 to find the center of the current output window. WINDOW(3) is the height of current output window in pixels.

Use CALL MOVETO(X,Y) to position the print position to any desired pixel, then PRINT the string (or use Drawtext, which is faster than PRINT).

The code example below works for any proportional or nonproportional font.

Each character in a nonproportional (monospaced) font is exactly the same width in pixels. WIDTH(x$) returns the same width for every character in a nonproportional font. Monaco (TEXTFONT 4) and Courier (TEXTFONT 22, if installed in the System) are examples of nonproportional fonts. (Note that Page 397 of "Microsoft QuickBASIC for Macintosh: BASIC Language Reference" should be corrected to show Courier as font 22, and Helvetica as font 21.)

However, most Macintosh fonts are proportional fonts. Each character in a proportional font can have a different width (in pixels), and WIDTH(x$) can return different widths for different characters.

Code Example

The following program varies the text size and centers the string printed in the current output window:

a$="This is centered."
TEXTFACE 1          ' Turns on bold face.
ww=WINDOW(2)        ' Width of the current output window in pixels.
hw=WINDOW(3)        ' Height of the current output window in pixels.
y=hw/2              ' Start printing halfway down the output window.
FOR j=21 TO 6 STEP -2
TEXTSIZE j          ' j varies the text size.
wa = WIDTH(a$)      ' Width of the string contents of a$ in pixels.
x = ww/2 - wa/2     ' x pixel position.
y = y + j           ' y pixel position.
CALL MOVETO(x, y)   ' Move the printing position.
drawtext a$         ' The Drawtext MBLC routine is faster than PRINT.

Additional query words: MQuickB

Keywords: KB48013