-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimerWnd.cpp
502 lines (400 loc) · 12.4 KB
/
TimerWnd.cpp
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#include "pch.h"
#include "resource.h"
#include "TimerWnd.h"
#define TIMERWND_CLASS_NAME _T("TIMERWND")
#define TIME_TEXT_SIZE 10
#define TIMER_ID 1
typedef struct tagTIMERMENUITEM {
UINT wID;
UINT fType;
UINT fState;
WCHAR szText[MAX_LOADSTRING];
RECT rect;
} TIMERMENUITEM, *LPTIMERMENUITEM;
typedef struct tagTIMERMENU
{
int nCount;
LPTIMERMENUITEM lpItems;
HFONT hFont;
HFONT hBoldFont;
LPTIMERMENUITEM lpSelectedItem;
} TIMERMENU, *LPTIMERMENU;
extern WCHAR szStartTimerText[MAX_LOADSTRING];
extern WCHAR szResumeTimerText[MAX_LOADSTRING];
extern WCHAR szPauseTimerText[MAX_LOADSTRING];
extern HWND hAPCWindow;
extern BOOL bTimerIsRun;
extern ULONGLONG uStartTime;
extern ULONGLONG uTotalTime;
HWND hTimerWnd = nullptr;
TIMERMENU timerMenu;
BOOL bIsTracking = false;
LRESULT CALLBACK TimerWndProc(HWND, UINT, WPARAM, LPARAM);
void DrawTimerMenu(HDC, RECT);
void DrawMenuItem(HDC, LPTIMERMENUITEM, HFONT);
LPTIMERMENUITEM GetTimerMenuItemFromPoint(POINT);
LPTIMERMENUITEM GetTimerMenuItemFromID(UINT);
void TimeToString(ULONGLONG, LPWSTR);
void SetTimerMenuItemText(LPTIMERMENUITEM, LPWSTR, BOOL);
ATOM RegisterTimerWndClass(HINSTANCE hInstance) {
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEXW);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = TimerWndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = nullptr;
wcex.hIconSm = nullptr;
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_MENU + 1);
wcex.lpszMenuName = nullptr;
wcex.lpszClassName = TIMERWND_CLASS_NAME;
return RegisterClassExW(&wcex);
}
LRESULT CALLBACK TimerWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) {
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
DrawTimerMenu(hdc, ps.rcPaint);
EndPaint(hWnd, &ps);
}
break;
case WM_MOUSEMOVE:
{
if (!bIsTracking) {
bIsTracking = true;
TRACKMOUSEEVENT tme;
tme.cbSize = sizeof(tme);
tme.dwFlags = TME_LEAVE;
tme.hwndTrack = hTimerWnd;
TrackMouseEvent(&tme);
}
POINT pt;
GetCursorPos(&pt);
ScreenToClient(hTimerWnd, &pt);
LPTIMERMENUITEM lpSelectedItem = GetTimerMenuItemFromPoint(pt);
if (lpSelectedItem != timerMenu.lpSelectedItem) {
if (timerMenu.lpSelectedItem != nullptr) {
timerMenu.lpSelectedItem->fState &= ~ODS_SELECTED;
InvalidateRect(hWnd, &timerMenu.lpSelectedItem->rect, false);
}
if (lpSelectedItem != nullptr) {
lpSelectedItem->fState |= ODS_SELECTED;
InvalidateRect(hWnd, &lpSelectedItem->rect, false);
}
timerMenu.lpSelectedItem = lpSelectedItem;
}
}
break;
case WM_LBUTTONUP:
if (timerMenu.lpSelectedItem != nullptr) {
if (hAPCWindow != nullptr && !(timerMenu.lpSelectedItem->fState & ODS_DISABLED))
SendMessageW(hAPCWindow, WM_COMMAND, timerMenu.lpSelectedItem->wID, 0);
}
break;
case WM_MOUSELEAVE:
bIsTracking = false;
if (timerMenu.lpSelectedItem != nullptr) {
timerMenu.lpSelectedItem->fState &= ~ODS_SELECTED;
InvalidateRect(hWnd, &timerMenu.lpSelectedItem->rect, false);
timerMenu.lpSelectedItem = nullptr;
}
break;
case WM_KILLFOCUS:
HideTimerWnd();
break;
case WM_TIMER:
SetTimerText(true);
break;
case WM_DESTROY:
{
if (bTimerIsRun)
StopTimerEvent();
delete[]timerMenu.lpItems;
DeleteObject(timerMenu.hFont);
DeleteObject(timerMenu.hBoldFont);
}
break;
default:
return DefWindowProcW(hWnd, message, wParam, lParam);
}
return 0L;
}
void LoadTimerMenu(HINSTANCE hInstance) {
HMENU hMenu = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDM_MAIN));
HMENU hSubMenu = GetSubMenu(hMenu, 0);
NONCLIENTMETRICSW ncm;
memset(&ncm, 0, sizeof(ncm));
ncm.cbSize = sizeof(ncm);
SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, 0, (PVOID)& ncm, 0);
timerMenu.nCount = GetMenuItemCount(hSubMenu);
timerMenu.lpItems = new TIMERMENUITEM[timerMenu.nCount];
timerMenu.lpSelectedItem = nullptr;
timerMenu.hFont = CreateFontIndirectW(&ncm.lfMenuFont);
ncm.lfMenuFont.lfWeight = 800;
timerMenu.hBoldFont = CreateFontIndirectW(&ncm.lfMenuFont);
LPTIMERMENUITEM lpMenuItem = timerMenu.lpItems;
for (int itemNum = 0; itemNum < timerMenu.nCount; itemNum++, lpMenuItem++) {
MENUITEMINFO mii;
mii.cbSize = sizeof(MENUITEMINFO);
mii.fMask = MIIM_ID | MIIM_FTYPE | MIIM_STATE | MIIM_STRING;
mii.dwTypeData = lpMenuItem->szText;
mii.cch = MAX_LOADSTRING;
GetMenuItemInfoW(hSubMenu, itemNum, TRUE, &mii);
lpMenuItem->wID = mii.wID;
lpMenuItem->fType = mii.fType;
lpMenuItem->fState = mii.fState;
}
SetTimerText(false);
SetStartStopTimerText(false);
SetAvailabilityResetMenuItem(false);
DestroyMenu(hMenu);
}
void TimeToString(ULONGLONG uTime, LPWSTR lpszTimeString) {
ULONGLONG hours = uTime / 3600000;
uTime %= 3600000;
ULONGLONG mins = uTime / 60000;
uTime %= 60000;
ULONGLONG secs = uTime / 1000;
swprintf_s(lpszTimeString, TIME_TEXT_SIZE, _T("%02llu:%02llu:%02llu"), hours, mins, secs);
}
SIZE MeasureMenuItem(LPTIMERMENUITEM lpMenuItem, HFONT hFont) {
SIZE itemSize;
if (lpMenuItem->fType & MFT_SEPARATOR) {
itemSize.cx = 100;
itemSize.cy = 10;
return itemSize;
}
LPWSTR lpszText = lpMenuItem->szText;
if (lpszText[0] != _T('\0')) {
HDC hdc = GetDC(hTimerWnd);
HFONT hOldFont = (HFONT)SelectObject(hdc, hFont);
DWORD dwSize = GetTabbedTextExtentW(hdc, lpszText, (int)wcslen(lpszText), 0, nullptr);
itemSize.cx = LOWORD(dwSize) + 17;
itemSize.cy = 21;
if (HIWORD(dwSize) > itemSize.cy)
itemSize.cy = HIWORD(dwSize);
SelectObject(hdc, hOldFont);
ReleaseDC(hTimerWnd, hdc);
}
else {
itemSize.cx = 100;
itemSize.cy = 21;
}
return itemSize;
}
SIZE MeasureTimerMenu() {
int left = 3;
int y = 3;
int width = 0;
LPTIMERMENUITEM lpMenuItem = timerMenu.lpItems;
for (int i = 0; i < timerMenu.nCount; i++, lpMenuItem++) {
SIZE szItemSize = MeasureMenuItem(lpMenuItem, timerMenu.hFont);
lpMenuItem->rect.left = left;
lpMenuItem->rect.top = y;
lpMenuItem->rect.bottom = lpMenuItem->rect.top + szItemSize.cy - 1;
y += szItemSize.cy;
if (width < szItemSize.cx)
width = szItemSize.cx;
}
// Set same width for all menu items.
lpMenuItem = timerMenu.lpItems;
for (int i = 0; i < timerMenu.nCount; i++, lpMenuItem++) {
lpMenuItem->rect.right = lpMenuItem->rect.left + width - 1;
}
SIZE szMenuSize;
szMenuSize.cx = left + width + 2;
szMenuSize.cy = y + 2;
return szMenuSize;
}
void SetupTimerWnd(HINSTANCE hInstance) {
LoadTimerMenu(hInstance);
SIZE menuSize = MeasureTimerMenu();
POINT pos;
GetCursorPos(&pos);
int left = pos.x;
int top = pos.y - menuSize.cy;
SetWindowPos(hTimerWnd, HWND_TOP, left, top, menuSize.cx, menuSize.cy, 0);
}
BOOL ShowTimerWnd(HINSTANCE hInstance) {
if (hTimerWnd == nullptr) {
hTimerWnd = CreateWindowExW(WS_EX_TOOLWINDOW | WS_EX_TOPMOST, TIMERWND_CLASS_NAME, nullptr, WS_POPUP,
0, 0, 0, 0, nullptr, nullptr, hInstance, nullptr);
if (!hTimerWnd) {
return FALSE;
}
SetupTimerWnd(hInstance);
}
ShowWindow(hTimerWnd, SW_SHOW);
UpdateWindow(hTimerWnd);
SetForegroundWindow(hTimerWnd);
if (bTimerIsRun)
StartTimerEvent();
return TRUE;
}
void HideTimerWnd() {
if (hTimerWnd == nullptr)
return;
DestroyWindow(hTimerWnd);
hTimerWnd = nullptr;
}
void StartTimerEvent() {
SetTimer(hTimerWnd, TIMER_ID, 1000, nullptr);
}
void StopTimerEvent() {
KillTimer(hTimerWnd, TIMER_ID);
}
void FillSolidRect(HDC hdc, LPCRECT lpRect, COLORREF clr) {
SetBkColor(hdc, clr);
ExtTextOutW(hdc, 0, 0, ETO_OPAQUE, lpRect, nullptr, 0, nullptr);
}
void DrawHorizontalLine(HDC hdc, int startX, int endX, int y, COLORREF color) {
HPEN hPen = CreatePen(PS_SOLID, 1, color);
HPEN hOldPen = (HPEN)SelectObject(hdc, hPen);
MoveToEx(hdc, startX, y, nullptr);
LineTo(hdc, endX, y);
SelectObject(hdc, hOldPen);
DeleteObject(hPen);
}
void DrawRect(HDC hdc, RECT rect, COLORREF color) {
HPEN hPen = CreatePen(PS_SOLID, 1, color);
HPEN hOldPen = (HPEN)SelectObject(hdc, hPen);
MoveToEx(hdc, rect.left, rect.top, nullptr);
LineTo(hdc, rect.right, rect.top);
LineTo(hdc, rect.right, rect.bottom);
LineTo(hdc, rect.left, rect.bottom);
LineTo(hdc, rect.left, rect.top);
SelectObject(hdc, hOldPen);
DeleteObject(hPen);
}
void DrawMenuItem(HDC hdc, LPTIMERMENUITEM lpMenuItem, HFONT hFont) {
RECT rect = lpMenuItem->rect;
RECT rcImage = rect;
RECT rcText = rect;
HFONT hOldFont = (HFONT)SelectObject(hdc, hFont);
rcImage.right = rcImage.left + rect.bottom - rect.top;
rcText.left += rcImage.right + 1;
SetBkMode(hdc, TRANSPARENT);
if (lpMenuItem->fState & ODS_SELECTED) {
FillSolidRect(hdc, &rect, GetSysColor(COLOR_MENUHILIGHT));
SetTextColor(hdc, GetSysColor(COLOR_HIGHLIGHTTEXT));
}
else {
FillSolidRect(hdc, &rect, GetSysColor(COLOR_MENU));
SetTextColor(hdc, GetSysColor(COLOR_MENUTEXT));
}
{
SIZE sz;
GetTextExtentPoint32W(hdc, lpMenuItem->szText, (int)wcslen(lpMenuItem->szText), &sz);
int ay1 = (rcText.bottom - rcText.top - sz.cy) / 2;
rcText.top += ay1;
rcText.left += 2;
rcText.right -= 15;
}
if (lpMenuItem->fState & ODS_DISABLED) {
if (!(lpMenuItem->fState & ODS_SELECTED)) {
RECT rcText1 = rcText;
InflateRect(&rcText1, -1, -1);
SetTextColor(hdc, GetSysColor(COLOR_3DHILIGHT));
DrawTextW(hdc, lpMenuItem->szText, (int)wcslen(lpMenuItem->szText), &rcText1,
DT_VCENTER | DT_LEFT | DT_EXPANDTABS);
SetTextColor(hdc, GetSysColor(COLOR_GRAYTEXT));
DrawTextW(hdc, lpMenuItem->szText, (int)wcslen(lpMenuItem->szText), &rcText,
DT_VCENTER | DT_LEFT | DT_EXPANDTABS);
}
else {
SetTextColor(hdc, GetSysColor(COLOR_MENU));
DrawTextW(hdc, lpMenuItem->szText, (int)wcslen(lpMenuItem->szText), &rcText,
DT_VCENTER | DT_LEFT | DT_EXPANDTABS);
}
}
else {
DrawTextW(hdc, lpMenuItem->szText, (int)wcslen(lpMenuItem->szText), &rcText,
DT_VCENTER | DT_LEFT | DT_EXPANDTABS);
}
SelectObject(hdc, hOldFont);
}
void DrawMenuSeparator(HDC hdc, RECT rect) {
int y = rect.top + 3;
DrawHorizontalLine(hdc, rect.left + 3, rect.right - 3, y, GetSysColor(COLOR_3DSHADOW));
DrawHorizontalLine(hdc, rect.left + 3, rect.right - 3, y + 1, GetSysColor(COLOR_3DHILIGHT));
}
void DrawTimerMenu(HDC hdc, RECT rcPaint) {
LPTIMERMENUITEM lpMenuItem = timerMenu.lpItems;
for (int i = 0; i < timerMenu.nCount; i++, lpMenuItem++) {
RECT rect;
if (!IntersectRect(&rect, &lpMenuItem->rect, &rcPaint))
continue;
if (lpMenuItem->fType & MFT_SEPARATOR)
DrawMenuSeparator(hdc, lpMenuItem->rect);
else
DrawMenuItem(hdc, lpMenuItem, lpMenuItem->wID == ID_TIME ?
timerMenu.hBoldFont : timerMenu.hFont);
}
RECT rect;
GetWindowRect(hTimerWnd, &rect);
rect.right = rect.right - rect.left - 1;
rect.bottom = rect.bottom - rect.top - 1;
rect.left = 0;
rect.top = 0;
DrawRect(hdc, rect, GetSysColor(COLOR_3DSHADOW));
}
LPTIMERMENUITEM GetTimerMenuItemFromPoint(POINT point) {
LPTIMERMENUITEM lpMenuItem = timerMenu.lpItems;
for (int i = 0; i < timerMenu.nCount; i++, lpMenuItem++) {
if (PtInRect(&lpMenuItem->rect, point)) {
return lpMenuItem->wID == ID_TIME || lpMenuItem->fState & ODS_DISABLED ?
nullptr : lpMenuItem;
}
}
return nullptr;
}
LPTIMERMENUITEM GetTimerMenuItemFromID(UINT wID) {
LPTIMERMENUITEM lpMenuItem = timerMenu.lpItems;
for (int i = 0; i < timerMenu.nCount; i++, lpMenuItem++) {
if (lpMenuItem->wID == wID)
return lpMenuItem;
}
return nullptr;
}
void SetTimerMenuItemText(LPTIMERMENUITEM lpMenuItem, LPWSTR lpszText, BOOL bInvalidate) {
wcscpy_s(lpMenuItem->szText, MAX_LOADSTRING, lpszText);
if (bInvalidate)
InvalidateRect(hTimerWnd, &lpMenuItem->rect, false);
}
void SetStartStopTimerText(BOOL bInvalidate) {
LPWSTR lpszStartStopTimerText;
if (bTimerIsRun) {
lpszStartStopTimerText = szPauseTimerText;
}
else {
lpszStartStopTimerText = uTotalTime > 0 ?
szResumeTimerText : szStartTimerText;
}
LPTIMERMENUITEM lpMenuItem = GetTimerMenuItemFromID(ID_START_PAUSE);
SetTimerMenuItemText(lpMenuItem, lpszStartStopTimerText, bInvalidate);
}
void SetTimerText(BOOL bValidate) {
WCHAR szTimeText[TIME_TEXT_SIZE] = _T("00:00:00");
if (bTimerIsRun) {
ULONGLONG uCurrentTime = uTotalTime + GetTickCount64() - uStartTime;
TimeToString(uCurrentTime, szTimeText);
}
else if (uTotalTime > 0) {
TimeToString(uTotalTime, szTimeText);
}
LPTIMERMENUITEM lpMenuItem = GetTimerMenuItemFromID(ID_TIME);
SetTimerMenuItemText(lpMenuItem, szTimeText, bValidate);
}
void SetAvailabilityResetMenuItem(BOOL bInvalidate) {
LPTIMERMENUITEM lpMenuItem = GetTimerMenuItemFromID(ID_RESET);
if (bTimerIsRun || uTotalTime > 0)
lpMenuItem->fState &= ~ODS_DISABLED;
else
lpMenuItem->fState |= ODS_DISABLED;
if (bInvalidate)
InvalidateRect(hTimerWnd, &lpMenuItem->rect, false);
}