以下是我創建的代碼,旨在通過播放/停止按鈕控制時間線。我使用工具箱中的計時器來增加時間,因爲我使用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();
}
}
}
看到相關的「播放/停止」按鈕,'Timer'代碼會比無關的CheckedChanged事件更有幫助。 –
這裏有什麼特別的問題? –
「每秒幀數」是什麼? –