7,763
社区成员
发帖
与我相关
我的任务
分享
If tmpValue > 0 Then '还没搞完?那就进去除'''
tmpValueA = tmpValue Mod 1000
tmpValue = tmpValue - tmpValueA
SS = tmpValue / 1000
If SS = 60 Then '如果等于60秒,当然向分钟进军..
MM = MM + 1 '再来一个MM~~~~
SS = 0
If MM = 60 Then '如果有60个MM了...
HH = HH + 1 '加一个小时..
MM = 0 '没有MM了....T_T
End If
End If
MS = tmpValueA '整了一圈还有剩?那就是毫秒了.
End If
If tmpValue > 1000 Then '还没搞完?那就进去除'''
tmpValueA = tmpValue Mod 1000
tmpValue = tmpValue - tmpValueA
SS = tmpValue / 1000
If SS = 60 Then '如果等于60秒,当然向分钟进军..
MM = MM + 1 '再来一个MM~~~~
SS = 0
If MM = 60 Then '如果有60个MM了...
HH = HH + 1 '加一个小时..
MM = 0 '没有MM了....T_T
End If
End If
tmpValue = tmpValueA '整了一圈还有剩?那就是毫秒了.
End If
MS = tmpValue
'简单的一个计时器,理论上来说是精确到毫秒吧....
'嗷嗷叫的老马
'紫水晶工作室 http://www.m5home.com
'PS:
'没有那个耐心等3600秒....只是手工在文本框里输入了不同的数字简单地验证了一下毫秒转换成HH:MM:SS:MS的算法...
'不知道会不会有BUG...要用的话自己处理:)
Option Explicit
Private Declare Function QueryPerformanceCounter Lib "kernel32" ( _
ByVal lpPerformanceCount As Long) As Long
Private Declare Function QueryPerformanceFrequency Lib "kernel32" ( _
ByVal lpFrequency As Long) As Long
Private Declare Sub Sleep Lib "kernel32.dll" ( _
ByVal dwMilliseconds As Long)
Private msValue As Long '1毫秒所需要的计数值
Private TimerOff As Boolean '定时器过程是否已经退出
Private Sub Command1_Click()
With Command1
If .Tag = "0" Then
.Caption = "停止计时"
.Tag = "1"
Label1.Caption = ""
TimerOff = False
Timer1.Enabled = True
Else
TimerOff = True
.Caption = "开始计时"
.Tag = "0"
End If
End With
End Sub
Private Sub Form_Load()
Dim I As Long, CountValue As Currency '1秒的基准值
Label1.Caption = "00:00:00:000"
Text1.Text = 0
With Command1
.Caption = "开始计时"
.Tag = "0"
End With
Call QueryPerformanceFrequency(VarPtr(CountValue)) '得到1秒计数值
CountValue = CountValue * 10000 '本来应该使用LARGE_INTEGER结构,懒得弄了...直接乘10000换成整数吧...
Debug.Print CountValue
msValue = CountValue / 1000 '得到1毫秒计数值
End Sub
Private Sub Form_Unload(Cancel As Integer)
If TimerOff <> True Then
TimerOff = True
DoEvents
End If
End Sub
Private Sub Text1_Change()
'在这里完成毫秒到HH:MM:SS:MS的换算
Dim HH As Long, MM As Long, SS As Long, MS As Long
Dim tmpValue As Currency, tmpValueA As Currency, tmpValueB As Currency, tmpValueC As Currency, tmpValueD As Currency
tmpValue = CCur(Text1.Text) '目前这里面就是经过的毫秒数
If tmpValue > 3600000 Then '够一小时了?那就进去除~~~
tmpValueA = tmpValue Mod 3600000
tmpValue = tmpValue - tmpValueA '先减余数.....
HH = tmpValue / 3600000 '那这里一定是整数...
tmpValue = tmpValueA '剩下的....
End If
If tmpValue > 60000 Then '够一分钟了?那就进去除....
tmpValueA = tmpValue Mod 60000
tmpValue = tmpValue - tmpValueA '....同上
MM = tmpValue / 60000 '......
If MM = 60 Then '如果等于60分钟,当然是向小时进一位...
HH = HH + 1
MM = 0
End If
tmpValue = tmpValueA '.....
End If
If tmpValue > 0 Then '还没搞完?那就进去除'''
tmpValueA = tmpValue Mod 1000
tmpValue = tmpValue - tmpValueA
SS = tmpValue / 1000
If SS = 60 Then '如果等于60秒,当然向分钟进军..
MM = MM + 1 '再来一个MM~~~~
SS = 0
If MM = 60 Then '如果有60个MM了...
HH = HH + 1 '加一个小时..
MM = 0 '没有MM了....T_T
End If
End If
MS = tmpValueA '整了一圈还有剩?那就是毫秒了.
End If
Label1.Caption = HH & ":" & MM & ":" & SS & ":" & MS
End Sub
Private Sub Timer1_Timer()
Dim tmpTimeA As Currency, tmpTimeB As Currency, tmpTimeC As Currency, tmpTimeD As Currency '单位是ms
Timer1.Enabled = False
Call QueryPerformanceCounter(VarPtr(tmpTimeA))
tmpTimeA = tmpTimeA * 10000 '开始计时的基准值
Do
Call QueryPerformanceCounter(VarPtr(tmpTimeB))
tmpTimeB = tmpTimeB * 10000
tmpTimeC = tmpTimeB - tmpTimeA '以后只需要计算经过多少秒,并换算成HH:MM:SS:MS格式就OK.
If tmpTimeC > tmpTimeD + msValue Then '以1毫秒为单位来更新界面吧...实际还是太快了点.
tmpTimeD = tmpTimeC
Text1.Text = tmpTimeD / msValue '无论怎么拖,怎么整~~~反正是准的.....
Sleep 1 '既然反正都是准的...小睡一会,降降CPU占用率....
DoEvents '处理一下界面堆积的消息
End If
Loop While TimerOff = False
End Sub
Private Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As Any) As Long
Private Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As Any) As Long
sub main()
dim dStart as double, dFinish as double, dFrequency as double, vTimeSpan as variant
QueryPerformanceCounter dStart
...
QueryPerformanceCounter dFinish
QueryPerformanceFrequency dFrequency
vTimeSpan = CDec(dFinish - dStart)/dFrequency
debug.print "耗时 " & formatnumber(vTimeSpan,7) & " 秒"
end sub