1,488
社区成员
发帖
与我相关
我的任务
分享'利用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 Private Sub Command1_Click() '执行结果是Timer1停止,API继续运行
MsgBox "查看Label1"
End Sub
Private Sub Command2_Click() '执行结果是Timer1与API都卡了,直到文件复制完成
FileCopy "D:\新建文件夹 (2)\Movie\11.rmvb", "G:\11.rmvb" '11.rmvb是一个500M的文件
End Sub
Private Sub Form_Load()
StartTimer 10
End Sub
Private Sub Form_Unload(Cancel As Integer)
StopTimer lTimerId
End Sub
Private Sub Timer1_Timer()
Static i As Long
i = i + 1
Label2.Caption = i
End Sub