Re: Fade into a Picture Box or Image Control
- From: "Mike D Sutton" <EDais@xxxxxxxx>
- Date: Mon, 13 Jun 2005 23:54:26 +0100
> Still pretty damn fast! Thanks, my friend.
>
> I have got this fade-in working reasonably well but I would like the effect
> to be just as smooth as possible while only taking about 2 seconds from
> start to finish.
>
> I'm calling the following timer event every 20 ms. After the alpha value is
> increased and AlphaBlend() is called, I am calling my PictureBox.Refresh to
> display the changes. Is there a better way than .Refresh to allow User to
> see this effect?
The killer with fixed increments like this is that whilst it may play great on your hardware accelerated hyperthreaded
quad-Zeon, Joe users old P3 only manages to draw a couple of frames a second and the same animation takes forever. The
way to circumvent this problem but still retain the high performance where available is to make your animation time
based rather than frame based, this also means you can drop the timer control which can actually slow down the
performance on faster machines because it doesn't 'tick' fast enough!
To change your animation to a fixed duration, you'll need something like this:
'***
Private Declare Sub Sleep Lib "Kernel32.dll" (ByVal dwMilliseconds As Long)
Private Declare Function timeGetTime Lib "WinMM.dll" () As Long
Private Sub RunFade(ByVal inFrom As Byte, ByVal inTo As Byte, ByVal inDuration As Single)
Dim StartTime As Long, EndTime As Long, TimeNow As Long
Dim MSecDuration As Long
Dim Opacity As Byte
' Don't be silly..
If (inDuration <= 0) Then Exit Sub
' Work out animation parameters
StartTime = timeGetTime()
MSecDuration = CLng(inDuration * 1000)
EndTime = StartTime + MSecDuration
TimeNow = StartTime
Do
' Where am I?
Opacity = (((TimeNow - StartTime) / MSecDuration) * (inTo - inFrom)) + inFrom
Call DrawFrame(Opacity)
' You're feeling sleepy...
Call Sleep(1)
' You could stick DoEvents in here if you want your app to remain responsive
' Woah, what time is it now??
TimeNow = timeGetTime()
Loop While (TimeNow < EndTime)
' Always ensure you draw the end point
Call DrawFrame(inTo)
End Sub
'***
Where DrawFrame() draw the frame at the given opacity, I prefer doing this rather than cluttering the animation loop
with checks like If (Opacity = 0) Then Don't Draw Else If (Opacity = &HFF) Then BitBlt() rather than AlphaBlend() etc.
This assumes that you've only got one animation and it therefore knows where it's going, you could however pass in an
hDC and/or coordinates and pass those down through to DrawFrame(). In that situation you could also kick this routine
off multiple times, each rendering to different DC's and even though they'll be stealing CPU cycles off one another both
animations will still last only as long as you requested (and however long it takes to draw the final frame.)
If you want to be really slick and exact timing is important, you can cache the last few frame draw (and delay) times
and predict whether the next frame will be worth drawing or you skip to the final frame - There's lots of stuff you can
do but it really depends what your requirements are.
As far as calling .Refresh() goes, double buffered drawing takes slightly longer than single buffered (AutoRedraw =
False) however when you're talking about AlphaBlending() which in itself takes a lot of CPU time (assuming no hardware
acceleration for the time being) then it's neither here nor there. IMO unless you want to drop back to single buffered
drawing and the difficulties that can sometimes impose then it's somewhat of a non-issue, if performance is a real
problem then you should be using a dedicated graphics library for the rendering such as OpenGL or DirectX.
Hope this helps,
Mike
- Microsoft Visual Basic MVP -
E-Mail: EDais@xxxxxxxx
WWW: Http://EDais.mvps.org/
.
- Follow-Ups:
- Re: Fade into a Picture Box or Image Control
- From: George Yachán
- Re: Fade into a Picture Box or Image Control
- References:
- Fade into a Picture Box or Image Control
- From: George Yachán
- Re: Fade into a Picture Box or Image Control
- From: Mike D Sutton
- Re: Fade into a Picture Box or Image Control
- From: George Yachán
- Re: Fade into a Picture Box or Image Control
- From: Mike D Sutton
- Re: Fade into a Picture Box or Image Control
- From: George Yachán
- Fade into a Picture Box or Image Control
- Prev by Date: Re: Fade into a Picture Box or Image Control
- Next by Date: Re: Fade into a Picture Box or Image Control
- Previous by thread: Re: Fade into a Picture Box or Image Control
- Next by thread: Re: Fade into a Picture Box or Image Control
- Index(es):
Relevant Pages
|