5,545,925 members and growing! (18,106 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » Miscellaneous Controls     Intermediate

.NET Animation Control

By Martin Cook

A managed wrapper for the animation common control.
C#, Windows, .NET 1.1, .NETVisual Studio, VS.NET2003, Dev

Posted: 4 Mar 2005
Updated: 9 Feb 2006
Views: 76,789
Bookmarked: 48 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
19 votes for this Article.
Popularity: 5.82 Rating: 4.55 out of 5
1 vote, 5.3%
1
0 votes, 0.0%
2
1 vote, 5.3%
3
4 votes, 21.1%
4
13 votes, 68.4%
5

Sample Image - CGAnimation1.jpg

Introduction

A friend of mine recently asked for advice related to showing animations on a C# WinForm. The question reminded me of some code I wrote back in 2001 to create a managed wrapper for the Windows common animation control. This article outlines that control and demonstrates a possible use of the control with a small sample application.

Using the code

Using the control in an application is no different than using any other WinForm control. Essentially, you should follow these steps:

  • In Visual Studio, select the 'View' | 'Toolbox' option to display the toolbox.
  • Right mouse click over the toolbox (not on an icon) and choose 'Add/Remove items...'
  • On the 'Customize Toolbox' form, click the 'Browse...' button.
  • Browse to the place where you downloaded the CG.Animation.dll file (see the links at the top of the article).
  • Click OK (the toolbox should now include an icon for the animation control).
  • Open a form in the Visual Studio designer and drag/drop the animation control onto the form.

Once an instance of the animation control exists on the form, you may adjust the properties using the Visual Studio property designer. The properties exposed by the control include:

  • AVIFileType - Allows a developer to choose from a list of standard animations or select a custom animation.
  • AutoCenter - Automatically centers the animation in the control.
  • AutoPlay - Starts playing the animation automatically.
  • FileName - Used to specify the name of a custom animation file.
  • Transparent - Forces the animation to use the background color of the control.
  • UseTimer - Directs the underlying control to use a timer for internal updates.

Sample Image - maximum width is 600 pixels

The control also exposes a few methods for altering the behavior at runtime. Here is a list of those methods:

  • Open() - Opens the animation file specified by the AnimationType and FileName properties.
  • Close() - Closes the currently open animation.
  • Stop() - Stops the currently playing animation.
  • Play() - Plays the currently open animation from start to finish in an endless loop.
  • Play(int repetitions) - Plays the currently open animation from start to finish, the specified number of times.
  • Play(int repetitions, int startFrame, int endFrame) - Plays the currently open animation from the specified frames, looping the specified number of times.
  • Seek(int frame) - Positions the currently open animation to the specified frame.

That's pretty much it. The control is simple to use.

Inside the control

The control gains the ability to play animations by subclassing the 'SysAnimate32' Windows common control class. The subclassing step is performed by overriding the CreateParms property and specifying the name of the Windows class. Here is what that code looks like:

protected override CreateParams CreateParams
{
    get
    {
        // Create the parameters for the animation control.

        CreateParams parms = base.CreateParams;
        parms.ClassName = "SysAnimate32";

        // Apply the appropriate animation control styles.

        if (AutoCenter)
            parms.Style |= NativeMethods.ACS_CENTER;

        if (AutoPlay)
            parms.Style |= NativeMethods.ACS_AUTOPLAY;

        if (Transparent)
            parms.Style |= NativeMethods.ACS_TRANSPARENT;

        if (UseTimer)
            parms.Style |= NativeMethods.ACS_TIMER;

        return parms;
    } // End get

} // End CreateParms()

In order to use a Windows common control class, you should start by initializing the common control library. That step is performed using the following code:

protected override void CreateHandle()
{
    // Should we make sure the common control library is initialized?

    if (!RecreatingHandle)
    {
        NativeMethods.INITCOMMONCONTROLSEX iccex = 
            new NativeMethods.INITCOMMONCONTROLSEX(
                NativeMethods.ICC_ANIMATE_CLASS
            );
        NativeMethods.InitCommonControls(iccex);
    } // End if we should init the common control library.


    base.CreateHandle();
    
} // End CreateHandle()

The standard Windows animations are loaded dynamically from the shell32.dll library. I used this approach because I didn't want to embed standard AVI files in my application resources, and I figured most other people would feel the same way. The code to load the library resources is shown here:

private void OpenHelper(
    CGAVIFileType aviFileType
    )
{

    // If the control hasn't been created then simply exit.

    if (!IsHandleCreated)
        return;

    // Close any currently open animation.

    Close();
    
    // Should we load the shell library?

    if (m_hShellModule == 0)
        m_hShellModule = NativeMethods.LoadLibraryEx(
            "shell32.dll", 
            0, 
            2
            );
    
    // Did the animation open?

    m_isOpen = (NativeMethods.SendMessage(
        new HandleRef(this, Handle),
        NativeMethods.ACM_OPEN, 
        m_hShellModule, 
        (int)aviFileType
        ) != 0);

    // Should we automtically play the animation?

    if (AutoPlay)
        Play();

} // End OpenHelper()

The ACM_OPEN message is defined by Windows and is used to load an AVI resource at runtime. In this case, from the shell32.dll library. To open a custom (external) AVI file instead, the control uses the following code:

private void OpenHelper(
    string fileName
    )
{

    // If the control hasn't been created then simply exit.

    if (!IsHandleCreated)
        return;

    // Close any currently open animation.

    Close();
            
    // Did the animation open?

    m_isOpen = (NativeMethods.SendMessage(
        new HandleRef(this, Handle),
        NativeMethods.ACM_OPEN, 
        0, 
        fileName
        ) != 0);

    // Should we automtically play the animation?

    if (AutoPlay)
        Play();

} // End OpenHelper()

Conclusion

That's pretty much all that is interesting about the internals of the control. I have used the code for a number of years without problems but there is always room for improvement - right? I cleaned things up somewhat before I published the project (be glad you don't have to look at my C# code from 2001). As always, I am interested in any feedback or suggestions.

Have fun! :o)

History

I wrote the original code in 2001. I have updated things sporadically since then but I haven't always maintained versions and change history. I decided to bump the version number up to 2.0 for this release.

Version 2.1 --> I changed the names of the enumerated animation types and the default size of the control in order to differentiate my code from other animation controls.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Martin Cook


I am a C# developer specializing in creating object-oriented software for Microsoft Windows. When I am not programming I enjoy reading, playing the guitar/piano, running, watching New York Ranger hockey, designing and building wacky electronic devices, and of course enjoying good times with my wife and children.

Oh yea, and I also blog about writing C# code at CodeGator.com - come check it out!


Occupation: Web Developer
Location: United States United States

Other popular Miscellaneous articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 50 (Total in Forum: 50) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralIsPlayingmemberphil_the6:34 9 Oct '07  
GeneralDoesn't display on some machinesmemberphil_the3:40 5 Oct '07  
GeneralRe: Doesn't display on some machinesmemberMartin Cook4:10 8 Oct '07  
GeneralRe: Doesn't display on some machinesmemberphil_the6:26 9 Oct '07  
Generalthankmemberamin keshani0:00 1 Feb '07  
QuestionNo anim on releasememberalbls1:08 1 Dec '06  
AnswerRe: No anim on releasememberalbls1:27 1 Dec '06  
Generalhimemberskb16:07 10 Nov '06  
GeneralNot all AVI files are opened...membermounoune4:24 26 Oct '06  
GeneralRe: Not all AVI files are opened...memberMartin Cook10:44 26 Oct '06  
GeneralRe: Not all AVI files are opened...membermounoune21:28 26 Oct '06  
GeneralRe: Not all AVI files are opened...memberphil_the2:36 1 Oct '07  
GeneralRe: Not all AVI files are opened...memberphil_the1:01 5 Oct '07  
GeneralWindows 98memberNautilus200022:48 16 Feb '06  
GeneralRe: Windows 98memberMartin Cook6:28 17 Feb '06  
GeneralRe: Windows 98memberNautilus200021:19 17 Feb '06  
GeneralVS 2005 issuememberEric Marcon12:13 8 Feb '06  
GeneralRe: VS 2005 issuememberMartin Cook4:31 9 Feb '06  
GeneralRe: VS 2005 issuememberMartin Cook6:34 9 Feb '06  
GeneralRe: VS 2005 issuememberEric Marcon8:22 9 Feb '06  
GeneralRe: VS 2005 issuememberJensB21:11 7 Sep '06  
GeneralRe: VS 2005 issuememberMartin Cook11:53 26 Sep '06  
GeneralXP Style problemmembercecildt3:20 7 Feb '06  
GeneralRe: XP Style problemmemberMartin Cook4:29 9 Feb '06  
GeneralRe: XP Style problemmemberMartin Cook6:35 9 Feb '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Feb 2006
Editor: Smitha Vijayan
Copyright 2005 by Martin Cook
Everything else Copyright © CodeProject, 1999-2008
Web07 | Advertise on the Code Project