Click here to Skip to main content
Click here to Skip to main content
Go to top

C# Form Animation with Windows API

, 10 Jan 2007
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

Comments and Discussions

 
Questionexcellent Pinmemberlankara26-Mar-14 1:53 
AnswerRe: excellent PinmemberDhimant Trivedi27-Mar-14 1:36 
Questionworking PinmemberMember 1061272620-Feb-14 8:16 
Questionnot working in maximize mod PinmemberMember 771455918-Jan-14 0:20 
AnswerRe: not working in maximize mod PinmemberDhimant Trivedi19-Jan-14 19:35 
GeneralC# Pinmembersajad_zero1-Aug-13 22:06 
GeneralAwwwwwwsome! PinmemberKrNeS5-Jul-13 6:15 
GeneralRe: Awwwwwwsome! PinmemberDhimant Trivedi5-Jul-13 21:04 
GeneralMy vote of 5 PinmemberVladimi-Krylov10-Oct-12 20:01 
GeneralRe: My vote of 5 PinmemberDhimant Trivedi11-Oct-12 0:25 
GeneralMy vote of 4 PinmemberNaveenSoftwares15-Jun-11 12:49 
GeneralWindows 7 Support PinmemberScottGBlood13-Oct-10 6:22 
AnswerRe: Windows 7 Support PinmemberDhimant Trivedi15-Oct-10 4:24 
GeneralHiding Form With Animated Effect Pinmemberatul paliwal16-Sep-10 21:18 
GeneralMy vote of 5 Pinmemberpeejay0220-Jul-10 20:41 
GeneralGood work and suggestion PinmemberKhaniya3-Mar-10 20:01 
QuestionHow can you make this to a Usercontrol added to a panel instead of form Pinmemberlohith.pandit18-Feb-10 20:51 
QuestionDid you know SlideControl? Pinmembercheind17-Jan-10 19:12 
QuestionUsed in Mobile device? Pinmemberdannykuo30-Jul-09 20:53 
GeneralHiding form with AW_SLIDE Pinmembersome1s6915-Jan-09 11:51 
GeneralRe: Hiding form with AW_SLIDE PinmemberDhimant Trivedi18-Jan-09 22:34 
QuestionSet hide form as default on init?? Pinmemberidrivefastlane28-Dec-08 21:41 
AnswerRe: Set hide form as default on init?? [modified] PinmemberDhimant Trivedi15-Jan-09 1:48 
QuestionAre there any Win API's effects except these effects? PinmemberMohammad Dayyan24-Dec-08 22:42 
AnswerRe: Are there any Win API's effects except these effects? PinmemberDhimant Trivedi15-Jan-09 1:23 

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 | Mobile
Web04 | 2.8.140916.1 | Last Updated 10 Jan 2007
Article Copyright 2007 by Dhimant Trivedi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid