昆明顺城购物中心客服:关于一个秒表的编程,怎么这样不行

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/30 19:47:08
这是一个包括毫秒的秒表,小弟有点问题,以下是我编的程序,于嘛不行啊

dim a as string, b as string, c as string, d as string
dim i as integer

private sub command1_click()
timer1.enabled = true
end sub

private sub form_load()
timer1.enabled = false
end sub

private sub timer1_timer()
i = i + 1
a = format(i mod 1000, "000")
b = format(i \ 1000 mod 60, "00:")
c = format(i \ 60000 mod 60, "00:")
d = format(i \ 3600000, "0:")
text1.text = d & c & b & a
end sub

这样是不准确的,这样根据不同电脑处理器的性能不同,运算速度是不一样的.需要用到一个额外的类,到网上搜一下,好像那个类叫StopWatch,精确到毫秒
代码如下
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "CStopWatch"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
'
' Win32 API declarations.
'
Private Declare Function timeGetTime Lib "winmm.dll" () As Long
Private Declare Function timeGetDevCaps Lib "winmm.dll" (lpTimeCaps As TIMECAPS, ByVal uSize As Long) As Long
'
' API Structure definitions.
'
Private Type TIMECAPS
wPeriodMin As Long
wPeriodMax As Long
End Type
'
' Set aside storage for private member variables.
'
Private m_StartTime As Long
Private m_PeriodMin As Long
Private m_PeriodMax As Long

' ********************************************
' Initialize
' ********************************************
Private Sub Class_Initialize()
'
' Retrieve system timer resolution.
'
Dim tc As TIMECAPS
Call timeGetDevCaps(tc, Len(tc))
m_PeriodMin = tc.wPeriodMin
m_PeriodMax = tc.wPeriodMax
'
' Initialize starting time.
'
m_StartTime = timeGetTime()
End Sub

' ********************************************
' Public Properties
' ********************************************
Public Property Get Elapsed() As Long
'
' Read-Only: return elapsed time in milliseconds
' since stopwatch was reset.
'
Elapsed = timeGetTime() - m_StartTime
End Property

Public Property Get MinimumResolution() As Long
'
' Read-Only: return minimum number of milliseconds
' timer is capable of resolving.
'
MinimumResolution = m_PeriodMin
End Property

Public Property Get MaximumResolution() As Long
'
' Read-Only: return maximum number of milliseconds
' timer is capable of resolving.
'
MaximumResolution = m_PeriodMax
End Property

' ********************************************
' Public Methods
' ********************************************
Public Sub Reset()
'
' Reinitialize starting time.
'
m_StartTime = timeGetTime()
End Sub

timer控件不精确,不可能精确到1毫秒。最精确用汇编,调用系统时钟中断