Click here to Skip to main content

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

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
    }
}

Web04 | 2.8.160208.1 | Advertise | Privacy
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service