2016-06-28 36 views
0

我试图让我的窗体平滑扩展和折叠动画。我目前的动画真的很紧张并且不一致。下面是一个Gif的Animation。有没有另外一种方法来做到这一点,不冻结窗体?为WinForms平滑扩展和折叠动画

private void ShowHideToggle_CheckStateChanged(object sender, EventArgs e) 
    { 
     if (ShowHideToggle.Checked) //checked = expand form 
     { 
      ShowHideToggle.Text = "<"; 
      while (Width < originalWidth) 
      { 
       Width++; 
       Application.DoEvents(); 
      } 
     } 
     else 
     { 
      ShowHideToggle.Text = ">"; 
      while(Width > 24) 
      { 
       Width--; 
       Application.DoEvents(); 
      } 
     } 
    } 
+1

作为一个补充说明,你应该避免'DoEvents' ...... [这是非常糟糕的( http://stackoverflow.com/questions/5181777/use-of-application-doevents)。 – DonBoitnott

+0

_Smooth ..动画_和_WinForms_不一起走得很好,我很抱歉地说.. – TaW

回答

1

创建一个Timer

Timer t = new Timer(); 
t.Interval = 14; 
t.Tick += delegate 
{ 
    if (ShowHideToggle.Checked) 
    { 
     if (this.Width > 30) // Set Form.MinimumSize to this otherwise the Timer will keep going, so it will permanently try to decrease the size. 
      this.Width -= 10; 
     else 
      t.Stop(); 
    } 
    else 
    { 
     if (this.Width < 300) 
      this.Width += 10; 
     else 
      t.Stop(); 
    } 
}; 

您的代码更改为:

private void ShowHideToggle_CheckStateChanged(object sender, EventArgs e) 
{ 
    t.Start(); 
} 
+0

它有点慢。任何其他想法? – DuckSoy

+1

't.Interval = 2;'这是幻觉; 15-25是更现实的价值.. – TaW

+0

增加this.Width'。我改变了答案中的代码。 – 2016-06-28 11:01:21