Solved

Different colored text in a single picturebox?

Posted on 2003-11-17
6
289 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

830 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