-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskbarIconManager.cpp
More file actions
474 lines (437 loc) · 22.4 KB
/
Copy pathTaskbarIconManager.cpp
File metadata and controls
474 lines (437 loc) · 22.4 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
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
#include <windows.h>
#include <shellapi.h>
#include <shobjidl.h>
#include <commctrl.h>
#include <algorithm>
#include <string>
#include <vector>
namespace {
constexpr int kTitle = 100;
constexpr int kAppLabel = 101;
constexpr int kHotkeyLabel = 102;
constexpr int kBindingsLabel = 103;
constexpr int kAppList = 1001;
constexpr int kRefresh = 1002;
constexpr int kHideTaskbar = 1003;
constexpr int kRestoreTaskbar = 1004;
constexpr int kHideWindow = 1005;
constexpr int kRestoreWindow = 1006;
constexpr int kHotkeyControl = 1007;
constexpr int kBindToggleWindow = 1008;
constexpr int kBindToggleTaskbar = 1009;
constexpr int kBindingList = 1010;
constexpr int kRemoveBinding = 1011;
constexpr int kToggleOwnIcon = 1012;
constexpr int kEnableAutoHide = 1013;
constexpr int kDisableAutoHide = 1014;
constexpr int kExit = 1015;
enum class Operation { ToggleWindow, ToggleTaskbar };
struct TaskbarStyleSnapshot {
HWND window;
LONG_PTR originalTaskbarStyleBits;
};
struct HotkeyBinding {
int id;
HWND window;
Operation operation;
WORD hotkey;
};
HWND g_mainWindow = nullptr;
HWND g_appList = nullptr;
HWND g_bindingList = nullptr;
HWND g_hotkeyControl = nullptr;
HWND g_status = nullptr;
std::vector<TaskbarStyleSnapshot> g_taskbarHiddenWindows;
std::vector<HWND> g_hiddenWindows;
std::vector<HotkeyBinding> g_bindings;
int g_nextHotkeyId = 1;
void SetStatus(const wchar_t* text) { SetWindowText(g_status, text); }
bool SetTaskbarAutoHide(bool enabled) {
APPBARDATA data{};
data.cbSize = sizeof(data);
const DWORD state = static_cast<DWORD>(SHAppBarMessage(ABM_GETSTATE, &data));
data.lParam = enabled ? (state | ABS_AUTOHIDE) : (state & ~ABS_AUTOHIDE);
return SHAppBarMessage(ABM_SETSTATE, &data) != 0;
}
bool IsTaskbarApplicationWindow(HWND window) {
if (window == g_mainWindow || !IsWindowVisible(window)) return false;
const LONG_PTR style = GetWindowLongPtr(window, GWL_STYLE);
const LONG_PTR exStyle = GetWindowLongPtr(window, GWL_EXSTYLE);
if ((style & WS_CHILD) != 0 || (exStyle & WS_EX_TOOLWINDOW) != 0) return false;
if (GetWindow(window, GW_OWNER) != nullptr && (exStyle & WS_EX_APPWINDOW) == 0) return false;
return GetWindowTextLength(window) > 0;
}
std::wstring WindowTitle(HWND window) {
const int length = GetWindowTextLength(window);
std::wstring title(static_cast<size_t>(length) + 1, L'\0');
title.resize(static_cast<size_t>(GetWindowText(window, title.data(), length + 1)));
return title.empty() ? L"(untitled window)" : title;
}
std::wstring WindowLabel(HWND window, const wchar_t* state = L"") {
DWORD processId{};
GetWindowThreadProcessId(window, &processId);
return std::wstring(state) + WindowTitle(window) + L" (PID " + std::to_wstring(processId) + L")";
}
void AddWindowToList(HWND window, const wchar_t* state = L"") {
const std::wstring label = WindowLabel(window, state);
const auto index = SendMessage(g_appList, LB_ADDSTRING, 0, reinterpret_cast<LPARAM>(label.c_str()));
if (index != LB_ERR && index != LB_ERRSPACE)
SendMessage(g_appList, LB_SETITEMDATA, static_cast<WPARAM>(index), reinterpret_cast<LPARAM>(window));
}
BOOL CALLBACK EnumerateWindows(HWND window, LPARAM) {
if (IsTaskbarApplicationWindow(window)) AddWindowToList(window);
return TRUE;
}
TaskbarStyleSnapshot* FindTaskbarSnapshot(HWND window) {
const auto it = std::find_if(g_taskbarHiddenWindows.begin(), g_taskbarHiddenWindows.end(),
[window](const TaskbarStyleSnapshot& item) { return item.window == window; });
return it == g_taskbarHiddenWindows.end() ? nullptr : &*it;
}
bool IsWindowHiddenByManager(HWND window) {
return std::find(g_hiddenWindows.begin(), g_hiddenWindows.end(), window) != g_hiddenWindows.end();
}
bool ApplyTaskbarStyle(HWND window, LONG_PTR taskbarStyleBits) {
const bool wasMinimized = IsIconic(window) != FALSE;
ShowWindow(window, SW_HIDE);
const LONG_PTR current = GetWindowLongPtr(window, GWL_EXSTYLE);
const LONG_PTR updated = (current & ~(WS_EX_APPWINDOW | WS_EX_TOOLWINDOW)) | taskbarStyleBits;
SetLastError(ERROR_SUCCESS);
const LONG_PTR previous = SetWindowLongPtr(window, GWL_EXSTYLE, updated);
const DWORD error = previous == 0 ? GetLastError() : ERROR_SUCCESS;
if (error == ERROR_SUCCESS)
SetWindowPos(window, nullptr, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
ShowWindow(window, wasMinimized ? SW_SHOWMINNOACTIVE : SW_SHOWNA);
if (error != ERROR_SUCCESS) SetLastError(error);
return error == ERROR_SUCCESS;
}
bool HideTaskbarButton(HWND window) {
if (!window || !IsWindow(window)) return false;
if (!FindTaskbarSnapshot(window)) {
const LONG_PTR exStyle = GetWindowLongPtr(window, GWL_EXSTYLE);
g_taskbarHiddenWindows.push_back({window, exStyle & (WS_EX_APPWINDOW | WS_EX_TOOLWINDOW)});
}
if (ApplyTaskbarStyle(window, WS_EX_TOOLWINDOW)) return true;
g_taskbarHiddenWindows.erase(std::remove_if(g_taskbarHiddenWindows.begin(), g_taskbarHiddenWindows.end(),
[window](const TaskbarStyleSnapshot& item) { return item.window == window; }), g_taskbarHiddenWindows.end());
return false;
}
bool RestoreTaskbarButton(HWND window) {
const auto* snapshot = FindTaskbarSnapshot(window);
if (!snapshot) return false;
const LONG_PTR originalBits = snapshot->originalTaskbarStyleBits;
if (!ApplyTaskbarStyle(window, originalBits)) return false;
g_taskbarHiddenWindows.erase(std::remove_if(g_taskbarHiddenWindows.begin(), g_taskbarHiddenWindows.end(),
[window](const TaskbarStyleSnapshot& item) { return item.window == window; }), g_taskbarHiddenWindows.end());
return true;
}
bool HideWindow(HWND window) {
if (!window || !IsWindow(window)) return false;
if (!IsWindowHiddenByManager(window)) g_hiddenWindows.push_back(window);
ShowWindowAsync(window, SW_HIDE);
return true;
}
bool RestoreWindow(HWND window) {
if (!window || !IsWindowHiddenByManager(window)) return false;
ShowWindowAsync(window, SW_RESTORE);
g_hiddenWindows.erase(std::remove(g_hiddenWindows.begin(), g_hiddenWindows.end(), window), g_hiddenWindows.end());
return true;
}
bool ToggleWindow(HWND window) {
return IsWindowHiddenByManager(window) ? RestoreWindow(window) : HideWindow(window);
}
bool ToggleTaskbarButton(HWND window) {
return FindTaskbarSnapshot(window) ? RestoreTaskbarButton(window) : HideTaskbarButton(window);
}
std::wstring HotkeyText(WORD hotkey) {
const BYTE key = LOBYTE(hotkey);
const BYTE flags = HIBYTE(hotkey);
std::wstring text;
if (flags & HOTKEYF_CONTROL) text += L"Ctrl+";
if (flags & HOTKEYF_ALT) text += L"Alt+";
if (flags & HOTKEYF_SHIFT) text += L"Shift+";
if (key >= 'A' && key <= 'Z') return text + wchar_t(key);
if (key >= '0' && key <= '9') return text + wchar_t(key);
wchar_t name[64]{};
const UINT scan = MapVirtualKey(key, MAPVK_VK_TO_VSC);
if (GetKeyNameText(static_cast<LONG>(scan << 16), name, ARRAYSIZE(name))) return text + name;
return text + L"VK_" + std::to_wstring(key);
}
DWORD RegisterModifiers(BYTE flags) {
DWORD modifiers{};
if (flags & HOTKEYF_CONTROL) modifiers |= MOD_CONTROL;
if (flags & HOTKEYF_ALT) modifiers |= MOD_ALT;
if (flags & HOTKEYF_SHIFT) modifiers |= MOD_SHIFT;
return modifiers;
}
const wchar_t* OperationText(Operation operation) {
return operation == Operation::ToggleWindow ? L"Toggle window" : L"Toggle taskbar";
}
void RefreshBindingList() {
for (auto it = g_bindings.begin(); it != g_bindings.end();) {
if (!IsWindow(it->window)) {
UnregisterHotKey(g_mainWindow, it->id);
it = g_bindings.erase(it);
} else ++it;
}
SendMessage(g_bindingList, LB_RESETCONTENT, 0, 0);
for (const auto& binding : g_bindings) {
const std::wstring label = HotkeyText(binding.hotkey) + L" — " + OperationText(binding.operation) +
L" — " + WindowTitle(binding.window);
const auto index = SendMessage(g_bindingList, LB_ADDSTRING, 0, reinterpret_cast<LPARAM>(label.c_str()));
if (index != LB_ERR && index != LB_ERRSPACE)
SendMessage(g_bindingList, LB_SETITEMDATA, static_cast<WPARAM>(index), binding.id);
}
}
void RefreshApplicationList() {
g_taskbarHiddenWindows.erase(std::remove_if(g_taskbarHiddenWindows.begin(), g_taskbarHiddenWindows.end(),
[](const TaskbarStyleSnapshot& item) { return !IsWindow(item.window); }), g_taskbarHiddenWindows.end());
g_hiddenWindows.erase(std::remove_if(g_hiddenWindows.begin(), g_hiddenWindows.end(),
[](HWND window) { return !IsWindow(window); }), g_hiddenWindows.end());
SendMessage(g_appList, LB_RESETCONTENT, 0, 0);
EnumWindows(EnumerateWindows, 0);
for (const auto& item : g_taskbarHiddenWindows) AddWindowToList(item.window, L"[Taskbar hidden] ");
for (HWND window : g_hiddenWindows) AddWindowToList(window, L"[Window hidden] ");
RefreshBindingList();
}
HWND SelectedWindow() {
const auto index = SendMessage(g_appList, LB_GETCURSEL, 0, 0);
if (index == LB_ERR) return nullptr;
return reinterpret_cast<HWND>(SendMessage(g_appList, LB_GETITEMDATA, static_cast<WPARAM>(index), 0));
}
bool PerformOperation(HWND target, Operation operation) {
return operation == Operation::ToggleWindow ? ToggleWindow(target) : ToggleTaskbarButton(target);
}
void BindSelectedWindow(Operation operation) {
const HWND target = SelectedWindow();
if (!target) {
MessageBox(g_mainWindow, L"Select an application window first.", L"Taskbar Application Manager", MB_ICONINFORMATION | MB_OK);
return;
}
const WORD hotkey = static_cast<WORD>(SendMessage(g_hotkeyControl, HKM_GETHOTKEY, 0, 0));
const BYTE key = LOBYTE(hotkey);
const DWORD modifiers = RegisterModifiers(HIBYTE(hotkey));
if (key == 0 || (modifiers & (MOD_CONTROL | MOD_ALT)) == 0) {
MessageBox(g_mainWindow, L"Use Ctrl or Alt together with another key.", L"Taskbar Application Manager", MB_ICONWARNING | MB_OK);
return;
}
const int id = g_nextHotkeyId;
if (!RegisterHotKey(g_mainWindow, id, modifiers | MOD_NOREPEAT, key)) {
MessageBox(g_mainWindow, L"That global shortcut is unavailable. Choose another one.", L"Taskbar Application Manager", MB_ICONWARNING | MB_OK);
return;
}
++g_nextHotkeyId;
g_bindings.push_back({id, target, operation, hotkey});
RefreshBindingList();
SetStatus(L"Toggle shortcut registered. It works globally while this manager is running.");
}
void RemoveSelectedBinding() {
const auto index = SendMessage(g_bindingList, LB_GETCURSEL, 0, 0);
if (index == LB_ERR) return;
const int id = static_cast<int>(SendMessage(g_bindingList, LB_GETITEMDATA, static_cast<WPARAM>(index), 0));
const auto it = std::find_if(g_bindings.begin(), g_bindings.end(),
[id](const HotkeyBinding& binding) { return binding.id == id; });
if (it == g_bindings.end()) return;
UnregisterHotKey(g_mainWindow, it->id);
g_bindings.erase(it);
RefreshBindingList();
}
void RestoreAllManagedWindows() {
for (const auto& item : g_taskbarHiddenWindows)
if (IsWindow(item.window)) ApplyTaskbarStyle(item.window, item.originalTaskbarStyleBits);
for (HWND window : g_hiddenWindows)
if (IsWindow(window)) ShowWindowAsync(window, SW_RESTORE);
for (const auto& binding : g_bindings) UnregisterHotKey(g_mainWindow, binding.id);
g_taskbarHiddenWindows.clear();
g_hiddenWindows.clear();
g_bindings.clear();
}
void UpdateOwnIconState(HWND window) {
const bool hidden = (GetWindowLongPtr(window, GWL_EXSTYLE) & WS_EX_TOOLWINDOW) != 0;
CheckDlgButton(window, kToggleOwnIcon, hidden ? BST_CHECKED : BST_UNCHECKED);
}
void SetOwnTaskbarIconVisible(HWND window, bool visible) {
LONG_PTR style = GetWindowLongPtr(window, GWL_EXSTYLE);
if (visible) { style &= ~WS_EX_TOOLWINDOW; style |= WS_EX_APPWINDOW; }
else { style |= WS_EX_TOOLWINDOW; style &= ~WS_EX_APPWINDOW; }
SetWindowLongPtr(window, GWL_EXSTYLE, style);
SetWindowPos(window, nullptr, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
UpdateOwnIconState(window);
}
void Move(HWND window, int x, int y, int width, int height) {
MoveWindow(window, x, y, std::max(1, width), std::max(1, height), TRUE);
}
void LayoutControls(HWND window) {
RECT client{};
GetClientRect(window, &client);
const int width = client.right - client.left;
const int height = client.bottom - client.top;
constexpr int margin = 16;
constexpr int gap = 10;
constexpr int buttonHeight = 28;
const int available = std::max(1, width - margin * 2);
int y = 14;
Move(GetDlgItem(window, kTitle), margin, y, available, 22);
y += 30;
Move(GetDlgItem(window, kAppLabel), margin, y, available, 18);
y += 20;
const int appListHeight = std::clamp(height / 4, 90, 115);
Move(g_appList, margin, y, available, appListHeight);
y += appListHeight + gap;
const int refreshWidth = 95;
const int taskbarWidth = std::max(160, (available - refreshWidth - gap * 2) / 2);
Move(GetDlgItem(window, kRefresh), margin, y, refreshWidth, buttonHeight);
Move(GetDlgItem(window, kHideTaskbar), margin + refreshWidth + gap, y, taskbarWidth, buttonHeight);
Move(GetDlgItem(window, kRestoreTaskbar), margin + refreshWidth + gap + taskbarWidth + gap, y,
available - refreshWidth - taskbarWidth - gap * 2, buttonHeight);
y += buttonHeight + 7;
const int windowButtonWidth = (available - gap) / 2;
Move(GetDlgItem(window, kHideWindow), margin, y, windowButtonWidth, buttonHeight);
Move(GetDlgItem(window, kRestoreWindow), margin + windowButtonWidth + gap, y,
available - windowButtonWidth - gap, buttonHeight);
y += buttonHeight + 13;
Move(GetDlgItem(window, kHotkeyLabel), margin, y + 3, 82, 20);
Move(g_hotkeyControl, margin + 86, y, 180, 24);
y += 34;
const int bindWidth = (available - gap) / 2;
Move(GetDlgItem(window, kBindToggleWindow), margin, y, bindWidth, buttonHeight);
Move(GetDlgItem(window, kBindToggleTaskbar), margin + bindWidth + gap, y,
available - bindWidth - gap, buttonHeight);
y += buttonHeight + 10;
Move(GetDlgItem(window, kBindingsLabel), margin, y, available, 18);
y += 20;
const int removeWidth = 120;
const int footerHeight = 125;
const int bindingHeight = std::max(50, height - y - footerHeight);
Move(g_bindingList, margin, y, available - removeWidth - gap, bindingHeight);
Move(GetDlgItem(window, kRemoveBinding), margin + available - removeWidth, y + (bindingHeight - buttonHeight) / 2,
removeWidth, buttonHeight);
y += bindingHeight + 10;
Move(GetDlgItem(window, kToggleOwnIcon), margin, y, std::min(280, available), 24);
y += 30;
const int autoWidth = std::min(210, (available - gap) / 2);
Move(GetDlgItem(window, kEnableAutoHide), margin, y, autoWidth, buttonHeight);
Move(GetDlgItem(window, kDisableAutoHide), margin + autoWidth + gap, y, autoWidth, buttonHeight);
Move(GetDlgItem(window, kExit), margin + available - 80, y, 80, buttonHeight);
y += buttonHeight + 7;
Move(g_status, margin, y, available, std::max(20, height - y - 8));
}
LRESULT CALLBACK WindowProc(HWND window, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) {
case WM_GETMINMAXINFO: {
auto* info = reinterpret_cast<MINMAXINFO*>(lParam);
info->ptMinTrackSize = {620, 580};
return 0;
}
case WM_CREATE:
CreateWindow(L"STATIC", L"Taskbar Application Manager", WS_CHILD | WS_VISIBLE, 0, 0, 0, 0,
window, reinterpret_cast<HMENU>(kTitle), nullptr, nullptr);
CreateWindow(L"STATIC", L"Application windows", WS_CHILD | WS_VISIBLE, 0, 0, 0, 0,
window, reinterpret_cast<HMENU>(kAppLabel), nullptr, nullptr);
g_appList = CreateWindow(L"LISTBOX", L"", WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | LBS_NOINTEGRALHEIGHT,
0, 0, 0, 0, window, reinterpret_cast<HMENU>(kAppList), nullptr, nullptr);
CreateWindow(L"BUTTON", L"Refresh", WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, window,
reinterpret_cast<HMENU>(kRefresh), nullptr, nullptr);
CreateWindow(L"BUTTON", L"Hide taskbar button", WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, window,
reinterpret_cast<HMENU>(kHideTaskbar), nullptr, nullptr);
CreateWindow(L"BUTTON", L"Restore taskbar button", WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, window,
reinterpret_cast<HMENU>(kRestoreTaskbar), nullptr, nullptr);
CreateWindow(L"BUTTON", L"Hide window", WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, window,
reinterpret_cast<HMENU>(kHideWindow), nullptr, nullptr);
CreateWindow(L"BUTTON", L"Restore window", WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, window,
reinterpret_cast<HMENU>(kRestoreWindow), nullptr, nullptr);
CreateWindow(L"STATIC", L"Global hotkey", WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, window,
reinterpret_cast<HMENU>(kHotkeyLabel), nullptr, nullptr);
g_hotkeyControl = CreateWindow(HOTKEY_CLASS, L"", WS_CHILD | WS_VISIBLE | WS_BORDER, 0, 0, 0, 0,
window, reinterpret_cast<HMENU>(kHotkeyControl), nullptr, nullptr);
CreateWindow(L"BUTTON", L"Bind toggle: window", WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, window,
reinterpret_cast<HMENU>(kBindToggleWindow), nullptr, nullptr);
CreateWindow(L"BUTTON", L"Bind toggle: taskbar", WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, window,
reinterpret_cast<HMENU>(kBindToggleTaskbar), nullptr, nullptr);
CreateWindow(L"STATIC", L"Registered shortcuts", WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, window,
reinterpret_cast<HMENU>(kBindingsLabel), nullptr, nullptr);
g_bindingList = CreateWindow(L"LISTBOX", L"", WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | LBS_NOINTEGRALHEIGHT,
0, 0, 0, 0, window, reinterpret_cast<HMENU>(kBindingList), nullptr, nullptr);
CreateWindow(L"BUTTON", L"Remove binding", WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, window,
reinterpret_cast<HMENU>(kRemoveBinding), nullptr, nullptr);
CreateWindow(L"BUTTON", L"Hide this manager from taskbar", WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
0, 0, 0, 0, window, reinterpret_cast<HMENU>(kToggleOwnIcon), nullptr, nullptr);
CreateWindow(L"BUTTON", L"Enable taskbar auto-hide", WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, window,
reinterpret_cast<HMENU>(kEnableAutoHide), nullptr, nullptr);
CreateWindow(L"BUTTON", L"Disable taskbar auto-hide", WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, window,
reinterpret_cast<HMENU>(kDisableAutoHide), nullptr, nullptr);
CreateWindow(L"BUTTON", L"Exit", WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, window,
reinterpret_cast<HMENU>(kExit), nullptr, nullptr);
g_status = CreateWindow(L"STATIC", L"", WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, window, nullptr, nullptr, nullptr);
UpdateOwnIconState(window);
LayoutControls(window);
RefreshApplicationList();
SetStatus(L"Select a window, then use an action or bind a toggle shortcut.");
return 0;
case WM_SIZE:
if (g_appList) LayoutControls(window);
return 0;
case WM_HOTKEY: {
const int id = static_cast<int>(wParam);
const auto it = std::find_if(g_bindings.begin(), g_bindings.end(),
[id](const HotkeyBinding& binding) { return binding.id == id; });
if (it != g_bindings.end()) {
PerformOperation(it->window, it->operation);
RefreshApplicationList();
}
return 0;
}
case WM_COMMAND:
switch (LOWORD(wParam)) {
case kRefresh: RefreshApplicationList(); return 0;
case kHideTaskbar:
if (!HideTaskbarButton(SelectedWindow())) MessageBox(window, L"The target rejected the taskbar-style change.", L"Taskbar Application Manager", MB_ICONWARNING | MB_OK);
RefreshApplicationList(); return 0;
case kRestoreTaskbar: RestoreTaskbarButton(SelectedWindow()); RefreshApplicationList(); return 0;
case kHideWindow: HideWindow(SelectedWindow()); RefreshApplicationList(); return 0;
case kRestoreWindow: RestoreWindow(SelectedWindow()); RefreshApplicationList(); return 0;
case kBindToggleWindow: BindSelectedWindow(Operation::ToggleWindow); return 0;
case kBindToggleTaskbar: BindSelectedWindow(Operation::ToggleTaskbar); return 0;
case kRemoveBinding: RemoveSelectedBinding(); return 0;
case kToggleOwnIcon:
SetOwnTaskbarIconVisible(window, IsDlgButtonChecked(window, kToggleOwnIcon) != BST_CHECKED); return 0;
case kEnableAutoHide:
case kDisableAutoHide:
if (!SetTaskbarAutoHide(LOWORD(wParam) == kEnableAutoHide))
MessageBox(window, L"Windows did not accept the taskbar setting.", L"Taskbar Application Manager", MB_ICONWARNING | MB_OK);
return 0;
case kExit: DestroyWindow(window); return 0;
}
break;
case WM_DESTROY:
RestoreAllManagedWindows();
PostQuitMessage(0);
return 0;
}
return DefWindowProc(window, message, wParam, lParam);
}
} // namespace
int WINAPI wWinMain(HINSTANCE instance, HINSTANCE, PWSTR, int showCommand) {
INITCOMMONCONTROLSEX controls{sizeof(controls), ICC_WIN95_CLASSES};
InitCommonControlsEx(&controls);
SetCurrentProcessExplicitAppUserModelID(L"Local.TaskbarApplicationManager.3");
WNDCLASS windowClass{};
windowClass.hInstance = instance;
windowClass.lpszClassName = L"TaskbarApplicationManagerWindow";
windowClass.lpfnWndProc = WindowProc;
windowClass.hCursor = LoadCursor(nullptr, IDC_ARROW);
windowClass.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_BTNFACE + 1);
if (!RegisterClass(&windowClass)) return 0;
g_mainWindow = CreateWindowEx(WS_EX_APPWINDOW, windowClass.lpszClassName, L"Taskbar Application Manager",
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_THICKFRAME,
CW_USEDEFAULT, CW_USEDEFAULT, 660, 610, nullptr, nullptr, instance, nullptr);
if (!g_mainWindow) return 0;
ShowWindow(g_mainWindow, showCommand);
UpdateWindow(g_mainWindow);
MSG message{};
while (GetMessage(&message, nullptr, 0, 0) > 0) {
TranslateMessage(&message);
DispatchMessage(&message);
}
return static_cast<int>(message.wParam);
}