Date: 02-16-2022
Return to Index
created by gbSnippets
Time - Create a Timer
'Almost all animation uses a Windows timer, which are used to send periodic
'%WM_Timer messages to the Dialog callback at specified intervals. A timer interval
'cannot be changed on the fly. You have to destroy the timer and create a new one.
'Multiple timers can exist at one time.
'Primary Code:
%ID_Timer = 500 'ID for timer
TimerInterval& = 2000 '2000 ms (2 seconds)
SetTimer(hDlg, %ID_Timer, TimerInterval&, 0) 'creates the timer
KillTimer CB.Hndl, %ID_Timer 'kills the timer (also done automatically when program ends)
'This code causes the %WM_Timer to be sent to the dialog every 2 seconds
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
%ID_Timer = 500
Global hDlg as DWord
Function PBMain () As Long
Dialog New Pixels, 0, "Timer",,, 300,100, %WS_OverlappedWindow To hDlg
Dialog Show Modal hDlg, Call DlgProc
End Function
CallBack Function DlgProc() As Long
Select Case CB.Msg
Case %WM_InitDialog
SetTimer(CB.Hndl, %ID_Timer, 500, ByVal %NULL) 'timer given ID=500, 500ms
' Dialog Post CB.Hndl, %WM_Timer, %ID_TIMER, 0 ' optional - forces an initial %WM_TIMER "event"
MsgBox "I go away to 4000", %MB_OK, "Message Box"
Case %WM_Timer
Static iCount as Long
If CB.wParam = %ID_Timer Then 'verify it's the timer of interest
iCount = iCount + 500
Dialog Set Text hDlg, Str$(iCount)
If iCount = 4000 Then SendMessage FindWindow("", "Message Box"), %WM_CLOSE, 0, 0
End If
Case %WM_Destroy
KillTimer CB.Hndl, %ID_Timer
End Select
End Function
'gbs_00125
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm