-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathasteroids.cpp
More file actions
278 lines (231 loc) · 6.96 KB
/
asteroids.cpp
File metadata and controls
278 lines (231 loc) · 6.96 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
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
/*
COPYRIGHT (C) 2026 ETHAN CHAN
ALL RIGHTS RESERVED. UNAUTHORIZED COPYING, MODIFICATION, DISTRIBUTION, OR USE
OF THIS SOFTWARE WITHOUT PRIOR PERMISSION IS STRICTLY PROHIBITED.
THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM,
OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
PROJECT NAME: DieKnow
FILENAME: src/asteroids.cpp
DESCRIPTION: Asteroids hider for DieKnow
AUTHOR: Ethan Chan
DATE: 2026-11-27
VERSION: 2.0.1
*/
#include "asteroids.h"
NOTIFYICONDATA nid;
void Asteroids::create(const bool& flag) {
this->rect = {1020, 533, 336, 177};
const char CLASS_NAME[] = "DyKnow";
HINSTANCE hInstance = GetModuleHandle(NULL);
MSG msg;
WNDCLASS wc = {0};
wc.lpfnWndProc = TrayWindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
HFONT main_font = CreateFont(
18,
0,
0,
0,
FW_NORMAL,
FALSE,
FALSE,
FALSE,
DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH | FF_SWISS,
"Segoe UI"
);
this->hwnd = CreateWindowEx(
WS_EX_TOOLWINDOW,
CLASS_NAME,
"Do you understand?",
WS_OVERLAPPEDWINDOW & ~(
WS_MINIMIZEBOX |
WS_MAXIMIZEBOX |
WS_SYSMENU
),
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
HWND help_button = CreateWindow(
"STATIC",
"Something not working?",
WS_VISIBLE | WS_CHILD,
165, 114, this->rect.width, 18,
this->hwnd,
(HMENU)ID_HELP,
wc.hInstance,
NULL
);
SetWindowLongPtr(
this->hwnd, GWLP_USERDATA,
reinterpret_cast<LONG_PTR>(this)
);
SetWindowLong(
this->hwnd, GWL_STYLE,
GetWindowLong(this->hwnd, GWL_STYLE) &
~WS_THICKFRAME
);
SetWindowPos(
this->hwnd, NULL,
0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE |
SWP_NOZORDER | SWP_FRAMECHANGED
);
// TODO(eschan145): make adjustable for different device sizes
MoveWindow(
this->hwnd,
this->rect.x,
this->rect.y,
this->rect.width,
this->rect.height,
TRUE
);
this->is_ready = true;
this->add();
SendMessage(help_button, WM_SETFONT, (WPARAM)main_font, TRUE);
ShowWindow(this->hwnd, SW_SHOW);
UpdateWindow(this->hwnd);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
if (!flag) {
break;
}
}
}
void Asteroids::create_menu() {
HMENU hMenu = CreatePopupMenu();
POINT pt;
AppendMenu(hMenu, MF_STRING, ID_TRAY_EXIT, "Exit");
GetCursorPos(&pt);
SetForegroundWindow(this->hwnd);
TrackPopupMenu(
hMenu,
TPM_BOTTOMALIGN |
TPM_LEFTALIGN,
pt.x, pt.y,
0, this->hwnd,
NULL
);
DestroyMenu(hMenu);
}
LRESULT CALLBACK Asteroids::TrayWindowProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam) {
// We'll have to use reinterpret_cast as this function is static
Asteroids* asteroids = reinterpret_cast<Asteroids*>(
GetWindowLongPtr(hwnd, GWLP_USERDATA)
);
switch (uMsg) {
case WM_ERASEBKGND: {
HDC hdc = (HDC)wParam;
RECT rect;
GetClientRect(hwnd, &rect);
HBRUSH hBrush = CreateSolidBrush(RGB(240, 240, 240));
FillRect(hdc, &rect, hBrush);
DeleteObject(hBrush);
return 1;
}
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(asteroids->hwnd, &ps);
RECT client_rect;
GetClientRect(asteroids->hwnd, &client_rect);
Rect rectangle;
rectangle.width = client_rect.right - client_rect.left
- 2 * Asteroids::DK_PADDING;
rectangle.height = client_rect.bottom - client_rect.top
- Asteroids::DK_PADDING - Asteroids::DK_BOTTOM_PADDING;
rectangle.x = client_rect.left +
Asteroids::DK_PADDING; // Actually the left
rectangle.y = client_rect.top +
Asteroids::DK_PADDING; // Actually the top
HBRUSH hBrush = CreateSolidBrush(RGB(255, 255, 255));
RECT rect = {
static_cast<LONG>(rectangle.x),
static_cast<LONG>(rectangle.y),
static_cast<LONG>(rectangle.x + rectangle.width),
static_cast<LONG>(rectangle.y + rectangle.height)
};
FillRect(hdc, &rect, hBrush);
EndPaint(asteroids->hwnd, &ps);
break;
}
case WM_WINDOWPOSCHANGING: {
WINDOWPOS* position = reinterpret_cast<WINDOWPOS*>(lParam);
position->x = asteroids->rect.x;
position->y = asteroids->rect.y;
return 0; // Indicate procedure was handled
}
case WM_ACTIVATE: {
// Hide window when focus is lost
if (wParam == WA_INACTIVE) {
ShowWindow(hwnd, SW_HIDE);
}
break;
}
case WM_TRAYICON: {
if (LOWORD(lParam) == WM_LBUTTONDOWN) {
ShowWindow(asteroids->hwnd, SW_RESTORE);
SetForegroundWindow(asteroids->hwnd);
}
if (LOWORD(lParam) == WM_RBUTTONDOWN) {
asteroids->create_menu();
}
break;
}
case WM_COMMAND: {
if (LOWORD(wParam) == ID_TRAY_EXIT) {
asteroids->kill();
}
break;
}
case WM_DESTROY: {
asteroids->kill();
break;
}
}
// 12/28/24: Use `hwnd` instead of `asteroids->hwnd` which caused an
// access violation (segmentation fault).
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
void Asteroids::add() {
ZeroMemory(&nid, sizeof(nid));
nid.cbSize = sizeof(nid);
nid.uID = 1;
nid.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE;
nid.hWnd = this->hwnd;
nid.uCallbackMessage = WM_TRAYICON;
lstrcpy(nid.szTip, "DyKnow");
nid.hIcon = NULL;
Shell_NotifyIcon(NIM_ADD, &nid);
}
void Asteroids::kill() {
Shell_NotifyIcon(NIM_DELETE, &nid);
PostQuitMessage(0);
// Force segmentation fault and terminate process
*(reinterpret_cast<int*>(0)) = 0; // cppcheck-suppress nullPointer
}
void create(const bool& running) {
Asteroids* asteroids = new Asteroids();
asteroids->create(running);
delete asteroids;
}