Results 1 to 3 of 3

Thread: [2008] Fade effect for text

  1. #1

    Thread Starter
    Fanatic Member dom_stapleton's Avatar
    Join Date
    Sep 2005
    Location
    Leigh-on-Sea, UK
    Posts
    665

    [2008] Fade effect for text

    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
    I use Microsoft Visual Basic 2008 Express Edition.

    If my post has been helpful, please rate it, unless you don't believe in Karma... which actually I don't!

    Resources:
    Visual Basic Tutorials (1, 2) | MSDN Library | Google | Krugle | Search Forums

    Free components:
    Windows Forms Components | XP Common Controls Library

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [2008] Fade effect for text

    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.
    My usual boring signature: Nothing

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Fade effect for text

    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:
    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
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width