VB6 返回当前毫秒
时间: 2023-08-15 09:21:18 浏览: 133
可以以下VB6代码实现返回当前毫秒数的功能:
```
Private Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As Currency) As Long
Private Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As Currency) As Long
Public Function GetCurrentMilliseconds() As Long
Dim freq As Currency
Dim count As Currency
Dim delta As Currency
Dim time As Double
' 获取时钟频率
QueryPerformanceFrequency freq
' 获取当前计数器值
QueryPerformanceCounter count
' 计算经过的计数器增量
delta = count - lastCount
' 计算经过的时间,单位为毫秒
time = (delta / freq) * 1000
' 更新上一次计数器值
lastCount = count
' 返回当前毫秒数
GetCurrentMilliseconds = CLng(time)
End Function
```
在函数内部,首先使用QueryPerformanceFrequency函数获取时钟频率,然后使用QueryPerformanceCounter函数获取当前计数器值。接着计算从上一次调用函数到现在经过了多少计数器增量值,然后将其除以时钟频率并乘以1000,得到经过的时间,单位为毫秒。最后将时间转换为Long类型并返回。需要注意的是,在函数外部需要定义一个lastCount变量,用于记录上一次调用函数时的计数器值。
阅读全文