Click here to Skip to main content
15,868,030 members
Articles / Programming Languages / C# 3.5

Simple Yet Extensible Animation Framework for Windows Forms

Rate me:
Please Sign up or sign in to vote.
4.72/5 (8 votes)
23 Nov 2011CPOL2 min read 39.1K   2K   25   7
WPF has built in support for animating properties, this article demonstrates the same in Windows Forms.
Preview.png

Introduction

Just the wows of built in features of a WPF noob makes the purpose of this article. WPF has features as many as possible, which we used to take much more time to code in Windows Forms. One of the many key features of WPF is that it supports animating dependency properties.

This article demonstrates the concept and a simple yet extensible Animation framework for Windows Forms.

Check out the article Carousel Control for Windows Forms built over this animation framework. :)

Background

This project implements a generic interface that defines an Animation type, and invokes the animation on the target property using reflection.

It makes your work simple, animating the width of a button is as simple as in WPF! You can do wonders by animating some simple little things!!

C#
IntAnimation widthAnimation =
                new IntAnimation
{
    AutoReset = true,
    AutoReverse = true,
    From = 100,
    To = 250,
    By = 1,
    Interval = 10,
    Duration = 3000
};

this.button1.Animate<int>("Width", widthAnimation);

Isn't this simple enough? Read on to know how!!

Using the Code

IAnimation<T> Interface

  • IAnimation<T> interface implements the following fields, which represents the information required for animating a property. Type T marks the type of target property the animation handles. For instance, to animate Width of a Control, the type will be integer.
  • AutoReset: If set to true, animation will commence again when completed.
  • AutoReverse: If set to true, animation will happen in reverse direction once it's completed. When both AutoReset and AutoReverse are set to true, animation will happen forth and back endlessly.
  • Interval: The time span between the successive value changes.
  • Duration: The time to run the animation. (Note: When range of values and duration is known, interval between successive updates can be calculated in Animation implementation as an alternative.)
  • By: The value by which the target property is updated.
  • From: The value from which the animation will bring.
  • To: The value at which the animation completes.
  • GetNewValue(int delta): Provides the next successive value for update.
C#
/// <summary>
/// Defines IAnimation interface.
/// </summary>
/// <typeparam name="T">the type Animation is applied to</typeparam>
public interface IAnimation<T>
{
    bool AutoReset { get; set; }

    bool AutoReverse { get; set; }

    int Interval { get; set; }

    int Duration { get; set; }

    T By { get; set; }
        
    T From { get; set; }

    T To { get; set; }

    T GetNewValue(int delta);

    event AnimationEventHandler<T> Completed;

    event AnimationEventHandler<T> Started;

    event AnimationEventHandler<T> Freezed;
}

Animator

Animator defines static extension methods to invoke animation on the target property. and here is how it works.

#1. Gets the PropertyInfo of the target object by its name.

C#
/// <summary>
/// Gets the PropertyInfo of specified type on the target object.
/// </summary>
/// <param name="target" />the target object.
/// <param name="property" />the name of the property.
/// <param name="t" />the return type of the property.
/// <returns>the property info if found, null otherwise.</returns>
private static PropertyInfo GetPInfo(object target, string property, Type t)
{
    if (t == null || target == null || String.IsNullOrEmpty(property))
        return null;

    return target.GetType().GetProperty(property, t);
}

#2. Sets the updated value on the target property.

C#
pinfo.SetValue(target, newValue, null);

Actual work goes inside the static extension method Animator.Animate() this consumes the target object, the name of the property and the instance of animation type which holds the information about the type and values.

There are two timers running inside the Animator, one updates the value on the target property on specific interval and the other updates the status of the animator.

C#
/**** truncated for clarity ****/

animSteps.Tick += (_, __) =>
        {
            T newValue = animation.GetNewValue(++delta);
            pinfo.SetValue(target, newValue, null);
        }
    };

    animDuration.Elapsed += (_, __) =>
        {
            if (!state.IsFreezed)
            {
                delta = -1;
                state.IsReversed = animation.AutoReverse ? !state.IsReversed : false;
            }

            if (!animSteps.Enabled && !animDuration.Enabled)
                state.RaiseCompleted(target);
        };

Points of Interest

This is a generic implementation and it can be used to animate any public property of any target type. The source includes the implementation for types Int and double.

History

  • First post - 2nd October, 2011
  • Few editorial revisions - 3rd October, 2011
  • New sample added for advanced usage pattern - 4th October 2011

Hope this will help reduce a lot of time writing codes for animating. Be sure to leave your comments and suggestions!.

Happy coding!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India
I code, learn, read and listen.

Comments and Discussions

 
Questioninteresting article and a small suggestion Pin
BillWoodruff29-Jul-12 1:56
professionalBillWoodruff29-Jul-12 1:56 
AnswerRe: interesting article and a small suggestion Pin
VallarasuS29-Jul-12 2:45
VallarasuS29-Jul-12 2:45 
Suggestionyeah, winforms animation Pin
schallos5-Oct-11 1:40
schallos5-Oct-11 1:40 
GeneralRe: yeah, winforms animation Pin
VallarasuS5-Oct-11 5:59
VallarasuS5-Oct-11 5:59 
AnswerRe: yeah, winforms animation Pin
schallos5-Oct-11 6:18
schallos5-Oct-11 6:18 
GeneralMy vote of 5 Pin
HaBiX2-Oct-11 20:58
HaBiX2-Oct-11 20:58 
GeneralRe: My vote of 5 Pin
VallarasuS3-Oct-11 1:15
VallarasuS3-Oct-11 1:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.