Click to See Complete Forum and Search --> : Fading form BackColor?
Hi, I'm using this code to fade the BackColor of my form:
'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.
Jotaf98
Jan 12th, 2001, 05:46 PM
Hum... first thing is, I don't understand the point of these 3 lines:
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 :p
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 =...
Jotaf98
Jan 12th, 2001, 06:31 PM
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:
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 ;)
Jotaf98
Jan 12th, 2001, 06:34 PM
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.
YoungBuck
Jan 12th, 2001, 11:00 PM
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
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
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! :)
'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?
schnarf283
Jan 13th, 2001, 08:26 AM
You can use the sleep API command to make it pause.
Jotaf98
Jan 16th, 2001, 10:59 AM
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":
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 ;)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.