Solved

Different colored text in a single picturebox?

Posted on 2003-11-17
6
285 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

776 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