2014-07-01 27 views
-3

以下是我創建的代碼,旨在通過播放/停止按鈕控制時間線。我使用工具箱中的計時器來增加時間,因爲我使用framesPerSecond進度時間軸。我覺得計時器似乎有點準確。我想知道是否有更準確地控制時間的建議改進?我是C#的新手,所以請耐心等待編碼。如果它幫助我創建一個更大的項目,這將成爲時間軸的控制,最終將像幻燈片一樣通過圖像進行交換。 感謝flipbook through frames using frames per second定時器

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace timelineControls 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
      timer1.Tick += new EventHandler(timer_Tick); // Every time timer ticks, timer_Tick will be called 
     } 

     private void cbPlayStop_CheckedChanged(object sender, EventArgs e) 
     { 
      Flipbook(); 
     } 

     private void numFps_ValueChanged(object sender, EventArgs e) 
     { 
      Flipbook(); 
     } 

     public void Flipbook() 
     { 
      bool result = cbPlayStop.Checked; 
      if (result) 
      { 
       cbPlayStop.BackColor = System.Drawing.Color.Red; 
       cbPlayStop.Text = "Stop"; 

       // increment the counter at a speed of (X) per second 
       float framerate = (float)1.0/(float)numFps.Value; 

       // Timer will tick every 1 seconds (1000 milliseconds) 
       timer1.Interval = (int)((float)1000 * framerate); 
       timer1.Enabled = true; // pauses/unpauses 
       timer1.Start(); 
      } 
      else 
      { 
       cbPlayStop.BackColor = System.Drawing.Color.Green; 
       cbPlayStop.Text = "Play"; 

       // stop timer from counting 
       timer1.Stop(); 
      } 
     } 

     private int curTime = 0; 

     void timer_Tick(object sender, EventArgs e) 
     { 
      //MessageBox.Show("Tick");     // Alert the user 
      int hours = DateTime.Now.Hour; 
      int minutes = DateTime.Now.Minute; 
      int seconds = DateTime.Now.Second; 
      int milSeconds = DateTime.Now.Millisecond; 

      string timeString = hours + " : " + minutes + " : " + seconds + " : " + milSeconds; 


      bool loopEnabled = cbLoop.Checked; 
      curTime += 1; 
      if (loopEnabled) 
      { 
       if (curTime > pBarTime.Maximum) 
       { 
        curTime = pBarTime.Minimum; 
       } 
      } 
      else 
      { 
       if (curTime > pBarTime.Maximum) 
       { 
        timer1.Stop(); 
        curTime = pBarTime.Maximum; 
       } 
      } 


      pBarTime.Value = curTime; 
      label2.Text = timeString; 
      lbCurTime.Text = curTime.ToString(); 
     } 
    } 
} 
+1

看到相關的「播放/停止」按鈕,'Timer'代碼會比無關的CheckedChanged事件更有幫助。 –

+1

這裏有什麼特別的問題? –

+2

「每秒幀數」是什麼? –

回答

0

每次調用FlipBook,所以是的,它的運行多次,時間要添加新的Tick事件。

此舉聲明構造函數:

public Form1() 
{ 
    InitializeComponent(); 
    timer.Tick += new EventHandler(timer_Tick); 
} 

在問候定時器的功能,請參閱MSDN Timer Class

The Windows Forms Timer component is single-threaded, and is limited to an accuracy of 55 milliseconds. If you require a multithreaded timer with greater accuracy, use the Timer class in the System.Timers namespace.

+0

你提到使用'System.Timers命名空間中的Timer類'我不太確定如何實現它。你知道任何好的教程或例子嗎? – JokerMartini

+0

@JokerMartini請參閱[比較.NET Framework類庫中的計時器類](http://msdn.microsoft.com/zh-cn/magazine/cc164015.aspx)。 – LarsTech

+0

我已經嘗試更新上面的代碼,以便製作更精確的多線程計數器。當我嘗試增加我的curTime變量時,我不斷收到奇怪的錯誤。不知道爲什麼這樣做。想知道你的想法。 – JokerMartini

相關問題