Click here to Skip to main content
15,499,289 members
Articles / Programming Languages / C#
Posted 19 Oct 2008

Tagged as


72 bookmarked

C# Fade Form Effect With the AnimateWindow API Function

Rate me:
Please Sign up or sign in to vote.
4.78/5 (35 votes)
19 Oct 2008CPOL1 min read
An easy way to perform fade/slide effects for forms.

The Motivation

I required a fade/slide effect to replace a previous thread based approach that was using deprecated methods.

When consulting the oracle (Google), I was able to find more than a few articles that use opacity or utility type implementation of the AnimateWindow function, but I was looking for a solution to hide the functionality and keep things as simple as possible, so I created FadeForm.

The Objective

Write a class that implements/wraps the AnimateWindow function which can easily be added to existing code.

The Code

The FadePage Object

Create a new class that derives from System.Windows.Forms.Form which will allow us to extend the behavior of a standard form with the AnimateWindow method.

public abstract class FadeForm : Form

Next, create a member variable that will determine if we fade or slide, and a few constructors to assign the variable, with the default being the slide effect.

public abstract class FadeForm : Form
    private bool _UseSlideAnimation;
    public FadeForm() : this(false) { }
    public FadeForm(bool useSlideAnimation)
        _UseSlideAnimation = useSlideAnimation;

The AnimateWindow Implementation

This is really the guts of the class that will control the effects. The logic is based on a previous article, and modified to hide the actual implementation.

Declare the integers that define the effects and the functions internally to the fade page class.

const int AW_HIDE = 0X10000;
const int AW_ACTIVATE = 0X20000;
const int AW_HOR_POSITIVE = 0X1;
const int AW_HOR_NEGATIVE = 0X2;
const int AW_SLIDE = 0X40000;
const int AW_BLEND = 0X80000;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int AnimateWindow
(IntPtr hwand, int dwTime, int dwFlags);

Finally, we override the load and close methods of the Form class to perform the automation.

protected override void OnLoad(EventArgs e)
    AnimateWindow(this.Handle, 1000, AW_ACTIVATE | (_UseSlideAnimation ? 
                  AW_HOR_POSITIVE | AW_SLIDE : AW_BLEND));
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
    if (e.Cancel == false)
        AnimateWindow(this.Handle, 1000, AW_HIDE | (_UseSlideAnimation ? 
                      AW_HOR_NEGATIVE | AW_SLIDE : AW_BLEND));

Using the Class

Consuming the class is very simple, and requires a single line of code change to existing forms, by choosing to derive from FadeForm instead of the standard Form.

public partial class TestForm : Form

Changes to…

public partial class TestForm : FadeForm


public partial class TestForm : FadeForm
    public TestForm() : base(true|false) {}



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer
United States United States
Ah, just another dev living the life of Dilbert. Smile | :)

Comments and Discussions

QuestionDid you know SlideControl? Pin
cheind17-Jan-10 20:13
Membercheind17-Jan-10 20:13 
QuestionFlashing... Pin
Pete Mattison6-Feb-09 12:30
MemberPete Mattison6-Feb-09 12:30 
GeneralTrying to use your code on other handles (open windows) Pin
Mister031-Dec-08 6:38
MemberMister031-Dec-08 6:38 
GeneralIt won't open in designer Pin
kstr29-Oct-08 11:07
Memberkstr29-Oct-08 11:07 
GeneralRe: It won't open in designer Pin
MCF.Goodwin29-Oct-08 16:43
MemberMCF.Goodwin29-Oct-08 16:43 
General5 Pin
#realJSOP22-Oct-08 4:30
mva#realJSOP22-Oct-08 4:30 
QuestionDisappearing textbox Pin
Member 197265521-Oct-08 5:09
MemberMember 197265521-Oct-08 5:09 
AnswerRe: Disappearing textbox Pin
MCF.Goodwin21-Oct-08 5:38
MemberMCF.Goodwin21-Oct-08 5:38 
GeneralRe: Disappearing textbox Pin
MCF.Goodwin21-Oct-08 14:34
MemberMCF.Goodwin21-Oct-08 14:34 
Questionhow To in manage code Pin
Bnaya Eshet21-Oct-08 0:54
MemberBnaya Eshet21-Oct-08 0:54 
AnswerRe: how To in manage code Pin
MCF.Goodwin21-Oct-08 1:45
MemberMCF.Goodwin21-Oct-08 1:45 
GeneralMore AnimateWindow Options Pin
AnandChavali20-Oct-08 1:24
MemberAnandChavali20-Oct-08 1:24 

Here are some other AnimateWindow options.

public const int AW_VER_POSITIVE = 0x00000004;
public const int AW_VER_NEGATIVE = 0x00000008;
public const int AW_CENTER = 0x00000010;

AW_CENTER produces a real nice effect. Pls see AnimateWindow in MSDN[^] for explanation.

Thanks and Regards,

GeneralIt doesn't as a MDI child Forms Pin
kenshiro200019-Oct-08 23:29
Memberkenshiro200019-Oct-08 23:29 
QuestionRe: It doesn't as a MDI child Forms Pin
MCF.Goodwin20-Oct-08 0:48
MemberMCF.Goodwin20-Oct-08 0:48 
GeneralRe: It doesn't as a MDI child Forms Pin
richardw4820-Oct-08 23:50
Memberrichardw4820-Oct-08 23:50 
GeneralReally Nice Pin
Manas_Patnaik19-Oct-08 22:11
MemberManas_Patnaik19-Oct-08 22:11 
Generalgreat!! Pin
yassir hannoun19-Oct-08 12:19
Memberyassir hannoun19-Oct-08 12:19 

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

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