Results 1 to 2 of 2

Thread: How can I Rotate Text . . . ???

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    76

    Question How can I Rotate Text . . . ???

    how can i rotate ( 90° ) text and print in my form ?
    please Help me .
    thank you .

  2. #2
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    Note: These pseudo-lables don't generate click events and always go underneath any other controls...but it's a start.

    Drawing text on a VB window
    In order to draw on a VB form using the API, we first need to get a handle to the form's Device Context.
    A device context is a hardware abstraction which allows the same drawing commands to be performed by a range of different hardware types - display drivers, printers, plotters etc.
    In VB the form exposes its device context handle in the Form.hDC member.

    Making a font at an angle
    In order to write text at an angle we have to request a font which is at that angle, select that font into the form's device context, write the text and then unselect the font.
    To request a font at an angle we use the LOGFONT structure. This structure defines the font we would ideally like to have, and when selected the hardware tries to match our ideal font as closely as it can.

    Code:
    Private Const LF_FACESIZE = 32
    
    Private Type LOGFONT
        lfHeight As Long
        lfWidth As Long
        lfEscapement As Long
        lfOrientation As Long
        lfWeight As Long
        lfItalic As Byte
        lfUnderline As Byte
        lfStrikeOut As Byte
        lfCharSet As Byte
        lfOutPrecision As Byte
        lfClipPrecision As Byte
        lfQuality As Byte
        lfPitchAndFamily As Byte
        lfFaceName(LF_FACESIZE) As Byte
    End Type
    To fill this structure we need to use the SelectObject API to get a handle to the font being used by the device context and then use the GetObject API to fill in the structure according to this handle.

    The declarations are:
    VB Code:
    1. Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
    2.  
    3. ' Getting a LOGFONT from its handle
    4. Private Declare Function GetObjectLOGFONT Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As LOGFONT) As Long

    Unfortunately, SelectObject needs to be passed a handle for a new GDI object of the type that you want returned to work. Thus, to get the handle of the current font for a form you have to pass in the handle for another font. This would be a circular problem except for the existence of certain standard fonts which are returned by the GetStcokObject API call:

    VB Code:
    1. Public Enum GDIStockFonts
    2.     OEM_FIXED_FONT = 10
    3.     ANSI_FIXED_FONT = 11
    4.     ANSI_VAR_FONT = 12
    5.     SYSTEM_FONT = 13
    6.     DEVICE_DEFAULT_FONT = 14
    7.     SYSTEM_FIXED_FONT = 16
    8.     DEFAULT_GUI_FONT = 17
    9. End Enum
    10.  
    11. Private Declare Function GetStockObject Lib "gdi32" (ByVal nIndex As Long) As Long


    Thus the procedure to fill a LOGFONT from a form is thus:
    VB Code:
    1. Public Sub GetCurrentLogFont(Byval frmIn As Form, lfIn as LOGFONT)
    2.  
    3. Dim lNewFont As Long
    4. Dim lOldFont As Long
    5. Dim lRet As Long
    6.  
    7.    ' Get the current font's handle
    8.     lOldFont = SelectObject(frmIn.HDC, GetStockObject(ANSI_FIXED_FONT))
    9.   ' Select it back in to prevent the actualk font being wrongly changed
    10.     lNewFont = SelectObject(frmInHDC, lOldFont)
    11.  
    12. lRet = GetObjectLOGFONT(lOldFont, Len(lfIn), lfIn)
    13.  
    14. End Sub

    Then to alter this to set the font at an angle we change the LOGFONT's Orientation member. This is in 10ths of a degree...so to set it to 45 degrees the actual value should be 450.

    The resulting LOGFONT needs to be given a font handle by calling CreateFontIndirec API call:
    VB Code:
    1. ' Declaration
    2. Private Declare Function CreateFontIndirect Lib "gdi32" Alias _
    3.             "CreateFontIndirectA" (lpLOGFONT As LOGFONT) As Long

    and the handle returned by this can then be used in SelectObject. Any text printed using the TextOut API call after that will use this angled font.
    VB Code:
    1. '\ Declaration
    2. Private Declare Function TextOutApi Lib "gdi32" Alias "TextOutA" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long) As Long

    Thus the final code is:
    VB Code:
    1. Public Sub PrintTextAtAnAngle(ByVal frmIn As Form, ByVal Angle As Long, Byval xPos As Long, ByVal yPos As Long, ByVal Text As String)
    2.  
    3. Dim lfNew As LOGFONT
    4. Dim hNewFont As Long
    5. Dim hOldFont As Long
    6. Dim lRet As Long
    7.  
    8. '\\ Make the angled font
    9. Call GetCurrentLogFont(frmIn, lfNew)
    10.  
    11. lfNew.lfEscapement = (Angle * 10)
    12.  
    13. hNewFont = CreateFontIndirect(lfNew)
    14.  
    15. '\\ Select the angled font
    16. hOldFont = SelectObject(frmIn.hdc, hNewFont)
    17.  
    18. '\\ print the text
    19. lRet = TextOutApi(frmIn.HDC, xPos, yPos, Text, Len(Text))
    20.  
    21. '\\ Reselect the previous font
    22. hNewFont = SelectObject(frmIn.hdc, hOldFont)
    23. End Sub

    Hope this helps,
    Duncan
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

Posting Permissions

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



Click Here to Expand Forum to Full Width