|
-
Aug 2nd, 2009, 06:41 PM
#1
Thread Starter
Addicted Member
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
-
Aug 2nd, 2009, 07:16 PM
#2
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
-
Aug 2nd, 2009, 07:40 PM
#3
Thread Starter
Addicted Member
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?
-
Aug 2nd, 2009, 07:57 PM
#4
Re: Help with DrawText API with Colors
 Originally Posted by brandoncampbell
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.
-
Aug 3rd, 2009, 09:35 AM
#5
Re: Help with DrawText API with Colors
 Originally Posted by brandoncampbell
...
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.
-
Aug 3rd, 2009, 03:58 PM
#6
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|