A window receives the WM_TIMER message when the time-out period of a timer (created by the SetTimer function) elapses. You should only create a handler for this message if you did not specify a TimerProc callback function for the timer. Otherwise, allow the window's default message handler to invoke the TimerProc function.
The WM_TIMER message should always return 0.
None.
Const WM_TIMER = &H113
Display the current time in text box control Text1. The time is updated twice every second, and the time is formatted according to the current locale's settings. To use this example, place a text edit box named Text1 on a form window.
' This code is licensed according to the terms and conditions listed here.
' Declarations and such needed for the example:
' (Copy them to the (declarations) section of a module.)
Public Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
Public Declare Function SetTimer Lib "user32.dll" (ByVal hWnd As Long, ByVal nIDEvent _
As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Public Declare Function KillTimer Lib "user32.dll" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long
Public Const WM_TIMER = &H113
Public Declare Function GetTimeFormat Lib "kernel32.dll" Alias "GetTimeFormatA" (ByVal _
Locale As Long, ByVal dwFlags As Long, lpTime As SYSTEMTIME, ByVal lpFormat As Any, _
ByVal lpTimeStr As String, ByVal cchTime As Long) As Long
Public Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal hWnd _
As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Const GWL_WNDPROC = -4
Public Declare Function CallWindowProc Lib "user32.dll" Alias "CallWindowProcA" (ByVal _
lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long
' *** Place the following code inside a module. ***
' A pointer to Form1's default window procedure:
Pubic pOldProc As Long
' The following function will handle all of Form1's messages. It only
' actually processes WM_TIMER and passes all others to the default handler.
Public Function WindowProc (ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, _
ByVal lParam As Long)
Dim systime As SYSTEMTIME ' the current time
Dim timestr As String * 260 ' receives the formatted string
Dim slength As Long ' length of formatted string returned
Select Case uMsg
Case WM_TIMER
' Retrieve the current time, according to the computer's time zone.
GetLocalTime systime
' Format a string to represent the time.
slength = GetTimeFormat(0, 0, systime, CLng(0), timestr, Len(timestr))
' Display the string in Text1, found on window Form1.
Form1.Text1.Text = Left(timestr, slength)
WindowProc = 0
Case Else
' Pass the message to Form1's previous procedure.
WindowProc = CallWindowProc(pOldProc, hWnd, uMsg, wParam, lParam)
End Select
End Function
' *** Place the following code inside Form1. ***
' Create the timer when the form opens and destroy it when the form closes.
' The timer is given an ID of 1, so the return values don't need to be saved.
' Also, set up the window procedure when the form is loaded, and remove it
' when the form is unloaded. This procedure will receive all of Form1's messages.
Private Sub Form1_Load()
Dim retval As Long ' return value
pOldProc = SetWindowLong(Form1.hWnd, GWL_WNDPROC, AddressOf WindowProc)
retval = SetTimer(Form1.hWnd, 1, 500, AddressOf TimerProc)
End Sub
Private Sub Form1_Unload(Cancel As Integer)
Dim retval As Long ' return value
retval = KillTimer(Form1.hWnd, 1)
retval = SetWindowLong(Form1.hWnd, GWL_WNDPROC, pOldProc)
End Sub
Back to the Message list.
Back to the Reference section.
Last Modified: December 17, 2000
This page is copyright © 2000 Paul Kuliniewicz.
Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/w/wm_timer.html