Click here to Skip to main content
15,867,835 members
Articles / Programming Languages / C#
Article

C# Form Animation with Windows API

Rate me:
Please Sign up or sign in to vote.
4.47/5 (37 votes)
10 Jan 2007CPOL2 min read 183.8K   18.8K   72   42
The article demonstrates Windows Form Animation using Windows API
C# Form Animation using Windows API

Introduction

This is my first ever article. I have been working with so many applications of-late but never thought of publishing any of my work online. I am referring to this site since long, and now I think it's pay back time for me.

Some Background

If you are familiar with Windows APIs, then you must be aware that they provide a great interface for developing out-of-the-box applications. There are so many of them available for use, but most of the time programmers hesitate to use them because there are some complexities involved in embedding them to the core application.

How To Use the Code

Just download and run the demo to see the animation in action.

How It Works

This article demonstrates the use of one of the Windows APIs called AnimateWindow. The API, among so many others is found in the user32.dll library. Detailed explanation of this API can be found here. A call to this API requires three arguments:

  1. A handle to the window (form) to be animated
  2. Animation duration (in milliseconds)
  3. Some flags describing the type of animation

The purpose of this application is fairly simple. The application will demonstrate the form animation using the AnimateWindow API. There are two forms in this application. One provides the user with various options that can be used to animate any form. The other just shows the form animation effect in action.

I have created a separate class WinAPI which is used to declare the constants that are used when calling the API and the API itself. The code for this class looks like this:

C#
public class WinAPI
    {
        /// <summary>
        /// Animates the window from left to right. 
        /// This flag can be used with roll or slide animation.
        /// </summary>
        public const int AW_HOR_POSITIVE = 0X1;
        /// <summary>
        /// Animates the window from right to left. 
        /// This flag can be used with roll or slide animation.
        /// </summary>
        public const int AW_HOR_NEGATIVE = 0X2;
        /// <summary>
        /// Animates the window from top to bottom. 
        /// This flag can be used with roll or slide animation.
        /// </summary>
        public const int AW_VER_POSITIVE = 0X4;
        /// <summary>
        /// Animates the window from bottom to top. 
        /// This flag can be used with roll or slide animation.
        /// </summary>
        public const int AW_VER_NEGATIVE = 0X8;
        /// <summary>
        /// Makes the window appear to collapse inward 
        /// if AW_HIDE is used or expand outward if the AW_HIDE is not used.
        /// </summary>
        public const int AW_CENTER = 0X10;
        /// <summary>
        /// Hides the window. By default, the window is shown.
        /// </summary>
        public const int AW_HIDE = 0X10000;
        /// <summary>
        /// Activates the window.
        /// </summary>
        public const int AW_ACTIVATE = 0X20000;
        /// <summary>
        /// Uses slide animation. By default, roll animation is used.
        /// </summary>
        public const int AW_SLIDE = 0X40000; 
        /// <summary>
        /// Uses a fade effect. 
        /// This flag can be used only if hwnd is a top-level window.
        /// </summary>
        public const int AW_BLEND = 0X80000;
        /// <summary>
        /// Animates a window.
        /// </summary>
        [DllImport("user32.dll", CharSet=CharSet.Auto)]
        public static  extern int AnimateWindow 
		(IntPtr hwand , int dwTime , int dwFlags) ;        
    } 

The Animated Child Form

The animated form has very little code in the Load event, a call to the WinAPI class's static implementation of the original API.

What's more, the form can be animated when it appears and/or disappears.

C#
WinAPI.AnimateWindow (this.Handle, animationTime, flags);

The Parent Form

The parent form is used to set the parameters of the animation and load the animated form. There are 6 buttons in this form in total. Listed below are the types of animations:

  1. Basic Animations
    1. Roll Left to Right
    2. Roll Right to Left
    3. Roll Top to Bottom
    4. Roll Bottom to Top
    5. Roll Collapse
    6. Roll Fade
  2. Slide Effect
    1. Slide Left to Right
    2. Slide Right to Left
    3. Slide Top to Bottom
    4. Slide Bottom to Top

Hope this code helps!!!

History

  • 10th January, 2007: Initial post

License

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


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionIs it supposed to work with Application.Run()? Pin
Member 1189510921-Apr-17 10:32
Member 1189510921-Apr-17 10:32 
GeneralThanks!!! Pin
Member 1170028018-May-15 9:11
Member 1170028018-May-15 9:11 
Questionexcellent Pin
lankara26-Mar-14 1:53
lankara26-Mar-14 1:53 
AnswerRe: excellent Pin
Dhimant Trivedi27-Mar-14 1:36
Dhimant Trivedi27-Mar-14 1:36 
Questionworking Pin
Member 1061272620-Feb-14 8:16
Member 1061272620-Feb-14 8:16 
AnswerRe: working Pin
Member 118006506-Jul-15 20:43
Member 118006506-Jul-15 20:43 
Questionnot working in maximize mod Pin
Member 771455918-Jan-14 0:20
Member 771455918-Jan-14 0:20 
AnswerRe: not working in maximize mod Pin
Dhimant Trivedi19-Jan-14 19:35
Dhimant Trivedi19-Jan-14 19:35 
GeneralC# Pin
sajad_zero1-Aug-13 22:06
sajad_zero1-Aug-13 22:06 
GeneralAwwwwwwsome! Pin
KrNeS5-Jul-13 6:15
KrNeS5-Jul-13 6:15 
GeneralRe: Awwwwwwsome! Pin
Dhimant Trivedi5-Jul-13 21:04
Dhimant Trivedi5-Jul-13 21:04 
GeneralMy vote of 5 Pin
Vladimi-Krylov10-Oct-12 20:01
Vladimi-Krylov10-Oct-12 20:01 
GeneralRe: My vote of 5 Pin
Dhimant Trivedi11-Oct-12 0:25
Dhimant Trivedi11-Oct-12 0:25 
GeneralMy vote of 4 Pin
NaveenSoftwares15-Jun-11 12:49
NaveenSoftwares15-Jun-11 12:49 
GeneralWindows 7 Support Pin
Scott G Blood13-Oct-10 6:22
Scott G Blood13-Oct-10 6:22 
AnswerRe: Windows 7 Support Pin
Dhimant Trivedi15-Oct-10 4:24
Dhimant Trivedi15-Oct-10 4:24 
GeneralHiding Form With Animated Effect Pin
atul paliwal16-Sep-10 21:18
atul paliwal16-Sep-10 21:18 
GeneralMy vote of 5 Pin
peejay0220-Jul-10 20:41
peejay0220-Jul-10 20:41 
GeneralGood work and suggestion Pin
Khaniya3-Mar-10 20:01
professionalKhaniya3-Mar-10 20:01 
QuestionHow can you make this to a Usercontrol added to a panel instead of form Pin
Lohith Pandit18-Feb-10 20:51
Lohith Pandit18-Feb-10 20:51 
QuestionDid you know SlideControl? Pin
cheind17-Jan-10 19:12
cheind17-Jan-10 19:12 
QuestionUsed in Mobile device? Pin
dannykuo30-Jul-09 20:53
dannykuo30-Jul-09 20:53 
GeneralHiding form with AW_SLIDE Pin
some1s6915-Jan-09 11:51
some1s6915-Jan-09 11:51 
GeneralRe: Hiding form with AW_SLIDE Pin
Dhimant Trivedi18-Jan-09 22:34
Dhimant Trivedi18-Jan-09 22:34 
QuestionSet hide form as default on init?? Pin
idrivefastlane28-Dec-08 21:41
idrivefastlane28-Dec-08 21:41 
First of all, thanks to dhimant for a wonderfull introduction in form animations.

Please help..

I have used opacity for my forms combinded with animations. When the form shows for the first time, the animation shows 2 layers, one with solid gray background, and the other with the opacity background. At the second show, the animation shows only the opacity layer, as it shoud be.

I tried to hide the form at first show unsuccessfully. Can anybody help me get rid of this solid background layer when using opacity?..

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.