您的位置:首页 > 编程语言 > C#

计时精度到底毫秒级别或者更高 如何使用C#制作一个精确计时器

2008-10-20 09:13 756 查看
代码如下:Private Declare Function GetTickCount Lib "kernel32" () As Long Public SStart EventArgs e) { time.IsInc = true; time.IsDec = false; } private void decrease_btn_Click(object sender EventArgs e) { if (time.IsInc) time.incease(); else { time.decrease(); } label1.Text = time.Hour.ToString() + ":" + time.Minute.ToString() + ":" + time.Second.ToString(); } private void increase_btn_Click(object sender EventArgs e) { //this.getList(); time = new TIME(); time.Second = int.Parse(System.DateTime.Now.Second.ToString()); time.Minute = int.Parse(System.DateTime.Now.Minute.ToString()); time.Hour = int.Parse(System.DateTime.Now.Hour.ToString()); } private void timer1_Tick(object sender
写一个TIME类 代码: class TIME { private int hour; private int minute; private int second; private bool isInc; private bool isDec; public int Hour { get { return this.hour; } set { this.hour = value; } } public int Minute { get { return this.minute; } set { this.minute = value; } } public int Second { get { return this.second; } set { this.second = value; } } public bool IsInc { get { return this.isInc; } set { this.isInc = value; } } public bool IsDec { get { return this.isDec; } set { this.isDec = value; } } public void incease() { if (this.Second < 60) this.Second++; else { if (this.minute < 60) this.minute++; else { this.hour++; } } } public void decrease() { if (this.Second > 0) this.Second--; else { if (this.minute > 0) this.minute--; else { this.minute = 59; this.hour--; } } } }主窗体中的代码: TIME time; private void Form1_Load(object sender Stime Private Sub Command1_Click() Command1.Enabled = False: Command2.Enabled = True: Command2.Default = True SStart = GetTickCount Text1.Text = Text1.Text & "你点击[开始]的时间:" & Now & vbCrLf End Sub Private Sub Command2_Click() SEnd = GetTickCount Stime = Val(SEnd - SStart) Text1.Text = Text1.Text & "你点击[停止]的时间:" & Now & vbCrLf & "已经过时间:" & Stime / 1000 & "秒" & Stime Mod 1000 & "毫秒" & vbCrLf End Sub Private Sub Form_Load() Command1.Caption = "开始": Command2.Caption = "停止": Command2.Enabled = False: Text1.Text = "": Command1.Default = True End Sub

|||
ytu77u

SEnd EventArgs e) { time.IsDec = true; time.IsInc = false; }这个实现的是现在以秒为单位的倒计时 你可以根据需要调节TIME的INTERVAL属性来实现你需要的功能

|||
终于写完了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐