-
Notifications
You must be signed in to change notification settings - Fork 0
/
windowstool.cpp
530 lines (424 loc) · 13.6 KB
/
windowstool.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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// windowstool.cpp
#include "misc.h"
#include "windowstool.h"
#include "array.h"
#include <windowsx.h>
#include <malloc.h>
#include <shlwapi.h>
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Global variables
// instance handle of this application
HINSTANCE g_hInst = NULL;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Functions
// load resource string
tstring loadString(UINT i_id)
{
_TCHAR buf[1024];
if (LoadString(g_hInst, i_id, buf, NUMBER_OF(buf)))
return tstring(buf);
else
return _T("");
}
// load small icon resource
HICON loadSmallIcon(UINT i_id)
{
return reinterpret_cast<HICON>(
LoadImage(g_hInst, MAKEINTRESOURCE(i_id), IMAGE_ICON, 16, 16, 0));
}
// load big icon resource
HICON loadBigIcon(UINT i_id)
{
return reinterpret_cast<HICON>(
LoadImage(g_hInst, MAKEINTRESOURCE(i_id), IMAGE_ICON, 32, 32, 0));
}
// set small icon to the specified window.
// @return handle of previous icon or NULL
HICON setSmallIcon(HWND i_hwnd, UINT i_id)
{
HICON hicon = (i_id == static_cast<UINT>(-1)) ? NULL : loadSmallIcon(i_id);
return reinterpret_cast<HICON>(
SendMessage(i_hwnd, WM_SETICON, static_cast<WPARAM>(ICON_SMALL),
reinterpret_cast<LPARAM>(hicon)));
}
// set big icon to the specified window.
// @return handle of previous icon or NULL
HICON setBigIcon(HWND i_hwnd, UINT i_id)
{
HICON hicon = (i_id == static_cast<UINT>(-1)) ? NULL : loadBigIcon(i_id);
return reinterpret_cast<HICON>(
SendMessage(i_hwnd, WM_SETICON, static_cast<WPARAM>(ICON_BIG),
reinterpret_cast<LPARAM>(hicon)));
}
// remove icon from a window that is set by setSmallIcon
void unsetSmallIcon(HWND i_hwnd)
{
HICON hicon = setSmallIcon(i_hwnd, -1);
if (hicon)
CHECK_TRUE( DestroyIcon(hicon) );
}
// remove icon from a window that is set by setBigIcon
void unsetBigIcon(HWND i_hwnd)
{
HICON hicon = setBigIcon(i_hwnd, -1);
if (hicon)
CHECK_TRUE( DestroyIcon(hicon) );
}
// resize the window (it does not move the window)
bool resizeWindow(HWND i_hwnd, int i_w, int i_h, bool i_doRepaint)
{
UINT flag = SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER;
if (!i_doRepaint)
flag |= SWP_NOREDRAW;
return !!SetWindowPos(i_hwnd, NULL, 0, 0, i_w, i_h, flag);
}
// get rect of the window in client coordinates
// @return rect of the window in client coordinates
bool getChildWindowRect(HWND i_hwnd, RECT *o_rc)
{
if (!GetWindowRect(i_hwnd, o_rc))
return false;
POINT p = { o_rc->left, o_rc->top };
HWND phwnd = GetParent(i_hwnd);
if (!phwnd)
return false;
if (!ScreenToClient(phwnd, &p))
return false;
o_rc->left = p.x;
o_rc->top = p.y;
p.x = o_rc->right;
p.y = o_rc->bottom;
ScreenToClient(phwnd, &p);
o_rc->right = p.x;
o_rc->bottom = p.y;
return true;
}
// get toplevel (non-child) window
HWND getToplevelWindow(HWND i_hwnd, bool *io_isMDI)
{
while (i_hwnd) {
#ifdef MAYU64
LONG_PTR style = GetWindowLongPtr(i_hwnd, GWL_STYLE);
#else
LONG style = GetWindowLong(i_hwnd, GWL_STYLE);
#endif
if ((style & WS_CHILD) == 0)
break;
if (io_isMDI && *io_isMDI) {
#ifdef MAYU64
LONG_PTR exStyle = GetWindowLongPtr(i_hwnd, GWL_EXSTYLE);
#else
LONG exStyle = GetWindowLong(i_hwnd, GWL_EXSTYLE);
#endif
if (exStyle & WS_EX_MDICHILD)
return i_hwnd;
}
i_hwnd = GetParent(i_hwnd);
}
if (io_isMDI)
*io_isMDI = false;
return i_hwnd;
}
// move window asynchronously
void asyncMoveWindow(HWND i_hwnd, int i_x, int i_y)
{
SetWindowPos(i_hwnd, NULL, i_x, i_y, 0, 0,
SWP_ASYNCWINDOWPOS | SWP_NOACTIVATE | SWP_NOOWNERZORDER |
SWP_NOSIZE | SWP_NOZORDER);
}
// move window asynchronously
void asyncMoveWindow(HWND i_hwnd, int i_x, int i_y, int i_w, int i_h)
{
SetWindowPos(i_hwnd, NULL, i_x, i_y, i_w, i_h,
SWP_ASYNCWINDOWPOS | SWP_NOACTIVATE | SWP_NOOWNERZORDER |
SWP_NOZORDER);
}
// resize asynchronously
void asyncResize(HWND i_hwnd, int i_w, int i_h)
{
SetWindowPos(i_hwnd, NULL, 0, 0, i_w, i_h,
SWP_ASYNCWINDOWPOS | SWP_NOACTIVATE | SWP_NOOWNERZORDER |
SWP_NOMOVE | SWP_NOZORDER);
}
// get dll version
DWORD getDllVersion(const _TCHAR *i_dllname)
{
DWORD dwVersion = 0;
if (HINSTANCE hinstDll = LoadLibrary(i_dllname)) {
DLLGETVERSIONPROC pDllGetVersion
= (DLLGETVERSIONPROC)GetProcAddress(hinstDll, "DllGetVersion");
/* Because some DLLs may not implement this function, you
* must test for it explicitly. Depending on the particular
* DLL, the lack of a DllGetVersion function may
* be a useful indicator of the version.
*/
if (pDllGetVersion) {
DLLVERSIONINFO dvi;
ZeroMemory(&dvi, sizeof(dvi));
dvi.cbSize = sizeof(dvi);
HRESULT hr = (*pDllGetVersion)(&dvi);
if (SUCCEEDED(hr))
dwVersion = PACKVERSION(dvi.dwMajorVersion, dvi.dwMinorVersion);
}
FreeLibrary(hinstDll);
}
return dwVersion;
}
// workaround of SetForegroundWindow
bool setForegroundWindow(HWND i_hwnd)
{
int nForegroundID = GetWindowThreadProcessId(GetForegroundWindow(), NULL);
int nTargetID = GetWindowThreadProcessId(i_hwnd, NULL);
//if (!AttachThreadInput(nTargetID, nForegroundID, TRUE))
//return false;
AttachThreadInput(nTargetID, nForegroundID, TRUE);
DWORD sp_time;
SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, &sp_time, 0);
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, (void *)0, 0);
SetForegroundWindow(i_hwnd);
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, (void *)sp_time, 0);
AttachThreadInput(nTargetID, nForegroundID, FALSE);
return true;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// edit control
// get edit control's text size
// @return bytes of text
size_t editGetTextBytes(HWND i_hwnd)
{
return Edit_GetTextLength(i_hwnd);
}
// delete a line
void editDeleteLine(HWND i_hwnd, size_t i_n)
{
int len = Edit_LineLength(i_hwnd, i_n);
if (len < 0)
return;
len += 2;
int index = Edit_LineIndex(i_hwnd, i_n);
Edit_SetSel(i_hwnd, index, index + len);
Edit_ReplaceSel(i_hwnd, _T(""));
}
// insert text at last
void editInsertTextAtLast(HWND i_hwnd, const tstring &i_text,
size_t i_threshold)
{
if (i_text.empty())
return;
size_t len = editGetTextBytes(i_hwnd);
if (i_threshold < len) {
Edit_SetSel(i_hwnd, 0, len / 3 * 2);
Edit_ReplaceSel(i_hwnd, _T(""));
editDeleteLine(i_hwnd, 0);
len = editGetTextBytes(i_hwnd);
}
Edit_SetSel(i_hwnd, len, len);
// \n -> \r\n
Array<_TCHAR> buf(i_text.size() * 2 + 1);
_TCHAR *d = buf.get();
const _TCHAR *str = i_text.c_str();
for (const _TCHAR *s = str; s < str + i_text.size(); ++ s) {
if (*s == _T('\n'))
*d++ = _T('\r');
*d++ = *s;
}
*d = _T('\0');
Edit_ReplaceSel(i_hwnd, buf.get());
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Windows2000/XP specific API
// initialize layerd window
static BOOL WINAPI initalizeLayerdWindow(
HWND i_hwnd, COLORREF i_crKey, BYTE i_bAlpha, DWORD i_dwFlags)
{
HMODULE hModule = GetModuleHandle(_T("user32.dll"));
if (!hModule) {
return FALSE;
}
SetLayeredWindowAttributes_t proc =
reinterpret_cast<SetLayeredWindowAttributes_t>(
GetProcAddress(hModule, "SetLayeredWindowAttributes"));
if (setLayeredWindowAttributes) {
setLayeredWindowAttributes = proc;
return setLayeredWindowAttributes(i_hwnd, i_crKey, i_bAlpha, i_dwFlags);
} else {
return FALSE;
}
}
// SetLayeredWindowAttributes API
SetLayeredWindowAttributes_t setLayeredWindowAttributes
= initalizeLayerdWindow;
// emulate MonitorFromWindow API
static HMONITOR WINAPI emulateMonitorFromWindow(HWND hwnd, DWORD dwFlags)
{
return reinterpret_cast<HMONITOR>(1); // dummy HMONITOR
}
// initialize MonitorFromWindow API
static HMONITOR WINAPI initializeMonitorFromWindow(HWND hwnd, DWORD dwFlags)
{
HMODULE hModule = GetModuleHandle(_T("user32.dll"));
if (!hModule)
return FALSE;
FARPROC proc = GetProcAddress(hModule, "MonitorFromWindow");
if (proc)
monitorFromWindow =
reinterpret_cast<HMONITOR (WINAPI *)(HWND, DWORD)>(proc);
else
monitorFromWindow = emulateMonitorFromWindow;
return monitorFromWindow(hwnd, dwFlags);
}
// MonitorFromWindow API
HMONITOR (WINAPI *monitorFromWindow)(HWND hwnd, DWORD dwFlags)
= initializeMonitorFromWindow;
// emulate GetMonitorInfo API
static BOOL WINAPI emulateGetMonitorInfo(HMONITOR hMonitor, LPMONITORINFO lpmi)
{
if (lpmi->cbSize != sizeof(MONITORINFO))
return FALSE;
lpmi->rcMonitor.left = 0;
lpmi->rcMonitor.top = 0;
lpmi->rcMonitor.right = GetSystemMetrics(SM_CXFULLSCREEN);
lpmi->rcMonitor.bottom = GetSystemMetrics(SM_CYFULLSCREEN);
SystemParametersInfo(SPI_GETWORKAREA, 0,
reinterpret_cast<PVOID>(&lpmi->rcWork), FALSE);
lpmi->dwFlags = MONITORINFOF_PRIMARY;
return TRUE;
}
// initialize GetMonitorInfo API
static
BOOL WINAPI initializeGetMonitorInfo(HMONITOR hMonitor, LPMONITORINFO lpmi)
{
HMODULE hModule = GetModuleHandle(_T("user32.dll"));
if (!hModule)
return FALSE;
FARPROC proc = GetProcAddress(hModule, "GetMonitorInfoA");
if (proc)
getMonitorInfo =
reinterpret_cast<BOOL (WINAPI *)(HMONITOR, LPMONITORINFO)>(proc);
else
getMonitorInfo = emulateGetMonitorInfo;
return getMonitorInfo(hMonitor, lpmi);
}
// GetMonitorInfo API
BOOL (WINAPI *getMonitorInfo)(HMONITOR hMonitor, LPMONITORINFO lpmi)
= initializeGetMonitorInfo;
// enumalte EnumDisplayMonitors API
static BOOL WINAPI emulateEnumDisplayMonitors(
HDC hdc, LPRECT lprcClip, MONITORENUMPROC lpfnEnum, LPARAM dwData)
{
lpfnEnum(reinterpret_cast<HMONITOR>(1), hdc, lprcClip, dwData);
return TRUE;
}
// initialize EnumDisplayMonitors API
static BOOL WINAPI initializeEnumDisplayMonitors(
HDC hdc, LPRECT lprcClip, MONITORENUMPROC lpfnEnum, LPARAM dwData)
{
HMODULE hModule = GetModuleHandle(_T("user32.dll"));
if (!hModule)
return FALSE;
FARPROC proc = GetProcAddress(hModule, "EnumDisplayMonitors");
if (proc)
enumDisplayMonitors =
reinterpret_cast<BOOL (WINAPI *)(HDC, LPRECT, MONITORENUMPROC, LPARAM)>
(proc);
else
enumDisplayMonitors = emulateEnumDisplayMonitors;
return enumDisplayMonitors(hdc, lprcClip, lpfnEnum, dwData);
}
// EnumDisplayMonitors API
BOOL (WINAPI *enumDisplayMonitors)
(HDC hdc, LPRECT lprcClip, MONITORENUMPROC lpfnEnum, LPARAM dwData)
= initializeEnumDisplayMonitors;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Windows2000/XP specific API
static BOOL WINAPI
initializeWTSRegisterSessionNotification(HWND hWnd, DWORD dwFlags)
{
LoadLibrary(_T("wtsapi32.dll"));
HMODULE hModule = GetModuleHandle(_T("wtsapi32.dll"));
if (!hModule) {
return FALSE;
}
WTSRegisterSessionNotification_t proc =
reinterpret_cast<WTSRegisterSessionNotification_t>(
GetProcAddress(hModule, "WTSRegisterSessionNotification"));
if (proc) {
wtsRegisterSessionNotification = proc;
return wtsRegisterSessionNotification(hWnd, dwFlags);
} else {
return 0;
}
}
// WTSRegisterSessionNotification API
WTSRegisterSessionNotification_t wtsRegisterSessionNotification
= initializeWTSRegisterSessionNotification;
static BOOL WINAPI initializeWTSUnRegisterSessionNotification(HWND hWnd)
{
HMODULE hModule = GetModuleHandle(_T("wtsapi32.dll"));
if (!hModule) {
return FALSE;
}
WTSUnRegisterSessionNotification_t proc =
reinterpret_cast<WTSUnRegisterSessionNotification_t>(
GetProcAddress(hModule, "WTSUnRegisterSessionNotification"));
if (proc) {
wtsUnRegisterSessionNotification = proc;
return wtsUnRegisterSessionNotification(hWnd);
} else {
return 0;
}
}
// WTSUnRegisterSessionNotification API
WTSUnRegisterSessionNotification_t wtsUnRegisterSessionNotification
= initializeWTSUnRegisterSessionNotification;
static DWORD WINAPI initializeWTSGetActiveConsoleSessionId(void)
{
HMODULE hModule = GetModuleHandle(_T("kernel32.dll"));
if (!hModule) {
return FALSE;
}
WTSGetActiveConsoleSessionId_t proc =
reinterpret_cast<WTSGetActiveConsoleSessionId_t>(
GetProcAddress(hModule, "WTSGetActiveConsoleSessionId"));
if (proc) {
wtsGetActiveConsoleSessionId = proc;
return wtsGetActiveConsoleSessionId();
} else {
return 0;
}
}
// WTSGetActiveConsoleSessionId API
WTSGetActiveConsoleSessionId_t wtsGetActiveConsoleSessionId
= initializeWTSGetActiveConsoleSessionId;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Utility
// PathRemoveFileSpec()
tstring pathRemoveFileSpec(const tstring &i_path)
{
const _TCHAR *str = i_path.c_str();
const _TCHAR *b = _tcsrchr(str, _T('\\'));
const _TCHAR *s = _tcsrchr(str, _T('/'));
if (b && s)
return tstring(str, MIN(b, s));
if (b)
return tstring(str, b);
if (s)
return tstring(str, s);
if (const _TCHAR *c = _tcsrchr(str, _T(':')))
return tstring(str, c + 1);
return i_path;
}
BOOL checkWindowsVersion(DWORD i_major, DWORD i_minor)
{
DWORDLONG conditionMask = 0;
OSVERSIONINFOEX osvi;
memset(&osvi, 0, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
osvi.dwMajorVersion = i_major;
osvi.dwMinorVersion = i_minor;
VER_SET_CONDITION(conditionMask, VER_MAJORVERSION, VER_GREATER_EQUAL);
VER_SET_CONDITION(conditionMask, VER_MINORVERSION, VER_GREATER_EQUAL);
// Perform the test.
return VerifyVersionInfo(&osvi, VER_MAJORVERSION | VER_MINORVERSION, conditionMask);
}