Hack Forums
[.NET] Easily animate your custom controls with mutimedia timer - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Easily animate your custom controls with mutimedia timer (/showthread.php?tid=5541623)



Easily animate your custom controls with mutimedia timer - hfTP54 - Feb 2, 2017

Easily animate your custom controls with mutimedia timer

Using the given code any control(or form) can be invalidated every millisecond (yes every 1ms not 15.6ms), so that you could animate it easily.

First add the following to your project:
Link to code


How to implement
Suppose I have a control called c1 (or a thembase):
PHP Code

<?php 
Class c1
    Inherits Control
End 
Class 

To use the code, just add the line Implements AnimatedObject
Some errors pop up at the word 'AnimatedObject'. Just ignore them and paste this:
PHP Code

<?php 
Public Property animating As Boolean Implements AnimatedObject.animating
    
Public ReadOnly Property designing As Boolean Implements AnimatedObject.designing
        Get
            
Return DesignMode
        End Get
    End Property

    
Public Sub leavemouse(e As EventArgs) Implements AnimatedObject.leavemouse
        OnMouseLeave
(e)
    
End Sub

    
Public Sub AnimatedObject_invalidate() Implements AnimatedObject.invalidate
        Invalidate
()
    
End Sub 

The above was done so that our ticker function can invalidate controls of all type since AnimatedObject is an interface, it doesn't matter what base control you've inherited.
For people who use a themebase, just do the above in the themebase else you have to add it to your control(another reason to use themebase).


How to use

In the constructor, add
PHP Code

<?php 
addanimatedobject
(me) 
This adds your control to the list of animated objects.


Similarly, in destructor ( dispose() func. ), add:
PHP Code

<?php 
removeanimatedobject
(me) 


Now, when you set the animating property of your control to true, your control starts invalidating every 1ms. You can put your animation code like calculating values in the top of your onpaint() override to do it every 1ms.
To stop, just set animating=false.


Some would say why not just use a timer, for them:
  • Both System.Timers.Timer and System.Threading.Timer have least period of almost 16ms. This one has almost 1ms. That's the reason why I ditched aeon's timer.

  • Instead of adding a seperate instance of timer for all of your controls, even when you are using a themebase, all invalidations are done by 1 timer (had to put it in a module)

  • IDK, I like to use stuff coded my myself :P






About the leavemouse() sub
The leavemouse sub can be used to trigger the onmouseleave() event of a control whenever the mouse passes over another control/form.

All of the above can be added to your themebase




Credits:



* - SIM-1847363 - Feb 3, 2017

I still have to look over this, overall doesn't look bad, in my case I would have used threading.