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
{
bool fadeForm;
Timer fadeTimer;
int fadeDuration;
int fadeStages;
WindowStatus status;
public FrmSplashScreen()
{
InitializeComponent();
}
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;
}
}
protected override void OnClosing(CancelEventArgs e)
{
if (this.fadeForm)
{
e.Cancel = true;
this.status = WindowStatus.Closing;
this.fadeTimer.Start();
}
}
}
public enum WindowStatus
{
Opening,
Closing
}
}