-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal_solution_cpp.cpp
More file actions
315 lines (275 loc) · 10.3 KB
/
final_solution_cpp.cpp
File metadata and controls
315 lines (275 loc) · 10.3 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
// TaskbarHider Pro (Windows 11) — headless GUI (no console/no tray/no logs)
// - Hides only the Explorer system taskbar and prevents it from appearing on hover
// - Does not affect third-party taskbars (YASB, etc.)
// - Win: temporarily show for 10 seconds (press Win again to instantly hide without blinking)
// - Alt+` — exit with restore
// - "Finished" hiding thread
// - Expands the workspace only when third-party managers are not present
#ifndef UNICODE
#define UNICODE
#endif
#ifndef _UNICODE
#define _UNICODE
#endif
#include <windows.h>
#include <shellapi.h>
#include <tlhelp32.h>
#include <vector>
#include <string>
#include <thread>
#include <atomic>
#include <algorithm>
#include <cwctype>
#ifndef ABM_SETSTATE
#define ABM_SETSTATE 0x0000000A
#endif
#ifndef ABS_AUTOHIDE
#define ABS_AUTOHIDE 0x0000001
#endif
#ifndef ABS_ALWAYSONTOP
#define ABS_ALWAYSONTOP 0x0000002
#endif
// --- Globals/State ---
static std::vector<HWND> g_explorer_taskbars;
static std::vector<HWND> g_third_party_taskbars;
static bool g_has_taskbar_managers = false;
static std::atomic<bool> g_exit_flag{false};
static std::atomic<bool> g_desired_hidden{true};
static std::atomic<bool> g_panel_temp_visible{false};
static std::atomic<ULONGLONG> g_show_deadline_ms{0};
static RECT g_original_work_area{};
static bool g_has_original_work_area = false;
// --- Utils ---
static inline ULONGLONG now_ms() { return GetTickCount64(); }
static inline bool key_down(int vk) { return (GetAsyncKeyState(vk) & 0x8000) != 0; }
static std::wstring to_lower(std::wstring s) {
std::transform(s.begin(), s.end(), s.begin(),
[](wchar_t c){ return (wchar_t)towlower(c); });
return s;
}
static bool ends_with_icase(const std::wstring& s, const std::wstring& suf) {
if (s.size() < suf.size()) return false;
std::wstring a = to_lower(s);
std::wstring b = to_lower(suf);
return a.compare(a.size() - b.size(), b.size(), b) == 0;
}
// --- Detect managers (через Toolhelp, без tasklist) ---
static bool detect_taskbar_managers() {
HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (snap == INVALID_HANDLE_VALUE) return false;
const wchar_t* managers[] = {
L"yasb.exe", L"taskbarx.exe", L"explorerpatcher.exe", L"startallback.exe",
L"translucent-tb.exe", L"rainmeter.exe", L"displayfusion.exe"
};
PROCESSENTRY32W pe{}; pe.dwSize = sizeof(pe);
bool found = false;
if (Process32FirstW(snap, &pe)) {
do {
std::wstring exe = to_lower(pe.szExeFile);
for (auto m : managers) {
if (exe == to_lower(std::wstring(m))) { found = true; break; }
}
if (found) break;
} while (Process32NextW(snap, &pe));
}
CloseHandle(snap);
return found;
}
// --- Get process path of window (для разделения Explorer vs прочие) ---
static std::wstring get_window_process_path(HWND hwnd) {
DWORD pid = 0;
GetWindowThreadProcessId(hwnd, &pid);
if (!pid) return L"";
HANDLE h = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
if (!h) return L"";
wchar_t buf[1024]; DWORD len = 1024;
std::wstring path;
if (QueryFullProcessImageNameW(h, 0, buf, &len)) path.assign(buf, len);
CloseHandle(h);
return path;
}
static void enumerate_taskbars() {
g_explorer_taskbars.clear();
g_third_party_taskbars.clear();
const wchar_t* classes[] = { L"Shell_TrayWnd", L"Shell_SecondaryTrayWnd" };
for (auto cls : classes) {
// Primary через FindWindowW
if (lstrcmpW(cls, L"Shell_TrayWnd") == 0) {
HWND primary = FindWindowW(cls, nullptr);
if (primary) {
std::wstring p = to_lower(get_window_process_path(primary));
if (ends_with_icase(p, L"\\explorer.exe") || ends_with_icase(p, L"/explorer.exe"))
g_explorer_taskbars.push_back(primary);
else
g_third_party_taskbars.push_back(primary);
}
}
// Все окна этого класса
HWND prev = nullptr;
while (true) {
HWND h = FindWindowExW(nullptr, prev, cls, nullptr);
if (!h) break;
prev = h;
if (std::find(g_explorer_taskbars.begin(), g_explorer_taskbars.end(), h) != g_explorer_taskbars.end() ||
std::find(g_third_party_taskbars.begin(), g_third_party_taskbars.end(), h) != g_third_party_taskbars.end())
continue;
std::wstring p = to_lower(get_window_process_path(h));
if (ends_with_icase(p, L"\\explorer.exe") || ends_with_icase(p, L"/explorer.exe"))
g_explorer_taskbars.push_back(h);
else
g_third_party_taskbars.push_back(h);
}
}
}
// --- Work area ---
static void save_work_area() {
RECT r{};
if (SystemParametersInfoW(SPI_GETWORKAREA, 0, &r, 0)) {
g_original_work_area = r;
g_has_original_work_area = true;
}
}
static void broadcast_workarea_change() {
SendMessageW(HWND_BROADCAST, WM_SETTINGCHANGE, (WPARAM)SPI_SETWORKAREA, 0);
}
static void set_fullscreen_work_area() {
RECT full{0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN)};
if (SystemParametersInfoW(SPI_SETWORKAREA, 0, &full, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE)) {
broadcast_workarea_change();
}
}
static void restore_work_area() {
if (!g_has_original_work_area) return;
if (SystemParametersInfoW(SPI_SETWORKAREA, 0, &g_original_work_area, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE)) {
broadcast_workarea_change();
}
}
// --- Taskbar control ---
static void force_hide_hwnd(HWND hwnd) {
ShowWindow(hwnd, SW_HIDE);
SetWindowPos(hwnd, nullptr, -10000, -10000, 1, 1,
SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
}
static void hide_system_taskbars(bool reactivate = true) {
for (HWND hwnd : g_explorer_taskbars) {
if (reactivate) {
for (int i = 0; i < 2; ++i) {
ShowWindow(hwnd, SW_SHOWNOACTIVATE);
Sleep(50);
ShowWindow(hwnd, SW_HIDE);
Sleep(50);
}
} else {
// Без «реактивации»: просто скрываем
ShowWindow(hwnd, SW_HIDE);
}
force_hide_hwnd(hwnd);
}
}
static void show_system_taskbars() {
for (HWND hwnd : g_explorer_taskbars) {
ShowWindow(hwnd, SW_SHOWNOACTIVATE);
}
}
static void close_start_menu() {
keybd_event(VK_ESCAPE, 0, 0, 0);
Sleep(20);
keybd_event(VK_ESCAPE, 0, KEYEVENTF_KEYUP, 0);
}
static HWND get_primary_taskbar_hwnd() {
if (!g_explorer_taskbars.empty()) return g_explorer_taskbars[0];
return FindWindowW(L"Shell_TrayWnd", nullptr);
}
static void set_taskbar_autohide(bool enable) {
APPBARDATA abd{};
abd.cbSize = sizeof(abd);
abd.hWnd = get_primary_taskbar_hwnd();
abd.lParam = enable ? ABS_AUTOHIDE : ABS_ALWAYSONTOP;
SHAppBarMessage(ABM_SETSTATE, &abd);
}
// --- Enforcement thread ---
static void enforcement_worker() {
while (!g_exit_flag.load()) {
if (g_desired_hidden.load() && !g_panel_temp_visible.load()) {
for (HWND hwnd : g_explorer_taskbars) {
if (IsWindowVisible(hwnd)) {
force_hide_hwnd(hwnd);
}
}
}
Sleep(100);
}
}
// --- Entry (GUI subsystem, полностью невидимое приложение) ---
int WINAPI wWinMain(HINSTANCE, HINSTANCE, PWSTR, int) {
g_has_taskbar_managers = detect_taskbar_managers();
enumerate_taskbars();
save_work_area();
g_desired_hidden = true;
g_panel_temp_visible = false;
g_show_deadline_ms = 0;
set_taskbar_autohide(true);
hide_system_taskbars(true);
if (!g_has_taskbar_managers && g_third_party_taskbars.empty()) {
set_fullscreen_work_area();
}
std::thread worker(enforcement_worker);
bool win_was_down = false;
bool alt_combo_latched = false;
// Главный цикл (поллинг клавиш)
while (!g_exit_flag.load()) {
ULONGLONG t = now_ms();
// Alt+` -> выход (по фронту)
bool alt_down = key_down(VK_MENU);
bool backtick_down = key_down(VK_OEM_3);
if (alt_down && backtick_down && !alt_combo_latched) {
break;
}
alt_combo_latched = (alt_down && backtick_down);
// Win -> показать/скрыть
bool win_down = key_down(VK_LWIN) || key_down(VK_RWIN);
if (win_down && !win_was_down) {
if (!g_panel_temp_visible.load()) {
// Показ на 10 сек
g_panel_temp_visible = true;
g_desired_hidden = false;
show_system_taskbars();
if (g_has_original_work_area && (!g_has_taskbar_managers && g_third_party_taskbars.empty())) {
restore_work_area();
}
g_show_deadline_ms = t + 10000ULL;
Sleep(120); // debounce
} else {
// Раннее скрытие без «мига»: без реактивации
close_start_menu();
g_desired_hidden = true;
g_panel_temp_visible = false;
hide_system_taskbars(false); // ключевое изменение — без Show→Hide
if (!g_has_taskbar_managers && g_third_party_taskbars.empty()) {
set_fullscreen_work_area();
}
Sleep(60);
}
}
win_was_down = win_down;
// Автоскрытие по таймауту (с реактивацией — как в оригинале)
if (g_panel_temp_visible.load() && t >= g_show_deadline_ms.load()) {
close_start_menu();
g_desired_hidden = true;
g_panel_temp_visible = false;
hide_system_taskbars(true);
if (!g_has_taskbar_managers && g_third_party_taskbars.empty()) {
set_fullscreen_work_area();
}
}
Sleep(20);
}
// Восстановление и выход
g_exit_flag = true;
if (worker.joinable()) worker.join();
g_desired_hidden = false;
show_system_taskbars();
restore_work_area();
set_taskbar_autohide(false);
return 0;
}