用“Win API”制作计时器,在开发环境运行正确,但编译后运行就出错?
请各位同行指教!
1、下面是窗体模块<frmTimer>Option ExplicitPrivate Sub Form_Load()
Call TimerStart
End SubPrivate Sub Form_Unload(Cancel As Integer)
Call TimerStop
End Sub2、下面是代码模块<modTimer>Option ExplicitPrivate Const TIMERR_NOERROR = 0
Private Const TIME_PERIODIC = 1
Private Const WM_USER = &H400
Private Const TIMER_USER = WM_USER + 1001
Private Const TIMER_ACCURACY = 1
Private Const TIMER_DELAY = 30Private Type TIMECAPS
wPeriodMin As Long
wPeriodMax As Long
End TypePrivate Type MMTIME
wType As Long
u As Long
End TypePrivate Declare Function timeGetTime Lib "winmm.dll" () As Long
Private Declare Function timeBeginPeriod Lib "winmm.dll" (ByVal uPeriod As Long) As Long
Private Declare Function timeEndPeriod Lib "winmm.dll" (ByVal uPeriod As Long) As Long
Private Declare Function timeGetDevCaps Lib "winmm.dll" (lpTimeCaps As TIMECAPS, ByVal uSize As Long) As Long
Private Declare Function timeKillEvent Lib "winmm.dll" (ByVal uID As Long) As Long
Private 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
Private Declare Function timeGetSystemTime Lib "winmm.dll" (lpTime As MMTIME, ByVal uSize As Long) As LongPrivate lngTimerID As Long
Private lngTimerAccuracy As LongPrivate Sub TimeProc(ByVal uID As Long, ByVal uMsg As Long, ByVal dwUser As Long, ByVal dw1 As Long, ByVal dw2 As Long)
Static LastTimer#, nc&
Dim CurrentTimer#, DeltaTimer#
CurrentTimer = Timer
DeltaTimer = CurrentTimer - LastTimer
nc = nc + 1
frmTimer.Print nc, Format(DeltaTimer * 1000, "0")
If nc = 40 Then nc = 0: frmTimer.Cls
LastTimer = CurrentTimer
End SubPublic Sub TimerStart()
Dim tc As TIMECAPS
If timeGetDevCaps(tc, Len(tc)) = TIMERR_NOERROR Then
lngTimerAccuracy = TIMER_ACCURACY
If tc.wPeriodMin > lngTimerAccuracy Then lngTimerAccuracy = tc.wPeriodMin
If tc.wPeriodMax < lngTimerAccuracy Then lngTimerAccuracy = tc.wPeriodMax
timeBeginPeriod lngTimerAccuracy
lngTimerID = timeSetEvent(TIMER_DELAY, lngTimerAccuracy, AddressOf TimeProc, TIMER_USER, TIME_PERIODIC)
End If
End SubPublic Sub TimerStop()
timeKillEvent lngTimerID
timeEndPeriod lngTimerAccuracy
End Sub
请各位同行指教!
1、下面是窗体模块<frmTimer>Option ExplicitPrivate Sub Form_Load()
Call TimerStart
End SubPrivate Sub Form_Unload(Cancel As Integer)
Call TimerStop
End Sub2、下面是代码模块<modTimer>Option ExplicitPrivate Const TIMERR_NOERROR = 0
Private Const TIME_PERIODIC = 1
Private Const WM_USER = &H400
Private Const TIMER_USER = WM_USER + 1001
Private Const TIMER_ACCURACY = 1
Private Const TIMER_DELAY = 30Private Type TIMECAPS
wPeriodMin As Long
wPeriodMax As Long
End TypePrivate Type MMTIME
wType As Long
u As Long
End TypePrivate Declare Function timeGetTime Lib "winmm.dll" () As Long
Private Declare Function timeBeginPeriod Lib "winmm.dll" (ByVal uPeriod As Long) As Long
Private Declare Function timeEndPeriod Lib "winmm.dll" (ByVal uPeriod As Long) As Long
Private Declare Function timeGetDevCaps Lib "winmm.dll" (lpTimeCaps As TIMECAPS, ByVal uSize As Long) As Long
Private Declare Function timeKillEvent Lib "winmm.dll" (ByVal uID As Long) As Long
Private 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
Private Declare Function timeGetSystemTime Lib "winmm.dll" (lpTime As MMTIME, ByVal uSize As Long) As LongPrivate lngTimerID As Long
Private lngTimerAccuracy As LongPrivate Sub TimeProc(ByVal uID As Long, ByVal uMsg As Long, ByVal dwUser As Long, ByVal dw1 As Long, ByVal dw2 As Long)
Static LastTimer#, nc&
Dim CurrentTimer#, DeltaTimer#
CurrentTimer = Timer
DeltaTimer = CurrentTimer - LastTimer
nc = nc + 1
frmTimer.Print nc, Format(DeltaTimer * 1000, "0")
If nc = 40 Then nc = 0: frmTimer.Cls
LastTimer = CurrentTimer
End SubPublic Sub TimerStart()
Dim tc As TIMECAPS
If timeGetDevCaps(tc, Len(tc)) = TIMERR_NOERROR Then
lngTimerAccuracy = TIMER_ACCURACY
If tc.wPeriodMin > lngTimerAccuracy Then lngTimerAccuracy = tc.wPeriodMin
If tc.wPeriodMax < lngTimerAccuracy Then lngTimerAccuracy = tc.wPeriodMax
timeBeginPeriod lngTimerAccuracy
lngTimerID = timeSetEvent(TIMER_DELAY, lngTimerAccuracy, AddressOf TimeProc, TIMER_USER, TIME_PERIODIC)
End If
End SubPublic Sub TimerStop()
timeKillEvent lngTimerID
timeEndPeriod lngTimerAccuracy
End Sub
那句换掉,Print不适合用于API回调函数的输出!