Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitlePrint rotated text using TextOut
Keywordsprint, rotate, text
CategoriesGraphics
 
Thanks to Mark Quincey.

Use CreateFontIndirect to create the font. Use SelectObject to select it. Use TextOut to print text.

 
' Print rotated text.
Private Sub cmdPrint_Click()
Const FONT_SIZE = 30
Const FONT_FACE = "Times New Roman"
Const TXT = "Here is some rotated text"

Dim printer_hdc As Long
Dim log_font As LOGFONT
Dim new_font As Long
Dim old_font As Long

    ' Initialize the printer.
    Printer.Print " "

    ' Save the hDC.
    printer_hdc = Printer.hdc

    ' Create the rotated font.
    With log_font
        .lfEscapement = 3150
        .lfHeight = (FONT_SIZE * -20) / _
            Printer.TwipsPerPixelY

        ' End the font name with a vbNullChar.
        ' Thanks to Tim Rude (timrude@ hotmail.com) for
        ' finding this.
        .lfFaceName = FONT_FACE & vbNullChar
    End With
    new_font = CreateFontIndirect(log_font)

    ' Select the font.
    old_font = SelectObject(printer_hdc, new_font)

    ' Draw the text.
    TextOut printer_hdc, 500, 500, TXT, Len(TXT)

    ' Restore the original font.
    SelectObject printer_hdc, old_font
    DeleteObject new_font

    Printer.EndDoc
End Sub
 
Notes:

  • In older versions of Visual Basic, you could select the font and then use Printer.Print to print using it. This no longer works. You seem to have to use TextOut.
  • You must make the printer do something (such as printing a space) to initialize its HDC.
  • If you access the Printer object's properties and methods, it messes up the font. For example, if you use Printer.TextWidth to see how big the text is, the Printer reselects its default font so you will need to reselect the custom font.

For more information on advanced printing in Visual Basic, see my book Ready-to-Run Visual Basic Graphics Programming.

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated