Results 1 to 4 of 4

Thread: Vertical text

  1. #1

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    What do you mean by Vertical text?

    I
    S

    T
    H
    I
    S

    W
    H
    A
    T

    Y
    O
    U

    M
    E
    A
    N
    ?


    If so you have to print each letter one at a time.

    Printer.Print "I"
    Printer.Print "S"

    Printer.Print "T"
    Printer.Print "H"
    Printer.Print "etc....."
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  2. #2
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    This function only works for monospaced fonts such as those used in text files to create columns. It will not work with windows programs using fonts that are not monospaced due to kerning. In other words, they won't line up. But it's great for creating text files with columns.

    VB Code:
    1. ' Declarations
    2.  
    3. Private Enum TEXT_ALIGN
    4.   ALIGN_LEFT = 0
    5.   ALIGN_RIGHT = 1
    6. End Enum
    7.  
    8. Private Function PadString(ByVal sString As String, Length As Integer, Align As Integer, Optional PadChar As String = " ") As String
    9. Dim iLen As Integer
    10.  
    11. sString = Trim$(sString)
    12. iLen = Len(sString)
    13.  
    14. Select Case Align
    15.   Case Is = ALIGN_RIGHT
    16.     PadString = String$(Length - iLen, PadChar) & sString
    17.   Case Else ' Align left
    18.     PadString = sString & String$(Length - iLen, PadChar)
    19. End Select
    20.  
    21. End Function

  3. #3
    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.
    Note that only TrueType fonts can be rotated.

    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(LF_FACESIZE) As Byte
    18. 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 GetStockObject 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(frmIn.HDC, 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 CreateFontIndirect 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.

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

    Use
    VB Code:
    1. Private Sub Form_Paint()
    2.  
    3. Call PrintTextAtAnAngle(Me, 90,200,100, "Straight up!")
    4.  
    5. End Sub

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

  4. #4
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    I was just about to post a link to your article actually
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

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