如何使用C#制作一个精确计时器,计时精度到底毫秒级别或者更高
QUESTION:设计一个1分钟的计时秒表,用两个单项按钮来控制秒表是顺序或逆序计时,精确到毫秒。界面自行设计。...
QUESTION:设计一个1分钟的计时秒表,用两个单项按钮来控制秒表是顺序或逆序计时,精确到毫秒。界面自行设计。
展开
5个回答
推荐于2018-04-06
展开全部
终于写完了,代码如下:
Private Declare Function GetTickCount Lib "kernel32" () As Long
Public SStart, SEnd, 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
Private Declare Function GetTickCount Lib "kernel32" () As Long
Public SStart, SEnd, 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
AiPPT
2024-09-19 广告
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图...
点击进入详情页
本回答由AiPPT提供
展开全部
c#本来就有timer控件,直接就可以用,精度可以调的!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-04-19
展开全部
ytu77u
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-04-19
展开全部
写一个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, 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, 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)
{
time.IsInc = true;
time.IsDec = false;
}
private void decrease_btn_Click(object sender, EventArgs e)
{
time.IsDec = true;
time.IsInc = false;
}
这个实现的是现在以秒为单位的倒计时 你可以根据需要调节TIME的INTERVAL属性来实现你需要的功能
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, 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, 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)
{
time.IsInc = true;
time.IsDec = false;
}
private void decrease_btn_Click(object sender, EventArgs e)
{
time.IsDec = true;
time.IsInc = false;
}
这个实现的是现在以秒为单位的倒计时 你可以根据需要调节TIME的INTERVAL属性来实现你需要的功能
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
要用c#调用dll的api,可以百度有类似的文章。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询