Results 1 to 9 of 9

Thread: Fading form BackColor?

  1. #1
    Guest

    Cool

    Hi, I'm using this code to fade the BackColor of my form:

    Code:
        'Declares variables for the fader
        Dim Red As Integer
        Dim Green As Integer
        Dim Blue As Integer
        Dim i As Integer
        
        'Colors to fade to/from
        For Red% = 0 To 12 Step 1
        For Green% = 0 To 0
        For Blue% = 0 To 246 Step 1
            'how to fade
            Form1.BackColor = RGB(Red%, Green%, Blue%)
        For i = 1 To 31999
        tmrFader.Enabled = False
        Next
        Next
        Next
        Next
    Now, this code works as I want it to, but its incredibly unstable. I'm using Win2K and VB6(No SP), and this little application crashes all the time. Does anybody see if I've done anything that I shouldn't have? Or maybe somebody can recommend a better way to do this.

  2. #2
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Hum... first thing is, I don't understand the point of these 3 lines:

    Code:
        For i = 1 To 31999
        tmrFader.Enabled = False
        Next
    Otherwise I don't see anything wrong, except for the useless loop for the Green that is always 0

  3. #3
    Guest

    Exclamation Because

    I intend to use this code for more than one fade, thats why I have the Green loop. The For i = 1 To 31999 Is to delay the fading a bit (31999 was the highest number I could use without causing an error, don't ask me why). And I need the tmrFader.Enabled = False to show only one fade instead of three (I have no idea why but thats the way it is). And the Next is obviously there because of the For i =...


  4. #4
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    The error in your code is that it will loop 12 times incrementing Red by one each time and inside each of these it will loop 246 times incrementing Blue each time...

    This is a modified version of your code. Remember, it uses only one loop:

    Code:
    Private Sub Form_Click()
        'Declare variables for the fader
        Dim i As Long
        
        'This is the final color's RGB!
        Const R = 255
        Const G = 128
        Const B = 64
        
        For i = 0 To 255
            'How to fade
            Form1.BackColor = RGB(R * i \ 256, G * i \ 256, B * i \ 256)
            
            'So the loop is interruptible and you can
            'click Close and other things
            DoEvents
        Next
    End Sub
    Modify the color, click the form and see it all happen

  5. #5
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    The other loop you used (0 to 31999 or something) will not work well because it will run very slow in a slow computer and very fast in a fast computer... forgot to tell you. Try a Timer instead: declare "i" using Static instead of Dim (so it is still there even when you get out of the sub), remove the loop and put it in the timer's sub.

  6. #6
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    Here is a function that a created to fade a form (or any object that has a BackColor property) a while back that you can use if you want to....

    In a module
    Code:
    Option Explicit
    
    Private Type RGBColor
        R As Long
        G As Long
        B  As Long
     End Type
    
    Public Sub FadeForm(pCtl As Object, pStartCol As Long, pEndCol As Long, pLoopCt As Long)
     Dim rgbS As RGBColor
     Dim rgbE As RGBColor
     Dim iRChg As Single, iGChg As Single, iBChg As Single
     Dim lCurColor As Long
     Dim iCt As Integer
     
     ' Retrieve the individual red, green, and blue values for the start color
     rgbS = IndColors(pStartCol)
     
     ' Retrieve the individual red, green, and blue values for the start color
     rgbE = IndColors(pEndCol)
     
     ' Get the amount to change the colors during the loop
     iRChg = (rgbE.R - rgbS.R) / pLoopCt
     iGChg = (rgbE.G - rgbS.G) / pLoopCt
     iBChg = (rgbE.B - rgbS.B) / pLoopCt
     
     For iCt = 1 To pLoopCt
        
        'get the value of the current color
        lCurColor = RGB(rgbS.R + iRChg * iCt, rgbS.G + iGChg * iCt, rgbS.B + iBChg * iCt)
        
        'display the  current color of the form
        pCtl.BackColor = lCurColor
        
        'allow other processes to execute
        DoEvents
        
     Next iCt
     
    End Sub
    
    Private Function IndColors(pCol As Long) As RGBColor
    
    With IndColors
    
        .B = pCol \ 65536
        .G = (pCol - .B * 65536) \ 256
        .R = pCol - .B * 65536 - .G * 256
        
    End With
    
    End Function
    And put this code in a form

    Code:
    Option Explicit
    
    Private Sub Form_Load()
        
        Me.Show
        
        ' Fade a form from Blue to Green, run the coloring loop 10000 times
        FadeForm Me, vbBlue, vbGreen, 10000
        
    End Sub
    If the sub is fading to quickly just increase the value of the pLoopCt parameter higher than 10000 to make the fading effect slow down, or if you want it to fade faster decrease the pLoopCt parameter. Enjoy!
    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

  7. #7
    Guest

    Arrow Que pasa?

    'Ello, thanks for the code, now my computer has stopped crashing . I used your code, Jotaf (didn't understand much of YoungBucks..). But what was it you said I should do if I wanted to delay the fade? Didn't understand what you wanted me to do... Mind telling me?

  8. #8
    Member
    Join Date
    Oct 2000
    Posts
    33
    You can use the sleep API command to make it pause.

  9. #9
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457

    Wink Sure

    YoundBuck's code is good, it allows you to fade not only from black to a color, but from any two colors! Try to study it a bit more

    This code assumes the timer's name is "Timer1":

    Code:
    Private Sub Timer1_Timer()
        'Declare variables for the fader
        Static i As Long
        
        'This is the final color's RGB!
        Const R = 255
        Const G = 128
        Const B = 64
        
        If i < 255 Then
            'How to fade
            Form1.BackColor = RGB(R * i \ 256, G * i \ 256, B * i \ 256)
            
            'NOTE: I removed DoEvents because now the
            'events happen between each Timer event ;)
            
            'Increase the count
            i = i + 1
        Else
            '"i" is bigger than 255, so stop
            Timer1.Enabled = False
        End If
    End Sub
    Hope that helped

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