Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Auto contrasting text against a changing background

Status
Not open for further replies.

IknowMe

Programmer
Aug 6, 2004
1,214
US
I am using the following Sub to add a percentage complete to the center of a progress bar.

Code:
    Private Sub UpdatePBar()

        Dim x As Single
        Dim y As Single
        Dim percentage As String = CType((ProgressBar1.Value / ProgressBar1.Maximum * 100), Integer).ToString & "%"
        Dim gr As Graphics = Me.ProgressBar1.CreateGraphics

        Dim sz As SizeF = gr.MeasureString(percentage, Me.ProgressBar1.Font, Me.ProgressBar1.Width)

        x = (Me.ProgressBar1.Width / 2) - (sz.Width / 2)
        y = (Me.ProgressBar1.Height / 2) - (sz.Height / 2)
        Me.ProgressBar1.Refresh()
        gr.DrawString(percentage, ProgressBar1.Font, Brushes.Black, x, y)

    End Sub

Problem is when I get near 50% complete the text is lost in the background color. I have switched to a brush color which contrasts ok against both the white and blue colors of the progress bar but am interested in learning how to inverse the color as the background changes. I did it with a if statement when I hit 50% but end up losing the first or last digit as the progress bar moves through the text.

Still looking for an example so if I find one I'll post back but in the meantime if anyone has a suggestion or can give me a better idea to narrow the search i'd really appreciate it.

[small]Sometimes you gotta leave your zone of safety. You have to manufacture Inspirado. You gotta get out of the apartment. You've got to run with the wolves. You've got to dive into the ocean and fight with the sharks. Or just treat yourself to a delicious hot fudge sundae........ with nuts. - Jack Black[/small]
 
found example here

[small]Sometimes you gotta leave your zone of safety. You have to manufacture Inspirado. You gotta get out of the apartment. You've got to run with the wolves. You've got to dive into the ocean and fight with the sharks. Or just treat yourself to a delicious hot fudge sundae........ with nuts. - Jack Black[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top