- 論壇徽章:
- 0
|
想在程序中加一個(gè)計(jì)時(shí)的功能,記錄從開始游戲時(shí)經(jīng)過的時(shí)間,以秒為單位。翻了下資料,發(fā)現(xiàn)timeGetTime這個(gè)api函數(shù)正和我意。先粘來函數(shù)在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這個(gè)函數(shù),記錄的是從windows開機(jī)到現(xiàn)在經(jīng)歷的時(shí)間,單位為毫秒。函數(shù)返回值為DWORD,即雙字,所以范圍為0-2^32。這個(gè)函數(shù)沒有參數(shù)。聲明的話,在VB中可以用API瀏覽器直接復(fù)制就ok了。
Private Declare Function timeGetTime Lib "winmm.dll" () As Long
使用很簡單,只是要注意的是,不要直接用它的返回值參加計(jì)算。這樣很容易出問題,最好使用兩次函數(shù)調(diào)用返回值的差值參與計(jì)算。在WIN NT中,他默認(rèn)的精度是5毫秒。如果你要使精度更高些,就要用timeBeginPeriod 和timeEndPeriod函數(shù)。如果要求在更高要求的環(huán)境下測度時(shí)間間隔就要用QueryPerformanceCounter 和QueryPerformanceFrequency兩個(gè)函數(shù)了。具體用法參考相關(guān)資料。 |
|