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
|