為ACCESS添加多個Timer功能

字號:

眾所周知,ACCESS只有一個Timer事件,并不能處理多個觸發(fā)事件,感覺十分不爽。
    現(xiàn)在我們可以借助API輕松實現(xiàn)多個定時器,而且調(diào)用也比較方便,下面是個簡單的例子
    '模塊代碼:
    '===============================================================
    '功能: 添加多個計時器
    '用法: 設(shè)置計時器 SetTimer Me.hwnd, 1, 10000, AddressOf TimerProc1
    ' 關(guān)閉計時器 KillTimer Me.hwnd, 1
    '作者: andymark
    ' QQ : 42503577 ewang11@163.com
    '
    '=================================================================
    Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
    '創(chuàng)建一個計時器
    '參數(shù): hwnd 窗口句柄
    ' nIDEvent 定時器ID,多個定時器時,可以通過該ID判斷是哪個定時器
    ' uElapse 時間間隔,單位為毫秒
    ' lpTimerFunc 回調(diào)函數(shù)
    Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
    '關(guān)閉銷毀計時器
    'Timer回調(diào)涵數(shù)
    Public Sub TimerProc1(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)
    MsgBox "測試第1個Timer事件"
    End Sub
    Public Sub TimerProc2(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)
    MsgBox "測試第2個Timer事件"
    End Sub
    Public Sub TimerProc3(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)
    MsgBox "測試第3個Timer事件"
    End Sub
    '窗體代碼
    Private Sub Form_Load()
    '設(shè)置10秒間隔,調(diào)用回調(diào)涵數(shù)TimerProc1
    SetTimer Me.hwnd, 1, 10000, AddressOf TimerProc1
    '設(shè)置4秒間隔,調(diào)用回調(diào)涵數(shù)TimerProc2
    SetTimer Me.hwnd, 2, 4000, AddressOf TimerProc2
    '設(shè)置14秒間隔,調(diào)用回調(diào)涵數(shù)TimerProc3
    SetTimer Me.hwnd, 3, 14000, AddressOf TimerProc3
    End Sub
    Private Sub Form_Unload(Cancel As Integer)
    '關(guān)閉所有計時器
    KillTimer Me.hwnd, 1
    KillTimer Me.hwnd, 2
    KillTimer Me.hwnd, 3
    End Sub