-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChronos.cpp
More file actions
251 lines (219 loc) · 5.42 KB
/
Chronos.cpp
File metadata and controls
251 lines (219 loc) · 5.42 KB
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
#include "pch.h"
#include "Chronos.h"
#include "errors.h"
#include "DPIScale.h"
Chronos::Chronos(PCWSTR szClassName) :
BaseWindow(szClassName)
{
}
Chronos::~Chronos()
{
}
LRESULT Chronos::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
return OnCreate();
case WM_COMMAND: // Command items from application menu; accelerators
{
if (HIWORD(wParam) == 0) // Menu
{
int wmId = LOWORD(wParam);
switch (wmId)
{
case IDM_EXIT:
::DestroyWindow(m_hwnd);
return 0;
case IDM_RESET:
ResetTimer();
return 0;
}
}
return DefWindowProc(m_hwnd, uMsg, wParam, lParam);
}
//case WM_CLOSE: {
// if (MessageBox(m_hwnd, L"Really quit?", L"Parthenos", MB_OKCANCEL) == IDOK)
// {
// ::DestroyWindow(m_hwnd);
// }
// return 0;
//}
//case WM_GETMINMAXINFO:
//{
// LPMINMAXINFO lpMMI = reinterpret_cast<LPMINMAXINFO>(lParam);
// lpMMI->ptMinTrackSize.x = 600;
// lpMMI->ptMinTrackSize.y = 600;
// return 0;
//}
case WM_DESTROY:
CoUninitialize();
PostQuitMessage(0);
return 0;
case WM_PAINT:
return OnPaint();
case WM_SIZE:
if (wParam == SIZE_MINIMIZED || wParam == SIZE_MAXIMIZED)
break;
else
return OnSize();
//case WM_MOUSEMOVE:
// return OnMouseMove(
// POINT{ GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) },
// wParam
// );
//case WM_MOUSELEAVE:
//{
// // This message seems to be sent when screen turns off, even when m_bMouseTracking == false.
// // OnMouseMove() begins tracking, so don't call it or else infinite loop.
// D2D1_POINT_2F dipCursor = DPIScale::PixelsToDips(POINT{ -1, -1 });
// if (m_mouseCaptured)
// {
// m_mouseCaptured->OnMouseMove(dipCursor, wParam, false);
// }
// else
// {
// bool handeled = false;
// for (auto item : m_activeItems)
// {
// handeled = item->OnMouseMove(dipCursor, wParam, handeled) || handeled;
// }
// }
// m_mouseTrack.Reset(m_hwnd);
// return 0;
//}
//case WM_MOUSEHOVER:
// // TODO: Handle the mouse-hover message.
// m_mouseTrack.Reset(m_hwnd);
// return 0;
//case WM_MOUSEWHEEL:
// return OnMouseWheel(
// POINT{ GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) },
// wParam
// );
//case WM_SETCURSOR:
// if (LOWORD(lParam) == HTCLIENT)
// {
// // Handled in WM_MOUSEMOVE
// return TRUE;
// }
// break;
case WM_LBUTTONDOWN:
return OnLButtonDown(
DPIScale::PixelsToDips(POINT{ GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) }),
wParam
);
//case WM_LBUTTONDBLCLK:
// return OnLButtonDblclk(
// POINT{ GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) },
// wParam
// );
//case WM_LBUTTONUP:
// return OnLButtonUp(
// POINT{ GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) },
// wParam
// );
//case WM_CHAR:
// return OnChar(static_cast<wchar_t>(wParam), lParam);
//case WM_KEYDOWN:
// if (OnKeyDown(wParam, lParam)) return 0;
// break; // pass on to DefWindowProc to process WM_CHAR messages...?
case WM_TIMER:
return OnTimer(wParam, lParam);
case WM_CONTEXTMENU:
return OnContextMenu(POINT{ GET_X_LPARAM(lParam) , GET_Y_LPARAM(lParam) });
//case WM_APP:
// ProcessCTPMessages();
}
return DefWindowProc(m_hwnd, uMsg, wParam, lParam);
}
LRESULT Chronos::OnCreate()
{
HR(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE));
m_d2.CreateLifetimeResources(m_hwnd);
SetTimer(m_hwnd, // handle to main window
sec1, // timer identifier
1000, // 1-second interval
(TIMERPROC)NULL); // no timer callback
return 0;
}
LRESULT Chronos::OnPaint()
{
m_d2.CreateGraphicsResources(m_hwnd);
PAINTSTRUCT ps;
BeginPaint(m_hwnd, &ps);
RECT rc;
GetClientRect(m_hwnd, &rc);
D2D1_RECT_F clientRect = DPIScale::PixelsToDips(rc);
m_d2.dc()->BeginDraw();
m_d2.dc()->Clear();
//m_d2.brush()->SetColor(D2D1::ColorF(0xEFD3B5, 0.7));
//m_d2.dc()->FillRectangle(clientRect, m_d2.brush());
std::wstring s = time();
m_d2.brush()->SetColor(D2D1::ColorF(0.9f, 0.5f, 1.0f, 1.0f));
m_d2.dc()->DrawText(
s.c_str(),
static_cast<UINT32>(s.size()),
m_d2.textFormat(D2Objects::Segoe18),
clientRect,
m_d2.brush()
);
//float w2 = (clientRect.right - clientRect.left) / 2.0f;
//float h2 = (clientRect.bottom - clientRect.top) / 2.0f;
//m_d2.dc()->FillEllipse({ {w2, h2}, w2, h2 }, m_d2.brush());
HR(m_d2.dc()->EndDraw());
HR(m_d2.swapChain()->Present(1, 0));
EndPaint(m_hwnd, &ps);
return 0;
}
LRESULT Chronos::OnSize()
{
if (m_d2.dc() != NULL)
{
m_d2.DiscardGraphicsResources();
InvalidateRect(m_hwnd, NULL, FALSE);
}
return 0;
}
LRESULT Chronos::OnLButtonDown(D2D1_POINT_2F point, WPARAM wParam)
{
ResetTimer();
return 0;
}
LRESULT Chronos::OnTimer(WPARAM wParam, LPARAM lParam)
{
switch (wParam)
{
case sec1:
if (++m_second >= 60)
{
m_second = 0;
m_minute++;
}
InvalidateRect(m_hwnd, NULL, FALSE);
break;
}
return 0;
}
LRESULT Chronos::OnContextMenu(POINT point)
{
HMENU hPopupMenu = GetSubMenu(LoadMenu(m_hInstance, L"ContextMenu"), 0);;
TrackPopupMenuEx(hPopupMenu, TPM_BOTTOMALIGN | TPM_RIGHTALIGN, point.x, point.y, m_hwnd, NULL);
return 0;
}
std::wstring Chronos::time() const
{
std::wstringstream ss;
ss << std::setfill(L'0') << std::setw(2) << m_minute << L":" << std::setw(2) << m_second;
return ss.str();
}
void Chronos::ResetTimer()
{
SetTimer(m_hwnd, // handle to main window
sec1, // timer identifier
1000, // 1-second interval
(TIMERPROC)NULL); // no timer callback
m_minute = 0;
m_second = 0;
InvalidateRect(m_hwnd, NULL, FALSE);
}