Solved

Different colored text in a single picturebox?

Posted on 2003-11-17
6
279 Views
Last Modified: 2010-05-01
I'm using VB6 to load text from a SQL server to a string, then I continuously scroll this string in a PictureBox control. I know how to color all the text, but can I color only a portion of the string?
0
Comment
Question by:TWCMIL
6 Comments
 
LVL 3

Expert Comment

by:monvelasquez
ID: 9768389
Im sure it's possible but i don't know how. It would be easier if you use a RichTextBox control instead
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9768408
You could use the DrawText() and SetTextColor() API's.  Each portion of the text that is to be a different color would have to be drawn separately.

Idle_Mind
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9768621
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 Sub Form_Paint()
    Dim text As String
    Dim r As RECT
   
    r.Top = 0
    r.Left = 0
    r.Right = Me.Width
    r.Bottom = Me.Height
   
    text = "This is a long string of text with different colors"
    For a = 1 To Len(text)
        SetTextColor Me.hdc, QBColor(Int(Rnd * 15))
        DrawText Me.hdc, Mid(text, a, 1), 1, r, 0
        r.Left = r.Left + TextWidth(Mid(text, a, 1)) / Screen.TwipsPerPixelX
    Next a
End Sub
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:gajendra
ID: 9768718
Sure you can change the color of every word or even every character

with PictureBox1
   .forecolor = vbyellow
   .Print  "Hello! "
   .foreColor = vbred
   .Print "How "
   .forecolor = vbblue
   .Print "do "
   .forecolor = rgb(100,200,255)
   .Print "you "
   .forecolor = vbcyan
   .Print "do "
end with

Gajendra
0
 
LVL 26

Accepted Solution

by:
EDDYKT earned 250 total points
ID: 9770682
0
 
LVL 1

Author Comment

by:TWCMIL
ID: 9774157
The problem here is that I continuously scroll the string with a timer control.
So I have a string I need to break up into 4 colors, color each part, then scroll it continuosly (looped). I already have the code that scrolls the text (and loops it so it always filles the width of the picture box). I also give the user the option to scroll smoothly or by character (less CPU power and faster speeds).

I have a Timer that fires this repeatedly at increments of 20ms to 500ms, the user controls the speed:

Sub ShowMessage(SCRText As String, ScrollSmooth As Boolean)

If ScrollSmooth = False Then

    Static MsgPtr As Integer
    Static MyText As String
    If Len(MyText) = 0 Then
        MsgPtr = 1
        MyText = SCRText
    Else
        MyText = SCRText & "¬"
    End If
    Picture1.Cls
    Picture1.Print Mid$(MyText, MsgPtr); MyText;
    'Don't move if the Scroll Lock Key is pressed
    If (GetKeyState(vbKeyScrollLock) And &H1&) = 0 Then
        MsgPtr = MsgPtr + 1
        If MsgPtr > Len(MyText) Then
            MsgPtr = 1
        End If
    End If
Else
    Static ScrollOffset As Long
    Dim LoopDraw As Long
    Dim TextSize As Long
    Dim TextPos As Long

    ' Get vertical text position and width
    TextPos = Picture1.ScaleY(ScrollPos, vbPixels, Picture1.ScaleMode)
    TextSize = Picture1.TextWidth(SCRText)

    ' Clear old drawing
    Picture1.Line (0, TextPos)-(Picture1.ScaleWidth, TextPos + Picture1.TextHeight(SCRText)), Picture1.BackColor, BF

    ' Draw the message enough times to fill the form
    For LoopDraw = -ScrollOffset To Picture1.ScaleWidth Step TextSize
        Picture1.CurrentX = LoopDraw
        Picture1.CurrentY = TextPos
        Picture1.Print SCRText
    Next LoopDraw

    ' Increment scroller offset and wrap when needed
    ScrollOffset = ScrollOffset + Picture1.ScaleX( _
        ScrollStep, vbPixels, Picture1.ScaleMode)
    If (ScrollOffset >= TextSize) Then _
        ScrollOffset = ScrollOffset - TextSize
End If
End Sub
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now