Results 1 to 6 of 6

Thread: Help with DrawText API with Colors

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Posts
    141

    Help with DrawText API with Colors

    is it possible to drawtext changing each letter with a different color?
    I know the api is

    Private Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hDC As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long

    and this will change the color..
    Private Declare Function SetTextColor Lib "gdi32" (ByVal hDC As Long, ByVal crColor As Long) As Long

    but i cant seem to figure out how to alternate the color

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Help with DrawText API with Colors

    Didn't give this much thought so FWIW just something I whipped up,

    Code:
    Option Explicit
    Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
    End Type
    Private Declare Function SetTextColor Lib "gdi32" (ByVal hDC As Long, ByVal crColor As Long) As Long
    Private Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hDC As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long
    Private Const DT_SINGLELINE = &H20
    Private Const DT_CALCRECT = &H400
    
    Private Sub TextDraw(ByVal PicBox As PictureBox, ByVal Txt As String, Optional ByVal LineNumber = 0)
        
        Dim rct As RECT
        Dim i As Integer
        Dim TxtHeight As Long, LeftPos As Long
        
        ' get height of text (for top position to draw/line number)
        TxtHeight = DrawText(PicBox.hDC, Txt, Len(Txt), rct, DT_CALCRECT)
        ' draw one character at a time
        For i = 1 To Len(Txt)
            ' set random color
            SetTextColor PicBox.hDC, RandomColor 'PicBox.ForeColor = RandomColor
            ' set position to draw
            With rct
                .Left = LeftPos
                .Right = PicBox.ScaleWidth
                .Top = LineNumber * TxtHeight
                .Bottom = PicBox.ScaleHeight
            End With
            ' draw the char.
            DrawText PicBox.hDC, Mid$(Txt, i, 1), 1, rct, DT_SINGLELINE
            ' add up char widths / left position for next char. to draw
            LeftPos = LeftPos + PicBox.TextWidth(Mid$(Txt, i, 1))
        Next i
        ' update
        PicBox.Refresh
    End Sub
    
    Private Function RandomColor() As Long
        RandomColor = RGB(Int(Rnd() * 256), Int(Rnd() * 256), Int(Rnd() * 256))
    End Function
    
    Private Sub Command1_Click()
        Picture1.Cls
        TextDraw Picture1, "Hello World!", 0   ' first line (top)
        TextDraw Picture1, "GoodBye World!", 1 ' second line
    End Sub
    
    Private Sub Form_Load()
        Picture1.ScaleMode = vbPixels
        Picture1.AutoRedraw = True
    End Sub
    Last edited by Edgemeal; Aug 2nd, 2009 at 08:48 PM. Reason: update/add comments

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Posts
    141

    Re: Help with DrawText API with Colors

    nice! thanks for the reply with an example. That should get me started.
    reason for doing this is because i would like to know how to format a label. I have gotten started on the control but was having issues with coloring the text. I just couldnt get the process into my thick skull. I knew it needed a loop.. but i couldn't figure out the text position.

    To make sure I understand it right, essentially..
    txtw = text width
    neww = new width .
    you are receiving the txtheight from the const. DT_CALCRECT?

  4. #4
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Help with DrawText API with Colors

    Quote Originally Posted by brandoncampbell View Post
    To make sure I understand it right, essentially..
    txtw = text width
    neww = new width .
    you are receiving the txtheight from the const. DT_CALCRECT?
    Yes, and the textheight part isn't really needed unless you want to draw the text to a specific line or draw more then one line of text.

    NewW is the next left starting position to draw, I should of named it LeftPos or something, TxtW is the width of the next character to draw.

    EDIT: Code updated
    Last edited by Edgemeal; Aug 2nd, 2009 at 08:31 PM.

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Help with DrawText API with Colors

    Quote Originally Posted by brandoncampbell View Post
    ...
    reason for doing this is because i would like to know how to format a label.
    FYI: When mentioning labels, if you meant VB's label control, VB labels cannot contain different color text, nor different font/sized text.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Posts
    141

    Re: Help with DrawText API with Colors

    LaVolpe, I realize that which is why I wanted to create my own label control. So that it can handle some type of formatting.

    just found this... and this would be nice to implement myself if I can.. http://www.teebo.com/html-label.htm
    Last edited by brandoncampbell; Aug 4th, 2009 at 04:28 PM.

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