Rotating Text

by Stephan Grieger - GUI Computing

In my previous article on building a ruler for the print preview window, I discussed how to build the horizontal bar only. The horizontal bar was the easiest as the text is also horizontal. However the vertical ruler has the text rotated 90 degrees. The first solution is to use a custom control to rotate the text but then the effect slows down and begins to flicker. The second solution is to use a font that can rotate the text, though I've not found one yet. That leaves only one solution, the API.

Now you may say, but there's no API that will rotate text. Correct, there isn't. However, that doesn't stop us from creating a new font that is rotated. Now before you tell me it's to complicated to build an entire font, we're not going to design a font from scratch. What we are going to do is utilise an existing font to build another one.

The API calls you will need are as follows (Page numbers refer to Daniel Appleman's API Bible) :

In the API help file that comes shipped with Visual Basic are the following type declarations :

Rect
LogFont

The LogFont structure is a little complex, so let me just explain what each of the components do.

lfHeightThe height of the font in logical units.
lfWidthThe width of the font in logical units.
lfEscapementSpecifies the angle of the text. Refer to page 436 for a full description.
lfOrientationUnused.
lfWeightSpecifies the weight of the font. This number ranges from 100 to 900. 100 is light.
lfItalicSpecifies whether the font is italic or not. Non zero specifies italic.
lfUnderlineNon zero for underlining.
lfStrikeoutNon zero for strikeout.
lfCharSetValid constants are ANSI_CHARSET, DEFAULT_CHARSET, SYMBOL_CHARSET, OEM_CHARSET.
lfOutprecisionProvides guidelines to help the GDI find a matching font.
lfClipPrecisionDefines the guidelines for clipping text when the characters are slightly outside the clipping region.
lfQualityOne of the following constants. DEFAULT_QUALITY, DRAFT_QUALITY, PROOF_QUALITY.
lpPitchAndFamilya combination of the following constants. DEFAULT_PITCH, FIXED_PITCH, VARIABLE_PITCH, FF_DECORATIVE, FF_MODERN, FF_ROMAN, FF_SCRIPT, FF_SWISS, FF_DONTCARE.
lfFaceNameSpecifies the name of the font to be used.

OK, So the Code at Last...

First step is to create the font, then select that new font as the font that we want to use for printing and print the text.

  If FontToUse <'> 0 Then di% = DeleteObject(FontToUse%)
  lf.lfHeight = 20
  lf.lfWidth = 10
  lf.lfEscapement = 2700
  lf.lfWeight = 400
  lf.lfItalic = Chr$(0)
  lf.lfItalic = Chr$(0)
  lf.lfUnderline = Chr$(0)
  lf.lfStrikeOut = Chr$(0)
  lf.lfOutPrecision = Chr$(2)
  lf.lfClipPrecision = Chr$(1)
  lf.lfQuality = Chr$(0)
  lf.lfPitchAndFamily = Chr$(0)
  lf.lfCharSet = Chr$(0)
  lf.lfFaceName = "Arial" & Chr$(0)

Now that we've set up the structure for the font we need to create it :

  FontToUse = CreateFontIndirect(lf)

Now select the new font as the one to use.

  oldhdc = SelectObject (PictureControl.hDC, Rect)

Print the text to the control.

  di% = TextOut(PictureControl.hDC, 100, 0, "Rotate Text", 11)
  di% = SelectObject(PictureControl.hDC, oldhdc)

The 2700 value used as the escapement will rotate the text 90 degrees. The text is pointing the wrong way but all we need to do is flip it the right way.


Written by: Stephan Grieger
August 1995

Image of arrow to previous article Image of arrow to next article

[HOME] [TABLE OF CONTENTS] [SEARCH]