Timer t1 = new Timer();
private void Form1_Load(object sender, EventArgs e)
{
Opacity = 0; //first the opacity is 0
t1.Interval = 10; //we'll increase the opacity every 10ms
t1.Tick += new EventHandler(fadeIn); //this calls the function that changes opacity
t1.Start();
}
void fadeIn(object sender, EventArgs e)
{
if (Opacity >= 1)
t1.Stop(); //this stops the timer if the form is completely displayed
else
Opacity += 0.05;
}
private void main_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true; //cancel the event so the form won't be closed
t1.Tick += new EventHandler(fadeOut); //this calls the fade out function
t1.Start();
if (Opacity == 0) //if the form is completly transparent
e.Cancel = false; //resume the event - the program can be closed
}
void fadeOut(object sender, EventArgs e)
{
if (Opacity <= 0) //check if opacity is 0
{
t1.Stop(); //if it is, we stop the timer
Close(); //and we try to close the form
}
else
Opacity -= 0.05;
}