-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain.c
More file actions
438 lines (308 loc) · 9.61 KB
/
Copy pathMain.c
File metadata and controls
438 lines (308 loc) · 9.61 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
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
// KeepAnEyeOut
// Joseph Ryan Ries - 2022
// https://github.com/ryanries/KeepAnEyeOut
// This app lets you keep an eye on another app in the background while you are multitasking.
// I originally created this app so I could keep an eye on my MMORPG while I was camping a mob.
// This way I could see when the mob had respawned even if I was browsing the web or working on something else.
// Important: Per-monitor DPI awareness should be set in the application manifest.
//
#include <windows.h>
#include <stdio.h>
#include <dwmapi.h>
#include "Main.h"
#include "resource.h"
#pragma comment(lib, "dwmapi.lib")
HWND gMainWindow;
HWND gLabel01;
HWND gComboBox01;
HWND gButton01;
HBITMAP gRefreshButtonImage;
HTHUMBNAIL gThumbnail;
DWM_THUMBNAIL_PROPERTIES gThumbnailProperties = { 0 };
BOOL gAppIsRunning = TRUE;
int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ PWSTR pCmdLine, _In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hInstance);
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(pCmdLine);
UNREFERENCED_PARAMETER(nCmdShow);
DWORD Result = ERROR_SUCCESS;
wchar_t ErrorMessage[256] = { 0 };
MSG WindowMessage = { 0 };
WNDCLASSEXW WindowClass = { 0 };
DWORD RefreshTime = 0;
WindowClass.cbSize = sizeof(WNDCLASSEXW);
WindowClass.lpfnWndProc = MainWindowProc;
WindowClass.lpszClassName = L"KeepAnEyeOutWindowClass";
WindowClass.style = CS_HREDRAW | CS_VREDRAW;
WindowClass.hbrBackground = (HBRUSH)COLOR_WINDOW;
WindowClass.hCursor = LoadCursorW(NULL, IDC_ARROW);
// Create the main window.
if (RegisterClassExW(&WindowClass) == 0)
{
Result = GetLastError();
_snwprintf_s(ErrorMessage, _countof(ErrorMessage), _TRUNCATE, L"Failed to register window class! Error: 0x%08lx", Result);
MessageBoxW(NULL, ErrorMessage, L"ERROR", MB_ICONERROR | MB_OK);
goto Exit;
}
gMainWindow = CreateWindowExW(
WS_EX_TOPMOST,
WindowClass.lpszClassName,
L"KeepAnEyeOut",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT,
CW_USEDEFAULT,
412,
320,
NULL,
NULL,
GetModuleHandleW(NULL),
NULL);
if (gMainWindow == NULL)
{
Result = GetLastError();
_snwprintf_s(ErrorMessage, _countof(ErrorMessage), _TRUNCATE, L"Failed to create window! Error: 0x%08lx", Result);
MessageBoxW(NULL, ErrorMessage, L"ERROR", MB_ICONERROR | MB_OK);
goto Exit;
}
while (gAppIsRunning == TRUE)
{
// PeekMessageW(&WindowMessage, gMainWindow, 0, 0, PM_REMOVE) handles messages for the main window ONLY!
// By using NULL, we can process messages for the main window and all of the child windows/controls.
while (PeekMessageW(&WindowMessage, NULL, 0, 0, PM_REMOVE))
{
DispatchMessageW(&WindowMessage);
}
// Periodically re-set the window to top-most, to prevent the window from being hidden
// underneath other top-most windows, for example, a fullscreen RDP session or fullscreen video.
// However, do NOT do this if the combobox is currently dropped down, because this action causes
// the combobox to collapse if dropped down, and that would really annoy the user if the user
// was trying to interact with the combobox when this happened.
if ((RefreshTime > 2000) && (SendMessageW(gComboBox01, CB_GETDROPPEDSTATE, 0, 0) == FALSE))
{
RefreshTime = 0;
SetWindowPos(gMainWindow, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
}
RefreshTime += 10;
Sleep(10);
}
Exit:
return(Result);
}
LRESULT CALLBACK MainWindowProc(_In_ HWND WindowHandle, _In_ UINT Message, _In_ WPARAM WParam, _In_ LPARAM LParam)
{
LRESULT Result = 0;
switch (Message)
{
case WM_CREATE:
{
HFONT Font = CreateFontW(14, 0, 0, 0, 200, 0, 0, 0, 0, 0, 0, 0, 0, L"Consolas");
wchar_t ErrorMessage[256] = { 0 };
// Create the static label.
gLabel01 = CreateWindowExW(
0,
L"STATIC",
L"Choose a window to keep an eye on:",
WS_VISIBLE | WS_CHILD | SS_LEFT,
4,
4,
300,
20,
WindowHandle,
(HMENU)LABEL01_ID,
GetModuleHandleW(NULL),
NULL);
if (gLabel01 == NULL)
{
_snwprintf_s(ErrorMessage, _countof(ErrorMessage), _TRUNCATE, L"Failed to create label! Error: 0x%08lx", GetLastError());
MessageBoxW(NULL, ErrorMessage, L"ERROR", MB_ICONERROR | MB_OK);
gAppIsRunning = FALSE;
break;
}
SendMessageW(gLabel01, WM_SETFONT, (WPARAM)Font, 0);
// Create the dropdown box.
gComboBox01 = CreateWindowExW(
0,
L"COMBOBOX",
NULL,
CBS_DROPDOWN | CBS_HASSTRINGS | WS_CHILD | WS_OVERLAPPED | WS_VISIBLE | WS_VSCROLL,
4,
24,
350,
300,
WindowHandle,
(HMENU)COMBOBOX01_ID,
GetModuleHandleW(NULL),
NULL);
if (gComboBox01 == NULL)
{
_snwprintf_s(ErrorMessage, _countof(ErrorMessage), _TRUNCATE, L"Failed to create combobox! Error: 0x%08lx", GetLastError());
MessageBoxW(NULL, ErrorMessage, L"ERROR", MB_ICONERROR | MB_OK);
gAppIsRunning = FALSE;
break;
}
SendMessageW(gComboBox01, WM_SETFONT, (WPARAM)Font, 0);
EnumWindows(EnumWindowsProc, 0);
// Create the refresh button.
gButton01 = CreateWindowExW(
0,
L"BUTTON",
NULL,
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_BITMAP,
360,
22,
24,
24,
WindowHandle,
(HMENU)BUTTON01_ID,
GetModuleHandleW(NULL),
NULL);
if (gButton01 == NULL)
{
_snwprintf_s(ErrorMessage, _countof(ErrorMessage), _TRUNCATE, L"Failed to create button! Error: 0x%08lx", GetLastError());
MessageBoxW(NULL, ErrorMessage, L"ERROR", MB_ICONERROR | MB_OK);
gAppIsRunning = FALSE;
break;
}
gRefreshButtonImage = (HBITMAP)LoadImageW(
GetModuleHandleW(NULL),
MAKEINTRESOURCEW(IDB_BITMAP1),
IMAGE_BITMAP,
0,
0,
LR_DEFAULTCOLOR);
if (gRefreshButtonImage == NULL)
{
_snwprintf_s(ErrorMessage, _countof(ErrorMessage), _TRUNCATE, L"Failed to load refresh bitmap! Error: 0x%08lx", GetLastError());
MessageBoxW(NULL, ErrorMessage, L"ERROR", MB_ICONERROR | MB_OK);
gAppIsRunning = FALSE;
break;
}
SendMessageW(gButton01, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)gRefreshButtonImage);
break;
}
case WM_COMMAND:
{
switch (LOWORD(WParam))
{
case BUTTON01_ID:
{
if (gThumbnail != NULL)
{
DwmUnregisterThumbnail(gThumbnail);
gThumbnail = NULL;
}
SendMessageW(gComboBox01, CB_RESETCONTENT, 0, 0);
EnumWindows(EnumWindowsProc, 0);
break;
}
case COMBOBOX01_ID:
{
if (HIWORD(WParam) == CBN_SELCHANGE)
{
HRESULT Hr = S_OK;
wchar_t ErrorMessage[256] = { 0 };
wchar_t WindowText[256] = { 0 };
DWORD SelectedIndex = 0;
// First we unregister any previous DWM thumbnail, or else we leak.
if (gThumbnail != NULL)
{
Hr = DwmUnregisterThumbnail(gThumbnail);
gThumbnail = NULL;
}
//
SelectedIndex = (DWORD)SendMessageW(gComboBox01, CB_GETCURSEL, 0, 0);
SendMessageW(gComboBox01, CB_GETLBTEXT, (WPARAM)SelectedIndex, (LPARAM)WindowText);
Hr = DwmRegisterThumbnail(
gMainWindow,
FindWindowW(NULL, WindowText),
&gThumbnail);
if (SUCCEEDED(Hr))
{
RECT ClientRect = { 0 };
GetClientRect(gMainWindow, &ClientRect);
ClientRect.left += 4;
ClientRect.right -= 4;
ClientRect.bottom -= 4;
ClientRect.top += 50;
gThumbnailProperties.dwFlags = DWM_TNP_SOURCECLIENTAREAONLY | DWM_TNP_VISIBLE | DWM_TNP_OPACITY | DWM_TNP_RECTDESTINATION;
gThumbnailProperties.fSourceClientAreaOnly = FALSE;
gThumbnailProperties.fVisible = TRUE;
gThumbnailProperties.opacity = 255;
// IMPORTANT: Per-Monitor DPI Awareness set in application manifest!
gThumbnailProperties.rcDestination = ClientRect;
Hr = DwmUpdateThumbnailProperties(gThumbnail, &gThumbnailProperties);
if (FAILED(Hr))
{
_snwprintf_s(ErrorMessage, _countof(ErrorMessage), _TRUNCATE, L"DwmUpdateThumbnailProperties failed! HRESULT = %08lx", Hr);
MessageBoxW(NULL, ErrorMessage, L"ERROR", MB_OK | MB_ICONERROR);
}
}
else
{
_snwprintf_s(ErrorMessage, _countof(ErrorMessage), _TRUNCATE, L"DwmRegisterThumbnail failed! HRESULT = %08lx", Hr);
MessageBoxW(NULL, ErrorMessage, L"ERROR", MB_OK | MB_ICONERROR);
}
}
break;
}
default:
{
}
}
break;
}
case WM_CLOSE:
{
gAppIsRunning = FALSE;
PostQuitMessage(0);
break;
}
case WM_LBUTTONUP:
{
if (gThumbnail != NULL)
{
wchar_t WindowText[256] = { 0 };
DWORD SelectedIndex = (DWORD)SendMessageW(gComboBox01, CB_GETCURSEL, 0, 0);
SendMessageW(gComboBox01, CB_GETLBTEXT, (WPARAM)SelectedIndex, (LPARAM)WindowText);
BringWindowToTop(FindWindowW(NULL, WindowText));
}
break;
}
case WM_SIZE:
case WM_SIZING:
{
if (gThumbnail != NULL)
{
RECT ClientRect = { 0 };
GetClientRect(gMainWindow, &ClientRect);
ClientRect.left += 4;
ClientRect.right -= 4;
ClientRect.bottom -= 4;
ClientRect.top += 50;
gThumbnailProperties.rcDestination = ClientRect;
DwmUpdateThumbnailProperties(gThumbnail, &gThumbnailProperties);
}
return(TRUE);
}
default:
{
Result = DefWindowProcW(WindowHandle, Message, WParam, LParam);
}
}
return(Result);
}
BOOL CALLBACK EnumWindowsProc(_In_ HWND WindowHandle, _In_ LPARAM LParam)
{
UNREFERENCED_PARAMETER(LParam);
if (IsWindowVisible(WindowHandle))
{
wchar_t WindowTitle[256] = { 0 };
GetWindowTextW(WindowHandle, WindowTitle, _countof(WindowTitle));
if ((wcslen(WindowTitle) > 0) && (WindowHandle != gMainWindow) && IsWindowVisible(WindowHandle))
{
SendMessageW(gComboBox01, CB_ADDSTRING, 0, (LPARAM)WindowTitle);
}
}
return(TRUE);
}