VB 和timer控件功能相同的函数

来源:百度知道 编辑:UC知道 时间:2024/05/08 08:23:29
如题

标准答案如下:

利用API函数实现定时器功能
模块中:
Option Explicit
Public lTimerId As Long
Private Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Private Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long
Private Sub TimerProc(ByVal lHwnd As Long, ByVal lMsg As Long, ByVal lTimerId As Long, ByVal lTime As Long)
Static i As Long
Form1.Label1.Caption = i
i = i + 1
End Sub
Public Sub StartTimer(lMinute As Long)
lTimerId = SetTimer(0, 0, lMinute, AddressOf TimerProc)
End Sub
Public Function StopTimer(lTimerId As Long) As Long
StopTimer = KillTimer(0, lTimerId)
End Function

窗体中:
Private Sub Form_Load()
StartTimer 1000
End Sub

Private Sub Form_Unload(Cancel As Integer)
StopTimer lTimerId
End Sub

api 函数 Declare Sub S