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):
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:
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.
Uses slide animation. By default, roll animation is used. This flag is ignored when used with AW_CENTER.
Activates the window. Do not use this value with AW_HIDE.
Uses a fade effect. This flag can be used only if hwnd is a top-level window.
Hides the window. By default, the window is shown.
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.
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.
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.
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.
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 Src Code VS2008