C#--winform窗体淡入淡出效果

    主要是利用了Form的Opacity属性和Timer控件。Opacity主要是指窗体的不透明性,其值在100%~0%,设置时可以为double型的值,为0.0时,Form完全透明,为1.0时,Form完全显示。Timer控件主要是用来计时的,有Interval、Enabled属性,Interval用来设置两次计时之间的间隔,Enabled设为true时计时器可用。

1、窗体淡出,代码如下

        private void timer2_Tick(object sender, EventArgs e)
        {
            if (this.Opacity >= 0.025)
            {
                this.Opacity -= 0.025;
            }
            else
            {
                timer2.Stop();
                this.Close();
            }

        }

        //关闭窗体的按钮点击事件
        private void btnClose_Click(object sender, EventArgs e)
        {
            timer2.Start();
        }

2、窗体的淡入

     this.Opacity=0.0//现在Form_Load中将Opacity设为0.0,即完全透明
     private void timer1_Tick(object sender, EventArgs e)
     {
         this.Opacity += 0.01;//每次改变Form的不透明属性
         if (this.Opacity >= 1.0)  //当Form完全显示时,停止计时
         {
             this.timer1.Enabled = false;
         }
     }

3、Opacity属性

     Opacity用于设置窗体的透明度,将Opacity设置为0时,即完全透明;

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章