-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskbarApplicationManagerLite.cpp
More file actions
311 lines (284 loc) · 14.7 KB
/
Copy pathTaskbarApplicationManagerLite.cpp
File metadata and controls
311 lines (284 loc) · 14.7 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
/*
* Taskbar Application Manager Lite
* Copyright (C) 2026 Taskbar Application Manager contributors
* SPDX-License-Identifier: GPL-3.0-only
*/
#include <windows.h>
#include <shellapi.h>
#include <shobjidl.h>
#include <algorithm>
#include <string>
#include <vector>
namespace {
constexpr int kTitle = 100;
constexpr int kAppLabel = 101;
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 kToggleOwnIcon = 1007;
constexpr int kEnableAutoHide = 1008;
constexpr int kDisableAutoHide = 1009;
constexpr int kExit = 1010;
struct TaskbarStyleSnapshot {
HWND window;
LONG_PTR originalTaskbarStyleBits;
};
HWND g_mainWindow = nullptr;
HWND g_appList = nullptr;
HWND g_status = nullptr;
std::vector<TaskbarStyleSnapshot> g_taskbarHiddenWindows;
std::vector<HWND> g_hiddenWindows;
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 WindowLabel(HWND window, const wchar_t* state = L"") {
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)));
if (title.empty()) title = L"(untitled window)";
DWORD processId{};
GetWindowThreadProcessId(window, &processId);
return std::wstring(state) + title + 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);
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 || !ApplyTaskbarStyle(window, snapshot->originalTaskbarStyleBits)) 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;
}
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] ");
}
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));
}
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);
}
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 control, int x, int y, int width, int height) {
MoveWindow(control, 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;
const int height = client.bottom;
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 listHeight = std::max(125, height - 250);
Move(g_appList, margin, y, available, listHeight);
y += listHeight + gap;
const int refreshWidth = 95;
const int taskbarWidth = (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 + 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, 430};
return 0;
}
case WM_CREATE:
CreateWindow(L"STATIC", L"Taskbar Application Manager Lite", 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"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"Lite edition: taskbar and window controls only; no shortcut bindings.");
return 0;
case WM_SIZE:
if (g_appList) LayoutControls(window);
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 Lite", 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 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 Lite", 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) {
SetCurrentProcessExplicitAppUserModelID(L"Local.TaskbarApplicationManagerLite.1");
WNDCLASS windowClass{};
windowClass.hInstance = instance;
windowClass.lpszClassName = L"TaskbarApplicationManagerLiteWindow";
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 Lite",
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_THICKFRAME,
CW_USEDEFAULT, CW_USEDEFAULT, 660, 500, 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);
}