Skip to main content

Animate Windows Form

In this tutorial, I will briefly explain how to add animation effects to a windows form in C#.

In order to animate a window in C#, we will need to call a native method using PInvoke.

 

Create a new Windows Forms Project

AnimateWindow Function

According to the MSDN Library, "the animate window function enables you to produce special effects when showing or hiding windows". There are only four types of animation that you can use. They are: roll, slide, collapse/expand, and alpha-blended fade.

In order to call the AnimateWindow function, as already stated, we need to use PInvoke. This is the PInvoke signature for the AnimateWindow function (the using System.Runtime.InteropServices; declaration is required)

 

Add this to the top of the "Form1" class.

[DllImport("user32.dll")]
static extern bool AnimateWindow(IntPtr hWnd, int time, AnimateWindowFlags flags);

Notice the "AnimateWindowFlags" parameter. This is a user-defined type. So, in order for this code to compile, you must add this to enum to your code

[Flags]
enum AnimateWindowFlags
{
AW_HOR_POSITIVE = 0x00000001,
AW_HOR_NEGATIVE = 0x00000002,
AW_VER_POSITIVE = 0x00000004,
AW_VER_NEGATIVE = 0x00000008,
AW_CENTER = 0x00000010,
AW_HIDE = 0x00010000,
AW_ACTIVATE = 0x00020000,
AW_SLIDE = 0x00040000,
AW_BLEND = 0x00080000
}

These are the possible animation types that you can use. Below is a brief overview of what each flag does.

AW_SLIDE :
Uses slide animation. By default, roll animation is used. This flag is ignored when used with AW_CENTER.

AW_ACTIVATE :
Activates the window. Do not use this value with AW_HIDE.

AW_BLEND :
Uses a fade effect. This flag can be used only if hwnd is a top-level window.

AW_HIDE :
Hides the window. By default, the window is shown.

AW_CENTER :
Makes the window appear to collapse inward if AW_HIDE is used or expand outward if the AW_HIDE is not used. The various direction flags have no effect.

AW_HOR_POSITIVE :
Animates the window from left to right. This flag can be used with roll or slide animation. It is ignored when used with AW_CENTER or AW_BLEND.

AW_HOR_NEGATIVE :
Animates the window from right to left. This flag can be used with roll or slide animation. It is ignored when used with AW_CENTER or AW_BLEND.

AW_VER_POSITIVE :
Animates the window from top to bottom. This flag can be used with roll or slide animation. It is ignored when used with AW_CENTER or AW_BLEND.

AW_VER_NEGATIVE :
Animates the window from bottom to top. This flag can be used with roll or slide animation. It is ignored when used with AW_CENTER or AW_BLEND.

 

Animating a Window

Now that we have the AnimateWindow function setup, let's animate a window.

Inside of the form's load event handler, add this code

AnimateWindow(this.Handle, 500, AnimateWindowFlags.AW_BLEND);

Inside of the form's close event handler, add this code

AnimateWindow(this.Handle, 1000, AnimateWindowFlags.AW_BLEND | AnimateWindowFlags.AW_HIDE);

Build and run your code. When your application starts, you should see the form fade in. Go ahead and mess around with the "time" parameter and the "AnimateWindowFlags" arameter.

Remember that the higher the value you set the time, the slower the animation will take place.

 

Happy Coding……

 

Comments

Anonymous said…
gud one ...
Anonymous said…
Nice one.....