Sunday, March 7, 2010

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):

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



Add this to the top of the “Form1” class.



Notice the “AnimateWindowFlags” parameter. This is a user-defined type. So, in order for this code to compile, you must add this 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.

First, we need to create a load event handler for our form.

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

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

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” parameter. Remember that the higher the value you set the time, the slower the animation will take place.



Download Demo Application
Download Src Code VS2008

That’s it!
Enjoy !

2 comments:

Neha Patel said...

Hello! the application is fine but i m new in c# programming...so, it's like all is new for me..by the way, it's very useful me in future...

Anonymous said...

I have a question... when I run the application there is no effect at all... My form appears suddenly without any animation at all. I can't figure out the problem. I have even downloaded your source but it's the same problem. Could you help me please?

Post a Comment