Timer Demo

#include <windows.h> #include <time.h> #include <string.h> #include <wchar.h> #define MY_TIMER 1 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); WCHAR str[128] = L"Starting timer..."; int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) { MSG msg; WNDCLASSW wc; wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wc.lpszMenuName = NULL; wc.lpszClassName = L"TimerWindow"; RegisterClassW(&wc); CreateWindowW( L"TimerWindow", L"Timer Demo", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 300, 150, NULL, NULL, hInstance, NULL); 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 ps; HDC hdc; switch(msg) { case WM_CREATE: SetTimer(hwnd, MY_TIMER, 1000, NULL); break; case WM_TIMER: if(wParam == MY_TIMER) { time_t t; struct tm *newtime; time(&t); newtime = localtime(&t); wcsftime( str, 128, L"%A %d %B %Y %H:%M:%S", newtime); InvalidateRect(hwnd, NULL, TRUE); } break; case WM_PAINT: hdc = BeginPaint(hwnd, &ps); TextOutW( hdc, 20, 20, str, wcslen(str)); EndPaint(hwnd, &ps); break; case WM_DESTROY: KillTimer(hwnd, MY_TIMER); PostQuitMessage(0); break; default: return DefWindowProc(hwnd, msg, wParam, lParam); } return 0; }