Hi, does anyone know how you can implement a fade effect for text so that as a piece of text changes it seems to fade in and out, like a transition.
Thanks in advance,
Dom
Printable View
Hi, does anyone know how you can implement a fade effect for text so that as a piece of text changes it seems to fade in and out, like a transition.
Thanks in advance,
Dom
First you need a timer. You'd change something every timer tick. The rate of fading would be based on the speed of the timer, and the amount of change.
As to what you change, it seems like there would be two options. The simplest would probably be to change the transparency or the alpha level of the text. Alpha level would be part of the forecolor. I've never tried either, but it's one of those (transparency may just be boolean, in which case that's not the right one).
A somewhat more interesting solution would be to look at the forecolor of the text, and the backcolor. Get the differences between the two for the three color chanels (Red, Green, Blue), then differentially move those three chanels until the forecolor matches the back color. For example, if the back color was 255,0,0 (bright red), and the forecolor was 0,0,255 (bright blue), then the differences are: Red:255, Green: 0, and Blue: 255. Now, suppose you wanted to fade the thing out in ten seconds. The steps would be these:
Set the timer interval to 500.
255/20= approximately 26, so on each tick Red+=26 and Blue-=26
Creating a little function that would do that gradation automatically for all three colors would not be too difficult, and the result would be more than a simple fade. After all, in the above example, even though green is not even used, the text would turn purple, then red, then vanish completely. If the back color was gray, the text could go through a fairly colorful sequence before fading.
Controls don't support transparent ForeColors. If you want to do it in a control then you'll have to use Shaggy's second idea. If you can do it with GDI+ then you can use Shaggy's first idea. Try this:To make that a bit more efficient you can Invalidate only the area containing the text but I couldn't be bothered for an example.Code:Public Class Form1
Private alpha As Integer = 255
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Timer1.Interval = 100
Me.Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Me.alpha -= 5
If Me.alpha < 0 Then
Me.alpha = 0
End If
If Me.alpha = 0 Then
Me.Timer1.Stop()
End If
Me.Refresh()
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim b As New SolidBrush(Color.FromArgb(Me.alpha, 0, 0, 0))
e.Graphics.DrawString("I feel like I'm fading away.", Me.Font, b, 100, 100)
End Sub
End Class