Winform窗體淡入淡出效果實現

窗體內定義

/// <summary>
  /// 是否顯示窗體
  /// </summary>
  private bool showfrm =true;
  /// <summary>
  /// 是否關閉窗體
  /// </summary>
  private bool closefrm = false;

timer事件

private void timer1_Tick(object sender, System.EventArgs e)
  {
   if(showfrm)
   {
    if(this.Opacity <1)
    {
     this.Opacity =this.Opacity +0.05;
    }
    else
    {
     showfrm=false;
     this.timer1.Enabled =false;
    }
   }
   if(closefrm)
   {
    if(this.Opacity>0.05)
    {
     this.Opacity=this.Opacity-0.05;
    }
    else
    {
     closefrm=false;
     this.timer1.Enabled=false;
     this.Close(); //關閉窗體
    }
   }
  }

窗體登陸事件

this.Opacity=0;
   showfrm=true;
   timer1.Enabled=true;

重寫窗體關閉事件

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  {
   timer1.Enabled=true;
   closefrm=true;
   if(this.Opacity>0.05)
   {
    e.Cancel=true;
   }
  } 

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