A Visual Basic 6 program can display text on a form or a
picture box using the Print method. The text’s location is determined by the
destination object’s CurrentX and CurrentY
properties. The text is always left-aligned at this position, starting at the
location (CurrentX, CurrentY)
and extending to the right. VB doesn’t have a provision for changing this
alignment setting, but you can do it with a Windows API call.

You’ll use theSetTextAlign API function, whose declaration is:

Declare Sub SetTextAlign Lib “gdi32” (ByValhDC As Long, ByValwFlags As Long)

hDC is the destination object’s device context, which
you obtain from its hDC
property. wFlags is a value that specifies the desired

You specify the horizontal alignment using one of the
following constants (which you must define in your program):

    (value = 0),
    (value = 2), or
    (value = 6)

You can also specify the vertical alignment with these

  • TA_TOP
    (value = 0),
    (value = 8), or
    (value = 24)

Top alignment is the default; in other words, the top of the
text is aligned with the CurrentY position. The other
two options are:

  • Bottom: The lowest any character
    descends, such as the descender on a
    “y” or “g.”
  • Baseline: The bottom of characters
    that don’t have descenders, such as
    “a” and “w.”

To apply both a horizontal and a vertical alignment, simply
use the Or operator to combine the two flags. This
code sets the alignment for Form1 to center, baseline:

Call SetTextAlign(Form1.hDC, TA_CENTER Or TA_BASELINE

Note that changing alignment doesn’t affect text that is
already on the form or picture box.

Advance your scripting skills to the next level with TechRepublic’s free Visual Basic newsletter, delivered each Friday. Automatically sign up today!