|
-
Jan 12th, 2001, 05:09 PM
#1
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.
-
Jan 12th, 2001, 06:46 PM
#2
Frenzied Member
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
-
Jan 12th, 2001, 07:06 PM
#3
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 =...
-
Jan 12th, 2001, 07:31 PM
#4
Frenzied Member
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
-
Jan 12th, 2001, 07:34 PM
#5
Frenzied Member
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.
-
Jan 13th, 2001, 12:00 AM
#6
Fanatic Member
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}
-
Jan 13th, 2001, 09:05 AM
#7
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?
-
Jan 13th, 2001, 09:26 AM
#8
Member
You can use the sleep API command to make it pause.
-
Jan 16th, 2001, 11:59 AM
#9
Frenzied Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|