Results 1 to 7 of 7

Thread: Text fader (2008)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2002
    Posts
    129

    Text fader (2008)

    Hello,

    Could anyone provide me with some help or a snippet of code that will enable me to fade text from one colour to another in a rich textbox. I have tried searching the forum but can't seem to find anything relevant.

    Regards,

    Dan.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Text fader (2008)

    try this. i found it on the internet + modified it. it fades the rtb forecolor from color1 to color2

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim mR As Integer
    4.     Dim mStepR As Integer
    5.  
    6.     Dim mG As Integer
    7.     Dim mStepG As Integer
    8.  
    9.     Dim mB As Integer
    10.     Dim mStepB As Integer
    11.  
    12.     Dim mStatus As Integer ' 0 -> Stopped  1-> Fading
    13.  
    14.     Const kSteps As Integer = 10 ' Number of colors between
    15.     Dim mContSteps As Integer
    16.     ' Different States
    17.     Const kStatusStopped As Integer = 0
    18.     Const kStatusFading As Integer = 1
    19.  
    20.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    21.         InitFade(Color.Red, Color.Blue)
    22.     End Sub
    23.  
    24.     Sub InitFade(ByVal color1 As Color, ByVal color2 As Color)
    25.  
    26.         Dim wRAct As Integer = color1.R
    27.         Dim wGAct As Integer = color1.G
    28.         Dim wBAct As Integer = color1.B
    29.  
    30.         Dim wRFin As Integer = color2.R
    31.         Dim wGFin As Integer = color2.G
    32.         Dim wBFin As Integer = color2.B
    33.  
    34.  
    35.         ' Calculate intermediate steps between each of the components
    36.         mStepR = CInt((wRAct - wRFin) / kSteps)
    37.         mStepG = CInt((wGAct - wGFin) / kSteps)
    38.         mStepB = CInt((wBAct - wBFin) / kSteps)
    39.  
    40.         ' Initializes Module variables
    41.         mR = wRAct
    42.         mG = wGAct
    43.         mB = wBAct
    44.         ' Init # of Steps
    45.         mContSteps = 0
    46.         ' Indicates Timer Event to process fade effect
    47.         mStatus = kStatusFading
    48.     End Sub
    49.  
    50.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    51.         If mStatus = kStatusFading Then
    52.             mContSteps += 1
    53.             If mContSteps = kSteps Then
    54.                 mStatus = kStatusStopped
    55.             Else
    56.                 If mR - mStepR > 0 Then mR -= mStepR Else mR = 0
    57.                 If mG - mStepG > 0 Then mG -= mStepG Else mG = 0
    58.                 If mB - mStepB > 0 Then mB -= mStepB Else mB = 0                
    59.                 RichTextBox1.ForeColor = Color.FromArgb(mR, mG, mB)
    60.                 Application.DoEvents()
    61.             End If
    62.         End If
    63.     End Sub
    64.  
    65. End Class

    edit: the timer interval is 100ms.
    if you want to fade the selected text, it'd be slightly more difficult because the text would be hilighted + you wouldn't see the effects
    Last edited by .paul.; Sep 11th, 2008 at 06:18 PM.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2002
    Posts
    129

    Re: Text fader (2008)

    Thanks for the help, That is a great effect but not exactly what I was looking for.


    I want the fade to be static and fade from left to right.

    Any other ideas,

    Regards,

    Dan.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Text fader (2008)

    in a richtextbox you'll only be able to fade by character. that is 1 character can only be 1 color

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Dec 2002
    Posts
    129

    Re: Text fader (2008)

    That's fine as long as the next character is almost the same colour as the one before it and gives the effect it is fading throughout the text.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Text fader (2008)

    try this

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    4.         InitFade(Color.Red, Color.Blue)
    5.     End Sub
    6.  
    7.     Sub InitFade(ByVal color1 As Color, ByVal color2 As Color)
    8.  
    9.         Dim steps As Integer = RichTextBox1.Text.Length ' Number of colors between
    10.  
    11.         Dim wRStart As Integer = color1.R
    12.         Dim wGStart As Integer = color1.G
    13.         Dim wBStart As Integer = color1.B
    14.  
    15.         Dim wRFin As Integer = color2.R
    16.         Dim wGFin As Integer = color2.G
    17.         Dim wBFin As Integer = color2.B
    18.  
    19.         ' Calculate intermediate steps between each of the components
    20.         Dim mStepR As Integer = CInt((wRStart - wRFin) / steps)
    21.         Dim mStepG As Integer = CInt((wGStart - wGFin) / steps)
    22.         Dim mStepB As Integer = CInt((wBStart - wBFin) / steps)
    23.  
    24.         ' Initializes Module variables
    25.         Dim mR As Integer = wRStart
    26.         Dim mG As Integer = wGStart
    27.         Dim mB As Integer = wBStart
    28.  
    29.         For x As Integer = 1 To steps
    30.  
    31.             RichTextBox1.SelectionStart = x - 1
    32.             RichTextBox1.SelectionLength = 1
    33.             RichTextBox1.SelectionColor = Color.FromArgb(mR, mG, mB)
    34.             Application.DoEvents()
    35.  
    36.             If mR - mStepR > 0 Then mR -= mStepR Else mR = 0
    37.             If mG - mStepG > 0 Then mG -= mStepG Else mG = 0
    38.             If mB - mStepB > 0 Then mB -= mStepB Else mB = 0
    39.  
    40.         Next
    41.  
    42.     End Sub
    43.  
    44. End Class

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Dec 2002
    Posts
    129

    Re: Text fader (2008)

    That is absolutely perfect!! Thanks a lot!

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