Results 1 to 12 of 12

Thread: text APIs

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2001
    Posts
    3

    text APIs

    G'day all,

    I was wondering if anyone knows if there are any APIs that print text vertically and horizontally aligned within a given rectangular area?
    Shade
    - Always carry spare spare bandaids

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Have you checked the API sites

    http://www.allapi.net/
    http://www.vbapi.com/

  3. #3
    jim mcnamara
    Guest
    I'm not sure if you mean text placement or text rotation to vertical. Here is both:

    Text placement using TextOut api:
    Code:
    'This Project needs
    '- two timers, interval=100
    '- a button
    
    'in general section
    Private Type POINTAPI
        x As Long
        y As Long
    End Type
    
    Private Declare Function GetActiveWindow Lib "user32" () As Long
    Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function Ellipse Lib "gdi32" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
    
    Private Declare Function TextOut 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
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Sub Form_Load()
        Timer1.Interval = 100
        Timer1.Enabled = True
        Timer2.Interval = 100
        Timer2.Enabled = True
        Command1.Caption = "Draw Text"
    End Sub
    'This will draw an Ellipse on the active window
    Sub Timer1_Timer()
        Dim Position As POINTAPI
        'Get the cursor position
        GetCursorPos Position
        'Draw the Ellipse on the Screen's DC
        Ellipse GetWindowDC(0), Position.x - 5, Position.y - 5, Position.x + 5, Position.y + 5
    End Sub
    Sub Command1_Click()
        'KPD-Team 1998
        'URL: http://www.allapi.net/
        'E-Mail: [email protected]
    
        Dim intCount As Integer, strString As String
        strString = "Cool, text on screen !"
        For intCount = 0 To 30
            'Draw the text on the screen
            TextOut GetWindowDC(0), intCount * 20, intCount * 20, strString, Len(strString)
        Next intCount
    End Sub
    Private Sub Timer2_Timer()
        'Draw the text to the active window
        TextOut GetWindowDC(GetActiveWindow), 50, 50, "This is a form", 14
    End Sub


    This is font rotation - printing in a PictureBox - PB
    Code:
    from vb2themax.com:
    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 As String * LF_FACESIZE
    End Type
    
    Private Declare Function SetGraphicsMode Lib "gdi32" (ByVal hdc As Long, _
        ByVal iMode As Long) As Long
    Private Declare Function MulDiv Lib "Kernel32" (ByVal nNumber As Long, _
        ByVal nNumerator As Long, ByVal nDenominator As Long) As Long
    Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, _
        ByVal nIndex As Long) As Long
    Private Declare Function CreateFontIndirect Lib "gdi32" Alias _
        "CreateFontIndirectA" (lpLogFont As LOGFONT) As Long
    Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, _
        ByVal hObject As Long) As Long
    Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As _
        Long
    Private Declare Sub CopyMemory Lib "Kernel32" Alias "RtlMoveMemory" (dest As _
        Any, Source As Any, ByVal bytes As Long)
    
    ' Print rotated text
    ' The first argoment can be a form, a picture box, the Printer, and in general
    ' any VB object that supports the Font and the hDC properties.
    ' Text is the string to be printed
    ' Angle is the orientation, in 10th of degrees (default is 90°)
    ' X and Y are the printing coordinates (omit to use the current coordinates)
    '
    ' Note: you get best results when using TrueType fonts
    
    Sub PrintRotatedText(PB As Object, ByVal Text As String, _
        Optional ByVal Angle As Integer = -900, Optional x As Variant, _
        Optional y As Variant)
    
        Dim hfont As Long, holdfont As Long
        Dim Font As LOGFONT
       
        Const GM_ADVANCED = 2
        Const LOGPIXELSY = 90
        SetGraphicsMode PB.hdc, GM_ADVANCED
        
        ' Create a Font object, similar to the current font in PB
        ' but with a different orientation
        Font.lfHeight = -MulDiv(PB.FontSize, GetDeviceCaps(PB.hdc, LOGPIXELSY), 72)
        Font.lfWidth = 0
        Font.lfEscapement = Angle
        Font.lfOrientation = Angle
        Font.lfWeight = IIf(PB.FontBold, 700, 400)
        Font.lfItalic = IIf(PB.FontItalic, 1, 0)
        Font.lfUnderline = IIf(PB.FontUnderline, 1, 0)
        Font.lfStrikeOut = IIf(PB.FontStrikethru, 1, 0)
        Font.lfCharSet = 0
        Font.lfOutPrecision = 0
        Font.lfClipPrecision = 0
        Font.lfQuality = 2
        Font.lfPitchAndFamily = 33
        Font.lfFaceName = PB.FontName & vbNullChar
        
        hfont = CreateFontIndirect(Font)
        holdfont = SelectObject(PB.hdc, hfont)
        
        ' Account for X,Y coordinates
        If Not IsMissing(x) Then PB.CurrentX = x
        If Not IsMissing(y) Then PB.CurrentY = y
        
        ' do the printing
        PB.Print Text
        ' reselect the old font
        SelectObject PB.hdc, holdfont 
        ' destroy the font object just created
        DeleteObject hfont
    End Sub

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2001
    Posts
    3
    Thanks for the replies,

    I guess I should have explained the situation a bit more completely.

    I have written a control loosely based on a grid concept.

    each cell of the grid amongst other things has a text attribute, a vertical alignment attribute, a horizontal alignment attribute and a wordwrap attribute.

    I have a function that displays the text within each cell correctly, (using GetTextExtentPoint32) but I am certain it isn't the most efficient implementation. Since wordwrap is so common (control buttons, label captions, text boxes etc) I was hoping there would be an API ( TextOutWrap perhaps) that would accept the arguments/parameters of a rect, a string, a vertical alignment, and a horizontal alignment.

    Any ideas?

    (I apologise if 2 versions of this reply turn up)
    Shade
    - Always carry spare spare bandaids

  5. #5
    Banned Motxopro's Avatar
    Join Date
    Dec 2001
    Posts
    57
    no

  6. #6
    Member
    Join Date
    Dec 2001
    Location
    ny
    Posts
    38
    actually, yes.

    Look up DrawText on MSDN. You can give it a RECT structure to draw your text within, and tell it to word-wrap, amongst other things.

  7. #7
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    Originally posted by Motxopro
    no
    r u brainless?

    there is no need for that ffs

    he doesn't care if you don't have any ideas and neither does anyone else.

    actually maybe i care, because i'm posting this now, to give u a piece of my mind, because i've noticed you never think before you post!

  8. #8

    Thread Starter
    New Member
    Join Date
    Dec 2001
    Posts
    3
    The DrawText API is certainly an easier than the way I was doing it.

    But (there's always a 'but' isn't there!) now what I need to do is draw several strings consecutively within the same rectangle formatted as if it was a single string. For instance a sentence with each word in a different colour.

    what I get at the moment with DrawText is each word written over the top of each other as they all start printing at the same place

    Is there some way of stating a start point for (as with TextOut) and some way of knowing where the drawn string ends so to know where the next string should start?

    so I guess what I am looking for is an API that behaves like DrawText but allows me to specify a start point, and returns its end point
    Shade
    - Always carry spare spare bandaids

  9. #9
    Member
    Join Date
    Dec 2001
    Location
    ny
    Posts
    38
    Nope, sorry. You'll have to write all the code to handle it yourself unfortunately.

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    How about something like this:
    VB Code:
    1. Const TA_BASELINE = 24
    2. Const TA_BOTTOM = 8
    3. Const TA_CENTER = 6
    4. Const TA_LEFT = 0
    5. Const TA_NOUPDATECP = 0
    6. Const TA_RIGHT = 2
    7. Const TA_TOP = 0
    8. Const TA_UPDATECP = 1
    9. Const TA_MASK = (TA_BASELINE + TA_CENTER + TA_UPDATECP)
    10. Private Declare Function SetTextAlign Lib "gdi32" (ByVal hdc As Long, ByVal wFlags As Long) As Long
    11. Private Declare Function TextOut 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
    12. Const mStr = "www.allapi.net"
    13. Private Sub Form_Paint()
    14.     'KPD-Team 2000
    15.     'URL: [url]http://www.allapi.net/[/url]
    16.     'E-Mail: [email][email protected][/email]
    17.     'set the form's scalemode to pixels
    18.     Me.ScaleMode = vbPixels
    19.     'set the forecolor to white
    20.     Me.ForeColor = vbWhite
    21.     'draw some lines to show where our TextOut-x- and y-parameters are
    22.     Me.Line (100, 0)-(100, 100)
    23.     Me.Line (100, 100)-(0, 100)
    24.     Me.Line (150, 0)-(150, 150)
    25.     Me.Line (150, 150)-(0, 150)
    26.     'set the forecolor back to black
    27.     Me.ForeColor = vbBlack
    28.     'call textalign to align to the right
    29.     SetTextAlign Me.hdc, TA_RIGHT Or TA_TOP Or TA_NOUPDATECP
    30.     'show some text
    31.     TextOut Me.hdc, 100, 100, mStr, Len(mStr)
    32.     'call textalign to align to the left
    33.     SetTextAlign Me.hdc, TA_LEFT Or TA_TOP Or TA_NOUPDATECP
    34.     'show some text
    35.     TextOut Me.hdc, 150, 150, mStr, Len(mStr)
    36. End Sub

  11. #11
    Megatron
    Guest
    Use the DrawText function, and specify DT_CENTER as the flag.

  12. #12
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    To jim mcnamara

    I have tried the above code, but it just prints it horizontally anyway. I realize you got the code from vb2themax, but does it work for you?

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