2017-07-15 90 views
0

我在Visual Studio中爲我的表單創建了一個轉換, 我爲寬度編寫了一個幻燈片效果,但它最終以緩慢的速度運行 有沒有辦法讓它更快? BTW這裏是代碼:表單幻燈片轉換C#

`int check = 0; 
private void button1_Click(object sender, EventArgs e) 
    { 
     this.button1.Text = "Hide"; 

     if (check == 0) 
     { 

      for (int i = 350; i <= 824; ++i) 
      { 
       this.Size = new Size(i, 507); 
       Thread.Sleep(10); 
       this.CenterToScreen(); 


      } 
      check = 1; 


     } 

     else if (check == 1) 
     { 
      this.button1.Text = "Key"; 

      for (int i = 824; i >= 351; i--) 
      { 
       this.Size = new Size(i, 507); 
       Thread.Sleep(5); 
       this.CenterToScreen(); 


      } 


      check = 0; 

     } 
    } 
+0

使用內置動畫。 https://stackoverflow.com/questions/33114198/slide-in-slide-out-animation –

+1

@SvSv他使用winforms而不是wpf – jonathana

+0

如果你的顯示器有60赫茲的刷新率,那麼改變速度比16.667毫秒更快反正顯示。使用更大的間隔和更大尺寸的計時器'for(int i = 824; i> = 351; i + = 10)' –

回答

1

通過使用現有的代碼,你可以調整它的速度等目的;

private int check = 0; 
private void button1_Click(object sender, EventArgs e) 
{ 
    this.button1.Text = "Hide"; 

    if (check == 0) 
    { 
     for (int i = 350; i <= 824; i += 2) 
     { 
      this.Size = new Size(i, 507); 
      Thread.Sleep(1); 
      this.CenterToScreen(); 
     } 
     check = 1; 


    } 

    else if (check == 1) 
    { 
     this.button1.Text = "Key"; 

     for (int i = 824; i >= 351; i -= 2) 
     { 
      this.Size = new Size(i, 507); 
      Thread.Sleep(1); 
      this.CenterToScreen(); 
     } 
     check = 0; 
    } 
} 

您可以像上面那樣更改Thread.Sleep行並增加或減少循環變量以加快動畫速度。