實作窗體淡入淡出的完整代碼
private bool isclose=false;
private bool canclose=false;
private void timer1_Tick(object sender, System.EventArgs e)
{
if(this.Opacity<1)
{
if(this.isclose)
{
this.timer1.Enabled=false;
this.timer2.Enabled=true;
}
this.Opacity+=0.1;
}
else
{
this.timer1.Enabled=false;
}
}
private void timer2_Tick(object sender, System.EventArgs e)
{
if(this.Opacity>0)
{this.Opacity-=0.1;}
else
{
this.timer2.Enabled=false;
this.canclose=true;
this.Close();
}
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if(this.timer1.Enabled)
{
e.Cancel=true;
this.isclose=true;
}
else
{
if(!this.canclose)
{
e.Cancel=true;
this.timer2.Enabled=true;
}
}
}
private void Form1_Load(object sender, System.EventArgs e)
{
this.Show();
this.timer1.Enabled=true;
}