//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);
}