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 on the printer using PaintPicture
Keywordsprint, rotate, text, PaintPicture
CategoriesGraphics
 
Use CreateFont to create the rotated font. Use SelectObject to select the font into a hidden PictureBox object and draw the text. Then use PaintPicture to copy the results to the Printer object. Clean up by reselecting the PictureBox's original font and destroying the rotated font with Destroy object.

The cmdPreview_Click event handler calls subroutine DrawText to display text on another form. The cmdPrint_Click event handler uses DrawText to display the text on the printer.

 
' Draw the text on the preview form.
Private Sub cmdPreview_Click()
    Screen.MousePointer = vbHourglass
    DrawText Form2
    Screen.MousePointer = vbDefault

    Form2.Show vbModal
End Sub

' Draw the text on the Printer object.
Private Sub cmdPrint_Click()
    Screen.MousePointer = vbHourglass
    DrawText Printer
    Screen.MousePointer = vbDefault

    Printer.EndDoc
End Sub
 
Subroutine DrawText uses CreateFont to make rotated fonts and draws on a hidden PictureBox. When it's done, it copies the result to the target object (printer or other form).

Because printers usually have much higher resolution than monitors, the result may be grainy. You can reduce graininess by drawing the text at larger than normal scale and then shrinking it to normal size with PaintPicture.

This example makes this reduction whether going to the printer or not. That makes the preview display a bit grainier but makes the print out smoother.

 
' Draw some text on the object.
Private Sub DrawText(obj As Object)
Const THE_TEXT = "Rotated text"
Const FONT_SIZE = 20
Const PRINTER_SCALE = 4
Dim wid As Single
Dim angle As Long
Dim new_font As Long
Dim old_font As Long

    ' Create a font rotated 360 degrees.
    new_font = CreateFont( _
        FONT_SIZE * PRINTER_SCALE, 0, 3600, 0, _
        FW_NORMAL, 0, 0, 0, DEFAULT_CHARSET, _
        OUT_TT_ONLY_PRECIS, _
        CLIP_LH_ANGLES Or CLIP_DEFAULT_PRECIS, _
        PROOF_QUALITY, TRUETYPE_FONTTYPE, _
        "Times New Roman")

    ' See how big the text is.
    old_font = SelectObject(picHidden.hdc, new_font)
    wid = 2 * (picHidden.TextWidth(THE_TEXT) + 120)
    picHidden.Width = wid
    picHidden.Height = wid
    picHidden.AutoRedraw = True
    picHidden.Cls

    ' Destroy the new font.
    SelectObject picHidden.hdc, old_font
    DeleteObject new_font

    ' Draw the rotated text.
    For angle = 300 To 3600 Step 300
        ' Create the rotated font.
        new_font = CreateFont( _
            FONT_SIZE * PRINTER_SCALE, 0, angle, 0, _
            FW_NORMAL, 0, 0, 0, DEFAULT_CHARSET, _
            OUT_TT_ONLY_PRECIS, _
            CLIP_LH_ANGLES Or CLIP_DEFAULT_PRECIS, _
            PROOF_QUALITY, TRUETYPE_FONTTYPE, _
            "Times New Roman")
        SelectObject picHidden.hdc, new_font

        ' Print the text.
        picHidden.CurrentX = wid / 2
        picHidden.CurrentY = wid / 2
        picHidden.Print THE_TEXT

        ' Destroy the new font.
        SelectObject picHidden.hdc, old_font
        DeleteObject new_font
    Next angle

    ' Make picHidden's image permanent.
    picHidden.Picture = picHidden.Image

    ' Copy the result onto the object.
    obj.PaintPicture picHidden.Picture, _
        0, 0, wid / PRINTER_SCALE, wid / PRINTER_SCALE, _
        0, 0, wid, wid
End Sub
 
For lots more information on rotated text and other graphics topics, see my book Ready-to-Run Visual Basic Graphics Programming.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated