take a look:
<br />
#include <stdio.h><br />
#include <windows.h> <br />
#pragma comment(lib, "user32.lib")<br />
<br />
HINSTANCE hInst; <br />
TCHAR szTitle[] = "timer sample"; <br />
TCHAR szWindowClass[] = "timersampleapp"; <br />
int nCountDown = 0;<br />
<br />
<br />
BOOL InitInstance(HINSTANCE, int); <br />
void ErrorExit(LPTSTR lpszFunction); <br />
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); <br />
<br />
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) <br />
{ <br />
UNREFERENCED_PARAMETER(hPrevInstance); <br />
UNREFERENCED_PARAMETER(lpCmdLine); <br />
<br />
MSG msg; <br />
WNDCLASSEX wcex; <br />
<br />
wcex.cbSize = sizeof(WNDCLASSEX); <br />
wcex.style = CS_HREDRAW | CS_VREDRAW; <br />
wcex.lpfnWndProc = WndProc; <br />
wcex.cbClsExtra = 0; <br />
wcex.cbWndExtra = 0; <br />
wcex.hInstance = hInstance; <br />
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION); <br />
wcex.hCursor = LoadCursor(NULL, IDC_ARROW); <br />
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); <br />
wcex.lpszMenuName = NULL; <br />
wcex.lpszClassName = szWindowClass; <br />
wcex.hIconSm = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(5), IMAGE_ICON, <br />
GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR); <br />
<br />
if (RegisterClassEx(&wcex) == 0) <br />
{ <br />
ErrorExit("RegisterClassEx"); <br />
return FALSE; <br />
} <br />
<br />
<br />
if (!InitInstance (hInstance, nCmdShow)) <br />
{ <br />
ErrorExit("InitInstance"); <br />
return FALSE; <br />
} <br />
<br />
while (GetMessage(&msg, NULL, 0, 0)) <br />
{ <br />
TranslateMessage(&msg); <br />
DispatchMessage(&msg); <br />
} <br />
<br />
return (int) msg.wParam; <br />
} <br />
<br />
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) <br />
{ <br />
HWND hWnd; <br />
hInst = hInstance; <br />
<br />
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, <br />
CW_USEDEFAULT, 0, 640, 480, NULL, NULL, hInstance, NULL); <br />
<br />
if (!hWnd) <br />
{ <br />
return FALSE; <br />
} <br />
<br />
nCountDown = 60;<br />
SetTimer(hWnd, 1, 1000, (TIMERPROC) NULL);<br />
<br />
ShowWindow(hWnd, nCmdShow); <br />
UpdateWindow(hWnd); <br />
<br />
return TRUE; <br />
} <br />
<br />
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) <br />
{ <br />
PAINTSTRUCT ps; <br />
HDC hdc; <br />
<br />
switch (message) <br />
{ <br />
case WM_TIMER:<br />
<br />
--nCountDown;<br />
if(nCountDown == 0) <br />
KillTimer(hWnd, 1);<br />
else<br />
{<br />
TCHAR msg[20] = {0};<br />
sprintf(msg, "%d", nCountDown);<br />
SetWindowText(hWnd, msg);<br />
}<br />
<br />
break;<br />
case WM_PAINT: <br />
hdc = BeginPaint(hWnd, &ps); <br />
EndPaint(hWnd, &ps); <br />
break; <br />
case WM_DESTROY: <br />
PostQuitMessage(0); <br />
break; <br />
default: <br />
return DefWindowProc(hWnd, message, wParam, lParam); <br />
} <br />
return 0; <br />
} <br />
<br />
void ErrorExit(LPTSTR lpszFunction) <br />
{ <br />
TCHAR szBuf[80]; <br />
LPVOID lpMsgBuf; <br />
DWORD dw = GetLastError(); <br />
<br />
FormatMessage( <br />
FORMAT_MESSAGE_ALLOCATE_BUFFER | <br />
FORMAT_MESSAGE_FROM_SYSTEM, <br />
NULL, <br />
dw, <br />
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), <br />
(LPTSTR) &lpMsgBuf, <br />
0, NULL ); <br />
<br />
wsprintf(szBuf, <br />
"%s failed with error %d: %s", <br />
lpszFunction, dw, lpMsgBuf); <br />
<br />
MessageBox(NULL, szBuf, "Error", MB_OK); <br />
LocalFree(lpMsgBuf); <br />
} <br />
|