The CreateTimerQueueTimer function creates a timer-queue timer. This timer expires at the specified due time, then after every specified period. When the timer expires, the callback function is called.
Функция CreateTimerQueueTimer function создаёт таймер. Создаётся с заданием времени. По истечении времени, вызывается заданная функция.
' This project requires a Form and a Module
' The Form must have two command buttons (Command1
' and Command2) on it.
'
'In a form
Private Declare Function CreateTimerQueue Lib "kernel32.dll" () As Long
Private Declare Function CreateTimerQueueTimer Lib "kernel32.dll" (ByRef phNewTimer As Long, ByVal TimerQueue As Long, ByVal Callback As Long, ByVal Parameter As Long, ByVal DueTime As Long, ByVal Period As Long, ByVal Flags As Long) As Long
Private Declare Function DeleteTimerQueue Lib "kernel32.dll" (ByVal TimerQueue As Long) As Long
Private Declare Function DeleteTimerQueueTimer Lib "kernel32.dll" (ByVal TimerQueue As Long, ByVal Timer As Long, ByVal CompletionEvent As Long) As Long
Private hQueue As Long
Private hTimer As Long
Private Sub Form_Load()
'KPD-Team 2002
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@allapi.net
hQueue = CreateTimerQueue()
Command1.Caption = "Start"
Command2.Caption = "Stop"
End Sub
Private Sub Form_Unload(Cancel As Integer)
DeleteTimerQueue hQueue
End Sub
Private Sub Command1_Click()
If hTimer = 0 Then
CreateTimerQueueTimer hTimer, hQueue, AddressOf TimerCallBack, ByVal 0&, 0, 1000, 0
End If
End Sub
Private Sub Command2_Click()
If hTimer <> 0 Then
DeleteTimerQueueTimer hQueue, hTimer, ByVal 0&
hTimer = 0
End If
End Sub
'In a module
Public Sub TimerCallBack(ByVal lpParameter As Long, ByVal TimerOrWaitFired As Long)
Debug.Print "Timer callback..."
End Sub
'This project requires a Form and a Module
'On the form, there should be one command button (Command1)
'and one Timer (Timer1)
'In the form:
Option Explicit
Private Sub Form_Load()
'KPD-Team 2001
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
Command1.Caption = "Start"
End Sub
Private Sub Form_Unload(Cancel As Integer)
'Stop the timers if they're still counting
timeKillEvent hMMTimer
Timer1.Enabled = False
End Sub
Private Sub Timer1_Timer()
'increment VBTimer
VBTimer = VBTimer + 1
End Sub
Private Sub Command1_Click()
If Command1.Caption = "Start" Then
'Start both timers
Timer1.Interval = 1
Timer1.Enabled = True
hMMTimer = timeSetEvent(1, 0, AddressOf TimerProc, 0, TIME_PERIODIC Or TIME_CALLBACK_FUNCTION)
Command1.Caption = "Stop"
Else
'Stop both timers
timeKillEvent hMMTimer
Timer1.Enabled = False
Command1.Caption = "Start"
'Show result
MsgBox "Timer1_Timer was called " & VBTimer & " times;" & vbNewLine & "TimerProc was called " & MMTimer & " times."
VBTimer = 0
MMTimer = 0
End If
End Sub
'In a module
Option Explicit
Public Const TIME_ONESHOT = 0 'Event occurs once, after uDelay milliseconds.
Public Const TIME_PERIODIC = 1 'Event occurs every uDelay milliseconds.
Public Const TIME_CALLBACK_EVENT_PULSE = &H20 'When the timer expires, Windows calls thePulseEvent function to pulse the event pointed to by the lpTimeProc parameter. The dwUser parameter is ignored.
Public Const TIME_CALLBACK_EVENT_SET = &H10 'When the timer expires, Windows calls theSetEvent function to set the event pointed to by the lpTimeProc parameter. The dwUser parameter is ignored.
Public Const TIME_CALLBACK_FUNCTION = &H0 'When the timer expires, Windows calls the function pointed to by the lpTimeProc parameter. This is the default.
Public Declare Function timeKillEvent Lib "winmm.dll" (ByVal uID As Long) As Long
Public 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
Public VBTimer As Long, MMTimer As Long
Public hMMTimer As Long
Sub TimerProc(ByVal uID As Long, ByVal uMsg As Long, ByVal dwUser As Long, ByVal dw1 As Long, ByVal dw2 As Long)
'Increment MMTimer
MMTimer = MMTimer + 1
End Sub
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 Const TIMER_ID = 1& 'arbitrary timer ID
Public Function SetTmr(hWnd As Long, Interval As Long) As Long
Dim Ret As Long
Ret = SetTimer(hWnd, TIMER_ID, Interval, AddressOf TimerCallback)
End Function
Public Function KillTmr(hWnd As Long) As Long
Dim Ret As Long
Ret = KillTimer(hWnd, TIMER_ID)
KillTmr = Ret
End Function
Private Sub TimerCallback(ByVal hWnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
Debug.Print "hWnd:" & hWnd & vbTab & "uMsg:" & uMsg & vbTab & "idEvent:" & idEvent & vbTab & "dwTime:" & dwTime
End Sub
gaidar писал(а):Это все в класс или в контрол - и вот тебе и новый таймер.
Сейчас этот форум просматривают: The trick и гости: 3