VB6.0調用SetTimer實現定時器

Timer.bas:

Option Explicit

Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long

Public Sub TimerProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
MsgBox Now()
End Sub
窗體代碼:
Option Explicit

Dim lngTimerID As Long
Dim BlnTimer As Boolean

Private Sub Form_Load()
BlnTimer
= False
Command1.Caption
= "定時開始"
End Sub

Private Sub Form_Unload(Cancel As Integer)
KillTimer
0, lngTimerID
End Sub

Private Sub Command1_Click()
If BlnTimer = False Then
'每5秒鐘調用一次函數
lngTimerID = SetTimer(0, 0, 5000, AddressOf TimerProc)
BlnTimer
= True
Command1.Caption
= "定時結束"
Else
KillTimer
0, lngTimerID
BlnTimer
= False
Command1.Caption
= "定時開始"
End If
End Sub
說明:
TimerProc函數定義一定要放在bas模塊文件中,否則運行代碼"lngTimerID = SetTimer(0, 0, 5000, AddressOf TimerProc)"會報錯,
提示:操作符 AddressOf 使用無效。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章