Home | API | MFC | C++ | C | Previous | Next

Programming Windows API

Timers

Timers are used to schedule a periodic event and execute some program code.  The SetTimer() API function creates a timer and sets its parameters. Once a timer is created, it will generate timer events until the timer is terminated or the application exits. The prototype of this function is

UINT_PTR SetTimer(HWND hWnd,UINT  ID,UINT uElapse,TIMERPROC lpTimerFunc);

hWnd – a handle to the window to be associated with the timer..
iD – specifies an id value associated with the timer.
uElapse – Sets the time between interrupts in in milliseconds.
lpTimerFunc - If lpTimerFunc is NULL, the system posts a WM_TIMER message to the application queue. If a function is specified the system directly calls a application-defined TIMERPROC callback function for each timer event.

If the function succeeds, the return value is an integer identifying the new timer. If the function fails to create a timer, the return value is zero

The SetTimer function also allows a timer’s parameters to be amended. Both the window handle and the timer identifier must be specified when modifying an existing timer. SetTimer can also change a message-based timer into a callback timer.

To terminate a timer user KillTimer() function. The prototype is

BOOL KillTimer(HWND hWnd,UINT_PTR uIDEvent);

Where HWnd is a handle to the window associated with the specified timer. And UINT_PTR is the identifier of timer to be destroyed.
If the function succeeds, the return value is nonzero.If the function fails, the return value is zero.

Example

The following short demonstrates the timer function by creating a simple digital clock

//Timer Demo
#include <windows.h>
#include <time.h>
#include <stdio.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
#define MY_TIMER 1
int nTimer;
char str[255]=("\0");
VOID CALLBACK MyTimerProc( HWND hWnd, UINT uTimerMsg, UINT uTimerID, DWORD dwTime )
{
// Code for timer MY_TIMER.
time_t t;
struct tm *newtime;
t=time(NULL);
newtime=localtime(&t);
wsprintfA(str, asctime(newtime));
InvalidateRect(hWnd,NULL,0);
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PWSTR pCmdLine, int nCmdShow) {
MSG msg;
WNDCLASS wc = {0};
wc.lpszClassName = TEXT("Center");
wc.hInstance = hInstance;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc;
wc.hCursor = LoadCursor(0, IDC_ARROW);
RegisterClass(&wc);
CreateWindow(wc.lpszClassName, TEXT("Center"),WS_OVERLAPPEDWINDOW | WS_VISIBLE,100, 100, 250, 150, 0, 0, hInstance, 0);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
PAINTSTRUCT paintstruct;
HDC hdc;
switch(msg) {
case WM_CREATE:
{
//sets up timer function
nTimer = SetTimer( hwnd, MY_TIMER, 1000,(TIMERPROC)MyTimerProc );
return 0;
}
case WM_PAINT:
hdc=BeginPaint(hwnd,&paintstruct);
TextOutA(hdc,1,1,str,strlen(str)-1);
EndPaint(hwnd,&paintstruct);
break;
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}

Download


Home | API | MFC | C++ | C | Previous | Next
Creating a Simple Window | Common Elements | Data Types and Character Sets | The Device Context | Graphics Device Interface | Displaying Text | Displaying Graphics | Mapping Modes | Keyboard Input | Working with the Mouse | Menus | Child Windows | ScrollBar Control | The Dialog Box | Windows Message Box | Common Dialog Box | Bitmaps | Common Controls | Creating a Toolbar | Multiple Document Interface | Timers | DLL’s | Creating Custom Controls | Owner Drawn Controls | API Hooking and DLL Injection | File Management Functions | String Manipulation | System Information Functions