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時,即完全透明;

 

 

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