Results 1 to 1 of 1

Thread: Drawing a rotated text on a form

  1. #1

    Drawing a rotated text on a form

    vb Code:
    1. Private Const LF_FACESIZE = 32
    2.  
    3. Private Type LOGFONT
    4.         lfHeight As Long
    5.         lfWidth As Long
    6.         lfEscapement As Long
    7.         lfOrientation As Long
    8.         lfWeight As Long
    9.         lfItalic As Byte
    10.         lfUnderline As Byte
    11.         lfStrikeOut As Byte
    12.         lfCharSet As Byte
    13.         lfOutPrecision As Byte
    14.         lfClipPrecision As Byte
    15.         lfQuality As Byte
    16.         lfPitchAndFamily As Byte
    17.         lfFaceName(1 To LF_FACESIZE) As Byte
    18. End Type
    19.  
    20. Private Const OBJ_FONT = 6
    21.  
    22. Private Declare Function TextOut Lib "gdi32" Alias "TextOutA" _
    23. (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal lpString As String, _
    24. ByVal nCount As Long) As Long
    25. Private Declare Function CreateFontIndirect Lib "gdi32" Alias "CreateFontIndirectA" _
    26. (lpLogFont As LOGFONT) As Long
    27. Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" _
    28. (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
    29. Private Declare Function GetCurrentObject Lib "gdi32" _
    30. (ByVal hdc As Long, ByVal uObjectType As Long) As Long
    31. Private Declare Function SetTextColor Lib "gdi32" _
    32. (ByVal hdc As Long, ByVal crColor As Long) As Long
    33. Private Declare Function SelectObject Lib "gdi32" _
    34. (ByVal hdc As Long, ByVal hObject As Long) As Long
    35. Private Declare Function DeleteObject Lib "gdi32" _
    36. (ByVal hObject As Long) As Long
    37.  
    38.  
    39. 'Create rotated font handle.
    40. Private Function GetFont(hdc As Long, Angle As Double) As Long
    41.     Dim hFont           As Long
    42.     Dim lf              As LOGFONT
    43.    
    44.     'Get the current HFONT handle
    45.     hFont = GetCurrentObject(hdc, OBJ_FONT)
    46.     'Retrieve the LOGFONT structure from the font handle.
    47.     GetObject hFont, Len(lf), lf
    48.     'Change the font angle
    49.     lf.lfEscapement = CInt(Angle * 10)
    50.     lf.lfOrientation = lf.lfEscapement
    51.     'Create a new font
    52.     GetFont = CreateFontIndirect(lf)
    53. End Function
    54.  
    55. Private Sub DrawText(hdc As Long, Text As String, X As Integer, Y As Integer, _
    56. Angle As Double, Color As Long)
    57.     Dim hFont           As Long
    58.     Dim hPrevFont       As Long
    59.    
    60.     SetTextColor hdc, Color
    61.     'Create a font for the rotated text
    62.     hFont = GetFont(hdc, Angle)
    63.     'Select the font into the DC
    64.     hPrevFont = SelectObject(hdc, hFont)
    65.     'Draw the text
    66.     TextOut hdc, X, Y, Text, Len(Text)
    67.     'Select back the previous font
    68.     SelectObject hdc, hPrevFont
    69.     'destroy the font object.
    70.     DeleteObject hFont
    71. End Sub
    72.  
    73. Private Sub Form_Paint()
    74.     Dim TextToDraw      As String
    75.     Dim X               As Integer
    76.     Dim Y               As Integer
    77.     Dim Angle           As Double
    78.    
    79.     'We must use a TrueType font, otherwise the text won't be rotated.
    80.     Font.Name = "Arial"
    81.     Font.Bold = True
    82.     Font.Size = 36
    83.     TextToDraw = "http://www.vbforums.com/"
    84.    
    85.     X = 20: Y = 350
    86.     'You can change the Angle value from 0 and up to 360 degrees in steps of 0.1 degrees.
    87.     Angle = 45
    88.     'Draw the text in 3 colors in order to create 3D effect.
    89.     DrawText hdc, TextToDraw, X - 1, Y - 1, Angle, RGB(0, 0, 255)
    90.     DrawText hdc, TextToDraw, X + 1, Y + 1, Angle, RGB(0, 0, 0)
    91.     DrawText hdc, TextToDraw, X, Y, Angle, RGB(0, 0, 192)
    92. End Sub

    Last edited by Hack; Apr 2nd, 2012 at 06:03 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •