程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> Visual Basic語言 >> VB綜合教程 >> vb實現高精度計時

vb實現高精度計時

編輯:VB綜合教程

  

'在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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved