Solved

Different colored text in a single picturebox?

Posted on 2003-11-17
6
276 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 3

Expert Comment

by:gajendra
Comment Utility
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
Comment Utility
0
 
LVL 1

Author Comment

by:TWCMIL
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 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

8 Experts available now in Live!

Get 1:1 Help Now