Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

Make a WindowsForm fade in on form load and fade out on form close

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
19 Apr 2011CPOL 8.1K   2   1
using System;using System.Configuration;using System.Collections.Generic;using System.ComponentModel;using System.Text;using System.Windows.Forms;namespace FadingForm{ public partial class FrmSplashScreen : Form { /// /// Indicates whether the...
C#
using System;
using System.Configuration;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
namespace FadingForm
{
    public partial class FrmSplashScreen : Form
    {
        /// <summary>
        /// Indicates whether the form will fade on open and close or not
        /// </summary>
        bool fadeForm;
        /// <summary>
        /// Times the window opacity changes
        /// </summary>
        Timer fadeTimer;
        /// <summary>
        /// How long the fade effect will be (in milliseconds)
        /// </summary>
        int fadeDuration;
        /// <summary>
        /// Number of opacity changes during the fade effect (1 - 100)
        /// </summary>
        int fadeStages;
        /// <summary>
        /// Opening or closing
        /// </summary>
        WindowStatus status;
        public FrmSplashScreen()
        {
            InitializeComponent();
        }
        /// <summary>
        /// Overrides the onload method of the from
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            this.fadeForm = Boolean.Parse(ConfigurationManager.AppSettings["FadeForm"]);
            if (this.fadeForm)
            {
                this.fadeDuration = Int32.Parse(ConfigurationManager.AppSettings["FadeDuration"]);
                this.fadeStages = Int32.Parse(ConfigurationManager.AppSettings["FadeStages"]);
                this.fadeTimer = new Timer();
                this.fadeTimer.Enabled = true;
                this.fadeTimer.Interval = fadeDuration / fadeStages;
                this.fadeTimer.Tick += new EventHandler(fadeTimer_Tick);
                this.status = WindowStatus.Opening;
                this.Opacity = 0;
                this.fadeTimer.Start();
            }
            base.OnLoad(e);
        }
        void fadeTimer_Tick(object sender, EventArgs e)
        {
            switch (this.status)
            {
                case WindowStatus.Opening:
                    if (this.Opacity < 1)
                        this.Opacity += (double)1 / (double)this.fadeStages;
                    else
                        this.fadeTimer.Stop();
                    break;
                case WindowStatus.Closing:
                    if (this.Opacity > 0)
                        this.Opacity -= (double)1 / (double)this.fadeStages;
                    else
                    {
                        this.fadeTimer.Stop();
                        this.fadeForm = false;
                        this.Close();
                    }
                    break;
            }
        }
        /// <summary>
        /// Overrides the OnClosing method of the from
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosing(CancelEventArgs e)
        {
            if (this.fadeForm)
            {
                e.Cancel = true;
                this.status = WindowStatus.Closing;
                this.fadeTimer.Start();
            }
        }
    }
    public enum WindowStatus
    {
        Opening,
        Closing
    }
}

License

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


Written By
Engineer
United Kingdom United Kingdom
I've been involved in object-oriented software development since 2006, when I graduated in Information and TLC Engineering. I've been working for several software companies / departments, mainly on Microsoft and Sun/Oracle technologies. My favourite programming language is C#, next comes Java.
I love design patterns and when I need to resolve a problem, I try to get the best solution, which is often not the quickest one.

"On the best teams, different individuals provide occasional leadership, taking charge in areas where they have particular strengths. No one is the permanent leader, because that person would then cease to be a peer and the team interaction would begin to break down. The structure of a team is a network, not a hierarchy ..."
My favourite team work quotation by DeMarco - Lister in Peopleware

Comments and Discussions

 
GeneralReason for my vote of 5 good example my 5 Pin
charles henington22-Apr-11 5:17
charles henington22-Apr-11 5:17 

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.