Click here to Skip to main content
11,720,547 members (67,874 online)
Click here to Skip to main content

C# Form Animation with Windows API

, 10 Jan 2007 CPOL 120.2K 14.5K 65
Rate this:
Please Sign up or sign in to vote.
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:

public class WinAPI
    {
        /// <span class="code-SummaryComment"><summary></span>
        /// Animates the window from left to right. 
        /// This flag can be used with roll or slide animation.
        /// <span class="code-SummaryComment"></summary></span>
        public const int AW_HOR_POSITIVE = 0X1;
        /// <span class="code-SummaryComment"><summary></span>
        /// Animates the window from right to left. 
        /// This flag can be used with roll or slide animation.
        /// <span class="code-SummaryComment"></summary></span>
        public const int AW_HOR_NEGATIVE = 0X2;
        /// <span class="code-SummaryComment"><summary></span>
        /// Animates the window from top to bottom. 
        /// This flag can be used with roll or slide animation.
        /// <span class="code-SummaryComment"></summary></span>
        public const int AW_VER_POSITIVE = 0X4;
        /// <span class="code-SummaryComment"><summary></span>
        /// Animates the window from bottom to top. 
        /// This flag can be used with roll or slide animation.
        /// <span class="code-SummaryComment"></summary></span>
        public const int AW_VER_NEGATIVE = 0X8;
        /// <span class="code-SummaryComment"><summary></span>
        /// Makes the window appear to collapse inward 
        /// if AW_HIDE is used or expand outward if the AW_HIDE is not used.
        /// <span class="code-SummaryComment"></summary></span>
        public const int AW_CENTER = 0X10;
        /// <span class="code-SummaryComment"><summary></span>
        /// Hides the window. By default, the window is shown.
        /// <span class="code-SummaryComment"></summary></span>
        public const int AW_HIDE = 0X10000;
        /// <span class="code-SummaryComment"><summary></span>
        /// Activates the window.
        /// <span class="code-SummaryComment"></summary></span>
        public const int AW_ACTIVATE = 0X20000;
        /// <span class="code-SummaryComment"><summary></span>
        /// Uses slide animation. By default, roll animation is used.
        /// <span class="code-SummaryComment"></summary></span>
        public const int AW_SLIDE = 0X40000; 
        /// <span class="code-SummaryComment"><summary></span>
        /// Uses a fade effect. 
        /// This flag can be used only if hwnd is a top-level window.
        /// <span class="code-SummaryComment"></summary></span>
        public const int AW_BLEND = 0X80000;
        /// <span class="code-SummaryComment"><summary></span>
        /// Animates a window.
        /// <span class="code-SummaryComment"></summary></span>
        [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.

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)

Share

About the Author

No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralThanks!!! Pin
Member 1170028018-May-15 9:11
memberMember 1170028018-May-15 9:11 
Questionexcellent Pin
lankara26-Mar-14 1:53
memberlankara26-Mar-14 1:53 
AnswerRe: excellent Pin
Dhimant Trivedi27-Mar-14 1:36
memberDhimant Trivedi27-Mar-14 1:36 
Questionworking Pin
Member 1061272620-Feb-14 8:16
memberMember 1061272620-Feb-14 8:16 
AnswerRe: working Pin
Member 118006506-Jul-15 20:43
memberMember 118006506-Jul-15 20:43 
Questionnot working in maximize mod Pin
Member 771455918-Jan-14 0:20
memberMember 771455918-Jan-14 0:20 
AnswerRe: not working in maximize mod Pin
Dhimant Trivedi19-Jan-14 19:35
memberDhimant Trivedi19-Jan-14 19:35 
GeneralC# Pin
sajad_zero1-Aug-13 22:06
membersajad_zero1-Aug-13 22:06 
GeneralAwwwwwwsome! Pin
KrNeS5-Jul-13 6:15
memberKrNeS5-Jul-13 6:15 
GeneralRe: Awwwwwwsome! Pin
Dhimant Trivedi5-Jul-13 21:04
memberDhimant Trivedi5-Jul-13 21:04 
GeneralMy vote of 5 Pin
Vladimi-Krylov10-Oct-12 20:01
memberVladimi-Krylov10-Oct-12 20:01 
GeneralRe: My vote of 5 Pin
Dhimant Trivedi11-Oct-12 0:25
memberDhimant Trivedi11-Oct-12 0:25 
GeneralMy vote of 4 Pin
NaveenSoftwares15-Jun-11 12:49
memberNaveenSoftwares15-Jun-11 12:49 
GeneralWindows 7 Support Pin
ScottGBlood13-Oct-10 6:22
memberScottGBlood13-Oct-10 6:22 
AnswerRe: Windows 7 Support Pin
Dhimant Trivedi15-Oct-10 4:24
memberDhimant Trivedi15-Oct-10 4:24 
GeneralHiding Form With Animated Effect Pin
atul paliwal16-Sep-10 21:18
memberatul paliwal16-Sep-10 21:18 
GeneralMy vote of 5 Pin
peejay0220-Jul-10 20:41
memberpeejay0220-Jul-10 20:41 
GeneralGood work and suggestion Pin
Khaniya3-Mar-10 20:01
memberKhaniya3-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
memberlohith.pandit18-Feb-10 20:51 
QuestionDid you know SlideControl? Pin
cheind17-Jan-10 19:12
membercheind17-Jan-10 19:12 
QuestionUsed in Mobile device? Pin
dannykuo30-Jul-09 20:53
memberdannykuo30-Jul-09 20:53 
GeneralHiding form with AW_SLIDE Pin
some1s6915-Jan-09 11:51
membersome1s6915-Jan-09 11:51 
GeneralRe: Hiding form with AW_SLIDE Pin
Dhimant Trivedi18-Jan-09 22:34
memberDhimant Trivedi18-Jan-09 22:34 
QuestionSet hide form as default on init?? Pin
idrivefastlane28-Dec-08 21:41
memberidrivefastlane28-Dec-08 21:41 
AnswerRe: Set hide form as default on init?? [modified] Pin
Dhimant Trivedi15-Jan-09 1:48
memberDhimant Trivedi15-Jan-09 1:48 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150901.1 | Last Updated 10 Jan 2007
Article Copyright 2007 by Dhimant Trivedi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid