窗体的淡入淡出

在许多应用程序中,欢迎界面的启动和退出采用了淡入淡出的技术手段,使得欢迎界面的出现和退出都非常的自然,不会显得很突兀。

本例子使用一个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



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值