高进度计时,精确到0.001秒,且不受任何干扰! (转)

本文介绍了一种基于Visual Basic的高精度计时器实现方法,通过调用系统API实现毫秒级计时,并提供了完整的代码示例及运行效果说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

高进度计时,精确到0.001秒,且不受任何干扰! (转)[@more@]

 

'在Project中加入一个Module,然后在其中加入以下代码:
Option Explicit

Type LARGE_INTEGER
  lowpart As Long
  highpart As Long
End Type

Public Declare Function QueryPerformanceCounter Lib "kernel32" _
  (lpPerformanceCount As LARGE_INTEGER) As Long
Public Declare Function QueryPerformanceFrequency Lib "kernel32" _
  (lpFrequency As LARGE_INTEGER) As Long
Public Declare Function timeSetEvent Lib "winmm.dll" (ByVal uDelay As Long, ByVal _
  uResolution As Long, ByVal lpFunction As Long, ByVal dwUser As Long, _
  ByVal uFlags As Long) As Long
Public Declare Function timeKillEvent Lib "winmm.dll" (ByVal uID As Long) As Long
Public Declare Function GetTickCount Lib "kernel32" () As Long

Public lMsfreq As Long
Public TimerCount As Single
Public lmmCount As Single
Public lTimeID As Long
Public actTime1 As Long
Public actTime2 As Long
Public iCountStart As Single

Dim iCount As Single

'注释: timeSetEvent的回调函数
Sub TimeProc(ByVal uID As Long, ByVal uMsg As Long, ByVal dwUser As Long, _
  ByVal dw1 As Long, ByVal dw2 As Long)
 
  Form1.Text2.Text = Format$(lmmCount, "00.00")
  lmmCount = lmmCount - 0.01
  If lmmCount <= 0 Then
  iCountStart = 60
  lmmCount = 60
  TimerCount = 60
  EndCount
  End If
End Sub
Sub EndCount()
  iCount = iCountStart
  iCountStart = 0
  timeKillEvent lTimeID
  actTime2 = GetTickCount - actTime1
  With Form1
  .Command1.Enabled = True
  .Command2.Enabled = False
  .Timer1.Enabled = False
 
  .Text1 = "计数器记时" + Format$((60 - iCount), "00.00") + "  " _
  + "实际经过时间" + Format$((actTime2 / 1000), "00.00")
  .Text2 = "计数器记时" + Format$((60 - lmmCount), "00.00") + "  " _
  + "实际经过时间" + Format$((actTime2 / 1000), "00.00")
  .Text3 = "计数器记时" + Format$((60 - TimerCount), "00.00") + "  " _
  + "实际经过时间" + Format$((actTime2 / 1000), "00.00")
  End With
End Sub


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752043/viewspace-990105/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10752043/viewspace-990105/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值