在许多应用程序中,欢迎界面的启动和退出采用了淡入淡出的技术手段,使得欢迎界面的出现和退出都非常的自然,不会显得很突兀。
本例子使用一个Timer组件,Timer组件可以对以时间控制的事件进行响应。Timer组件中的Enabled属性用于控制该组件是否有效,Start方法用于开启该Timer组件的计时,而Interval属性则用于控制Timer组件的时间单位。可以把Timer组件想象成一个秒表以便于理解。
namespace Fade
{
public partial class Fade : Form
{
//设置用于指示淡入淡出变化方向的变量
private bool isFade = true;
public Fade()
{
InitializeComponent();
}
private void Fade_Load(object sender, EventArgs e)
{
//使欢迎界面的大小和背景图片的大小相同
this.ClientSize = this.BackgroundImage.Size;
this.Opacity = 0; //使欢迎界面全透明
this.timer.Interval = 50; //设置timer的时间间隔
this.timer.Enabled = true; //使计时器timer开始运作
this.timer.Start();
}
private void timer_Tick(object sender, EventArgs e)
{
if (isFade)
{
this.Opacity += 0.02; //由不透明变为透明
if (this.Opacity >= 1)
{
isFade = false;
}
}
else
{
this.Opacity -= 0.02; //有透明变为不透明
if (this.Opacity <= 0) //当完全透明时停止计时器,并退出欢迎界面
{
this.timer.Stop();
this.Close();
}
}
}
}
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// timer
//
this.timer.Tick += new System.EventHandler(this.timer_Tick);
//
// Fade
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackgroundImage = global::Fade.Properties.Resources.Tulips;
this.ClientSize = new System.Drawing.Size(284, 262);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "Fade";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form1";
this.Load += new System.EventHandler(this.Fade_Load);
this.ResumeLayout(false);
}
#endregion