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
 
 
 
 
 
TitleDraw rotated text centered at a point in Visual Basic 6
DescriptionThis example shows how to draw rotated text centered at a point in Visual Basic 6
Keywordsrotate text, center text, CreateFont
CategoriesGraphics
 

Subroutine DrawCenteredRotatedText draws centered rotated text. It starts by converting the fon'ts size in points into logical font units. It then uses CreateFont to make an appropriately rotated font and selects it.

The routine then gets the text's size in twips (the PictureBox's units). It uses some trigonometry to get vectorts and pointing in the directions of the text's rotated width and height. It uses those vectors to calculate where the text's origin should be to center the text and it draws the text.

The code finishes by drawing the center and using the vectors to draw a bounding box to show where the text is drawn.

 
Private Sub DrawCenteredRotatedText(ByVal pic As _
    PictureBox, ByVal txt As String, ByVal X As Single, _
    ByVal Y As Single, ByVal angle As Single, ByVal _
    font_points As Integer)
Const CLIP_LH_ANGLES As Long = 16   ' Needed for tilted
    ' fonts.
Const PI As Single = 3.14159265

Dim font_units As Single
Dim escapement As Long
Dim oldfont As Long
Dim newfont As Long
Dim wid As Single
Dim hgt As Single
Dim wx As Single
Dim wy As Single
Dim hx As Single
Dim hy As Single
Dim theta As Single
Dim ox As Single
Dim oy As Single

    font_units = font_points * GetDeviceCaps(pic.hdc, _
        LOGPIXELSY) / 72
    escapement = CLng(angle * 10)
    newfont = CreateFont(CLng(font_units), 0, escapement, _
        escapement, 700, _
        False, False, False, 0, 0, CLIP_LH_ANGLES, 0, 0, _
            "Times New Roman")
    ' Select the new font.
    oldfont = SelectObject(pic.hdc, newfont)

    ' Get the text width.
    wid = pic.TextWidth(txt)

    ' Convert the font height in points into twips.
    hgt = pic.ScaleY(font_points, vbPoints, vbTwips)

    theta = -angle * PI / 180 ' Negate because y increases
        ' downward.
    wx = wid * Cos(theta) / 2
    wy = wid * Sin(theta) / 2
    hx = -hgt * Sin(theta) / 2
    hy = hgt * Cos(theta) / 2

    ' Find the rotated origin.
    ox = X - wx - hx
    oy = Y - wy - hy

    ' Display the text.
    pic.CurrentX = ox
    pic.CurrentY = oy
    pic.Print txt

    ' Restore the original font.
    newfont = SelectObject(pic.hdc, oldfont)

    ' Free font resources (important!)
    DeleteObject newfont

    ' Draw the center point.
    pic.Circle (X, Y), 30, vbRed

    ' Draw the rotated bounding box.
    pic.CurrentX = X - wx - hx
    pic.CurrentY = Y - wy - hy
    pic.Line -(X + wx - hx, Y + wy - hy), vbBlue
    pic.Line -(X + wx + hx, Y + wy + hy), vbBlue
    pic.Line -(X - wx + hx, Y - wy + hy), vbBlue
    pic.Line -(X - wx - hx, Y - wy - hy), vbBlue
End Sub

Private Sub Form_Load()
Dim i As Integer

    picCanvas.AutoRedraw = True

    For i = 0 To 200 Step 50
        DrawCenteredRotatedText picCanvas, Format$(i), 300 _
            + i * 25, 600, i, 40
    Next i
End Sub


Private Sub Form_Resize()
    picCanvas.Move 0, 0, ScaleWidth, ScaleHeight
End Sub
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated