Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Tagged as

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

, 19 Apr 2011
Rate this:
Please Sign up or sign in to vote.
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...
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)

Share

About the Author

Erion Pici
Software Developer (Senior)
Italy Italy
I've been involved in object-oriented software development since 2006, when I graduated in Information and TLC Engineering at the Università degli Studi di Perugia, in Italy. I've been working for several software companies / departments, mainly on Microsoft and Sun 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 Pinmembercharles henington22-Apr-11 5:17 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 19 Apr 2011
Article Copyright 2011 by Erion Pici
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid