1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
#define WIN32_LEAN_AND_MEAN
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <tchar.h>
#include <sstream>
const TCHAR szClassName [] = _T("Timer_Demo_Window_Class");
const TCHAR szWindowName[] = _T("Timer Demo");
const TCHAR szWindowName_Running[] = _T("Timer Demo (running)");
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int CALLBACK
_tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR /*lpCmdLine*/, int nCmdShow)
{
WNDCLASSEX wcex = {0};
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wcex.lpfnWndProc = WndProc;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszClassName = szClassName;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
ATOM atom = RegisterClassEx(&wcex);
if(0 == atom)
return 0;
HWND hWnd = CreateWindowEx( 0,
szClassName,
szWindowName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL );
if(!hWnd)
return 0;;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
MSG msg = {0};
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static const TCHAR szFont[] = _T("Arial");
static const int nFontSize = 48;
static const UINT uTimerPeriond = 1000; // i.e. 1 sec (1000 msec)
static HFONT s_hFont = NULL;
static int s_nCount = 0;
static UINT s_uTimerID = 0;
switch (uMsg)
{
case WM_CREATE:
{
HDC hDC = GetDC(hWnd);
LOGFONT logFont = {0};
logFont.lfHeight = -MulDiv(nFontSize, GetDeviceCaps(hDC, LOGPIXELSY), 72);
logFont.lfWeight = FW_BOLD;
_tcsncpy(logFont.lfFaceName, szFont, LF_FACESIZE);
s_hFont = CreateFontIndirect(&logFont);
ReleaseDC(hWnd, hDC);
}
break;
case WM_DESTROY:
{
if(0 != s_uTimerID)
{
KillTimer(hWnd, s_uTimerID);
s_uTimerID = 0;
}
if(s_hFont != NULL)
{
DeleteObject(s_hFont);
s_hFont = NULL;
}
PostQuitMessage(0);
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps = {0};
HDC hDC = BeginPaint(hWnd, &ps);
RECT rcClient = {0};
GetClientRect(hWnd, &rcClient);
HFONT hFontOld = (HFONT)SelectObject(hDC, s_hFont);
#ifdef _UNICODE
std::wostringstream oss;
#else
std::ostringstream oss;
#endif
oss << s_nCount;
DrawText(hDC, oss.str().c_str(), oss.str().length(), &rcClient,
DT_CENTER | DT_VCENTER | DT_SINGLELINE);
SelectObject(hDC, hFontOld);
EndPaint(hWnd, &ps);
}
break;
case WM_LBUTTONDBLCLK:
{
if(0 == s_uTimerID)
{
SetWindowText(hWnd, szWindowName_Running);
s_nCount = 0;
InvalidateRect(hWnd, NULL, TRUE);
s_uTimerID = SetTimer(hWnd, 1, uTimerPeriond, NULL);
}
else
{
SetWindowText(hWnd, szWindowName);
KillTimer(hWnd, s_uTimerID);
s_uTimerID = 0;
}
}
break;
case WM_TIMER:
{
++s_nCount;
InvalidateRect(hWnd, NULL, TRUE);
}
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}
|