- 论坛徽章:
- 0
|
想在程序中加一个计时的功能,记录从开始游戏时经过的时间,以秒为单位。翻了下资料,发现timeGetTime这个api函数正和我意。先粘来函数在MSDN上的介绍。
timeGetTime
The timeGetTime function retrieves the system time, in milliseconds. The system time is the time elapsed since Windows was started.
DWORD timeGetTime(VOID); Parameters
This function does not take parameters.
Return Values
Returns the system time, in milliseconds.
Remarks
The only difference between this function and the timeGetSystemTime function is that timeGetSystemTime uses the MMTIME structure to return the system time. The timeGetTime function has less overhead than timeGetSystemTime.
Note that the value returned by the timeGetTime function is a DWORD value. The return value wraps around to 0 every 2^32 milliseconds, which is about 49.71 days.This can cause problems in code that directly uses the timeGetTime return value in computations, particularly where the value is used to control code execution. You should always use the difference between two timeGetTime return values in computations.
Windows NT: The default precision of the timeGetTime function can be five milliseconds or more, depending on the machine. You can use the timeBeginPeriod and timeEndPeriod functions to increase the precision of timeGetTime. If you do so, the minimum difference between successive values returned by timeGetTime can be as large as the minimum period value set using timeBeginPeriod and timeEndPeriod. Use the QueryPerformanceCounter and QueryPerformanceFrequency functions to measure short time intervals at a high resolution,
Windows 95: The default precision of the timeGetTime function is 1 millisecond. In other words, the timeGetTime function can return successive values that differ by just 1 millisecond. This is true no matter what calls have been made to the timeBeginPeriod and timeEndPeriod functions.
不是很难,但看完也够麻烦的了。大致意思就是说,timeGetTime这个函数,记录的是从windows开机到现在经历的时间,单位为毫秒。函数返回值为DWORD,即双字,所以范围为0-2^32。这个函数没有参数。声明的话,在VB中可以用API浏览器直接复制就ok了。
Private Declare Function timeGetTime Lib "winmm.dll" () As Long
使用很简单,只是要注意的是,不要直接用它的返回值参加计算。这样很容易出问题,最好使用两次函数调用返回值的差值参与计算。在WIN NT中,他默认的精度是5毫秒。如果你要使精度更高些,就要用timeBeginPeriod 和timeEndPeriod函数。如果要求在更高要求的环境下测度时间间隔就要用QueryPerformanceCounter 和QueryPerformanceFrequency两个函数了。具体用法参考相关资料。 |
|