标题
增加 1 行删除 1 行
1改良版窗口透明器,摸鱼绝佳助手!1辅助摸鱼的一个小玩具——窗口透明器
正文
增加 376 行删除 361 行
123456789101112131414 行未改动2930313233343536373839404142434445464768 行未改动11611711811912012112212312421 行未改动1461471481491501511521531541551561571581591601611621631641651661677 行未改动1751761771781791801811821831841851861871881891906 行未改动1971981992002012022032042052069 行未改动2162172182192202212222232242 行未改动22722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148217 行未改动50050150250350450550650750850951011 行未改动5225235245255265275285295305315325335345355365375385393 行未改动5435445455465475485495505515529 行未改动5625635645655665675685695705715725735745755765775785795805815825835845854 行未改动59059159259359459559659759859960060160260360423 行未改动6286296306316326336346356366376386396406416426436446456466476487 行未改动65665765865966066166266366466566666766866967067167267367467512 行未改动68868969069169269369469569669717 行未改动71571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474528 行未改动7747757767777787797807817822 行未改动7857867877887897907917927937947957967977 行未改动80580680780880981081181212 行未改动82582682782882983083183223 行未改动85685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890939 行未改动94995095195295395495595695795895996096196296396496596696796896997097197297397410 行未改动9859869879889899909919929939943 行未改动998999100010011002100310041005100610077 行未改动1015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111829 行未改动11921193119411951196119711981199120012011202120312041205120612071208120927 行未改动123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112922 行未改动12951296129712981299130013011302130313041305130613071308130913101311131213132 行未改动13161317131813191320132113221323132413251326# 窗口透明器退役 Oier,并非专业干工程的,做得不够优秀的地方请多指教。由 C++ 编写,用于 Windows。公布源代码,需要在自己电脑上编译一下。编译须知见后文,使用说明程序里有。发现 BUG 可找作者私信反馈,最多大概两天会看到。:::info[源代码]```cpp// 禁用Windows宏冲突#ifdef _WIN32#ifndef NOMINMAX#define NOMINMAX14 行未改动using namespace std;// 按键事件类型// 按键事件类型enum class KeyEventType {KEY_DOWN,KEY_UP,KEY_DOUBLE_CLICK};// 按键信息结构struct KeyInfo {int vkCode; // 虚拟键码string keyName; // 按键名称bool ctrlPressed; // Ctrl是否按下bool altPressed; // Alt是否按下bool shiftPressed; // Shift是否按下bool winPressed; // Win键是否按下68 行未改动if (GetKeyNameText(lParam, keyName, sizeof(keyName))) {result += keyName;} else {char buffer[32];sprintf_s(buffer, "0x%02X", mainKey);result += buffer;}}return result;21 行未改动private:// 静态实例指针,用于钩子回调访问static GlobalKeyListener* s_instance;// 用于32位/64位兼容的线程ID存储#if defined(_WIN64) || defined(__x86_64__) || defined(__amd64__)DWORD_PTR m_threadId;#elseDWORD m_threadId;#endifHHOOK m_keyboardHook;atomic<bool> m_isRunning;thread m_messageThread;// 按键状态 - 使用GetAsyncKeyState获取实时状态,不在这里存储// 只在钩子回调中使用,确保状态准确// 回调函数映射unordered_map<KeyCombo, CallbackInfo, KeyComboHash> m_keyCallbacks;// 双击检测相关7 行未改动// 需要阻止的组合键(用于快速检查)unordered_set<KeyCombo, KeyComboHash> m_blockedCombos;// 保护共享资源的互斥锁 - 使用mutable使其在const函数中可修改mutable recursive_mutex m_mutex;// 私有构造函数,使用单例模式GlobalKeyListener() : m_keyboardHook(nullptr), m_isRunning(false) {s_instance = this;}// 键盘钩子处理函数(必须是静态的)static LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {if (nCode >= 0 && s_instance) {KBDLLHOOKSTRUCT* kbStruct = reinterpret_cast<KBDLLHOOKSTRUCT*>(lParam);6 行未改动bool winPressed = KEY_DOWN(VK_LWIN) || KEY_DOWN(VK_RWIN);// 检查是否需要阻止此按键bool shouldBlock = false;// 只有按下事件才检查是否需要阻止if (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) {shouldBlock = s_instance->ShouldBlockKey(vkCode, ctrlPressed, altPressed, shiftPressed, winPressed);}9 行未改动s_instance->PostDoubleClickCheck(vkCode, ctrlPressed, altPressed, shiftPressed, winPressed);}}// 如果需要阻止,返回1表示已处理,不让系统继续处理if (shouldBlock) {return 1;}}2 行未改动// 检查是否需要阻止此按键bool ShouldBlockKey(int vkCode, bool ctrlPressed, bool altPressed, bool shiftPressed, bool winPressed) {lock_guard<recursive_mutex> lock(m_mutex);// 检查是否有需要阻止的组合键for (const auto& combo : m_blockedCombos) {if (combo.matches(vkCode, ctrlPressed, altPressed, shiftPressed, winPressed)) {return true;}}return false;}// 发送按键事件到消息线程void PostKeyEvent(int vkCode, KeyEventType eventType, bool ctrlPressed, bool altPressed, bool shiftPressed, bool winPressed, bool isHandled) {if (!m_isRunning || !m_threadId) return;// 打包数据LPARAM lParam = MAKELPARAM((ctrlPressed ? 1 : 0) |((altPressed ? 1 : 0) << 1) |((shiftPressed ? 1 : 0) << 2) |((winPressed ? 1 : 0) << 3) |((isHandled ? 1 : 0) << 4),vkCode);WPARAM wParam = static_cast<WPARAM>(eventType);PostThreadMessage(m_threadId, WM_USER + 1, wParam, lParam);}// 发送双击检查到消息线程void PostDoubleClickCheck(int vkCode, bool ctrlPressed, bool altPressed, bool shiftPressed, bool winPressed) {if (!m_isRunning || !m_threadId) return;// 打包修饰键状态DWORD modifiers = (ctrlPressed ? 1 : 0) |((altPressed ? 1 : 0) << 1) |((shiftPressed ? 1 : 0) << 2) |((winPressed ? 1 : 0) << 3);LPARAM lParam = MAKELPARAM(modifiers, vkCode);PostThreadMessage(m_threadId, WM_USER + 2, vkCode, lParam);}// 获取按键名称string GetKeyName(int vkCode) const {char keyName[256] = {0};UINT scanCode = MapVirtualKey(vkCode, MAPVK_VK_TO_VSC);// 处理扩展键switch (vkCode) {case VK_INSERT:case VK_DELETE:case VK_HOME:case VK_END:case VK_PRIOR:case VK_NEXT:case VK_LEFT:case VK_RIGHT:case VK_UP:case VK_DOWN:case VK_NUMLOCK:case VK_CANCEL:case VK_SNAPSHOT:case VK_DIVIDE:scanCode |= 0x0100; // 扩展键标志break;}LONG lParam = (scanCode << 16);if (GetKeyNameText(lParam, keyName, sizeof(keyName)) > 0) {return string(keyName);}// 特殊键处理switch (vkCode) {case VK_CONTROL:return "Ctrl";case VK_LCONTROL:return "Left Ctrl";case VK_RCONTROL:return "Right Ctrl";case VK_MENU:return "Alt";case VK_LMENU:return "Left Alt";case VK_RMENU:return "Right Alt";case VK_SHIFT:return "Shift";case VK_LSHIFT:return "Left Shift";case VK_RSHIFT:return "Right Shift";case VK_LWIN:return "Left Win";case VK_RWIN:return "Right Win";case VK_ESCAPE:return "Esc";case VK_RETURN:return "Enter";case VK_SPACE:return "Space";case VK_BACK:return "Backspace";case VK_TAB:return "Tab";case VK_CAPITAL:return "Caps Lock";case VK_NUMLOCK:return "Num Lock";case VK_SCROLL:return "Scroll Lock";case VK_INSERT:return "Insert";case VK_DELETE:return "Delete";case VK_HOME:return "Home";case VK_END:return "End";case VK_PRIOR:return "Page Up";case VK_NEXT:return "Page Down";case VK_UP:return "Up Arrow";case VK_DOWN:return "Down Arrow";case VK_LEFT:return "Left Arrow";case VK_RIGHT:return "Right Arrow";case VK_F1:return "F1";case VK_F2:return "F2";case VK_F3:return "F3";case VK_F4:return "F4";case VK_F5:return "F5";case VK_F6:return "F6";case VK_F7:return "F7";case VK_F8:return "F8";case VK_F9:return "F9";case VK_F10:return "F10";case VK_F11:return "F11";case VK_F12:return "F12";case VK_OEM_PLUS:return "Plus";case VK_OEM_MINUS:return "Minus";case VK_OEM_COMMA:return "Comma";case VK_OEM_PERIOD:return "Period";case VK_ADD:return "NumPad +";case VK_SUBTRACT:return "NumPad -";case VK_MULTIPLY:return "NumPad *";case VK_DIVIDE:return "NumPad /";case VK_DECIMAL:return "NumPad .";case VK_NUMPAD0:return "NumPad 0";case VK_NUMPAD1:return "NumPad 1";case VK_NUMPAD2:return "NumPad 2";case VK_NUMPAD3:return "NumPad 3";case VK_NUMPAD4:return "NumPad 4";case VK_NUMPAD5:return "NumPad 5";case VK_NUMPAD6:return "NumPad 6";case VK_NUMPAD7:return "NumPad 7";case VK_NUMPAD8:return "NumPad 8";case VK_NUMPAD9:return "NumPad 9";default: {// 尝试获取字符表示BYTE keyboardState[256] = {0};GetKeyboardState(keyboardState);WCHAR charBuffer[10] = {0};int result = ToUnicode(vkCode, scanCode, keyboardState, charBuffer, 10, 0);if (result > 0) {// 转换为多字节字符串char mbBuffer[10] = {0};WideCharToMultiByte(CP_UTF8, 0, charBuffer, -1, mbBuffer, sizeof(mbBuffer), nullptr, nullptr);return string(mbBuffer);}// 返回虚拟键码char buffer[32];sprintf_s(buffer, "Key 0x%02X", vkCode);return string(buffer);}}}// 处理按键事件(在消息线程中执行)void ProcessKeyEvent(WPARAM wParam, LPARAM lParam) {KeyEventType eventType = static_cast<KeyEventType>(wParam);int vkCode = HIWORD(lParam);WORD modifiers = LOWORD(lParam);bool ctrlPressed = (modifiers & 0x01) != 0;bool altPressed = (modifiers & 0x02) != 0;bool shiftPressed = (modifiers & 0x04) != 0;bool winPressed = (modifiers & 0x08) != 0;bool isHandled = (modifiers & 0x10) != 0;KeyInfo keyInfo;keyInfo.vkCode = vkCode;keyInfo.keyName = GetKeyName(vkCode);keyInfo.ctrlPressed = ctrlPressed;keyInfo.altPressed = altPressed;keyInfo.shiftPressed = shiftPressed;keyInfo.winPressed = winPressed;keyInfo.eventType = eventType;keyInfo.timestamp = GetTickCount();keyInfo.isHandled = isHandled;lock_guard<recursive_mutex> lock(m_mutex);// 查找匹配的回调函数vector<const CallbackInfo*> matchedCallbacks;for (const auto& pair : m_keyCallbacks) {const KeyCombo& combo = pair.first;if (combo.matches(vkCode, ctrlPressed, altPressed, shiftPressed, winPressed)) {matchedCallbacks.push_back(&pair.second);17 行未改动int vkCode = static_cast<int>(wParam);WORD modifiers = LOWORD(lParam);bool ctrlPressed = (modifiers & 0x01) != 0;bool altPressed = (modifiers & 0x02) != 0;bool shiftPressed = (modifiers & 0x04) != 0;bool winPressed = (modifiers & 0x08) != 0;DWORD currentTime = GetTickCount();lock_guard<recursive_mutex> lock(m_mutex);11 行未改动pressCount++;if (pressCount >= 2) {// 触发双击事件KeyInfo keyInfo;keyInfo.vkCode = vkCode;keyInfo.keyName = GetKeyName(vkCode);keyInfo.ctrlPressed = ctrlPressed;keyInfo.altPressed = altPressed;keyInfo.shiftPressed = shiftPressed;keyInfo.winPressed = winPressed;keyInfo.eventType = KeyEventType::KEY_DOUBLE_CLICK;keyInfo.timestamp = currentTime;keyInfo.isHandled = false;// 执行双击回调auto callbackIt = m_doubleClickCallbacks.find(vkCode);if (callbackIt != m_doubleClickCallbacks.end() && callbackIt->second.callback) {3 行未改动// 防止回调异常}}// 重置计数pressCount = 0;}it->second.lastPressTime = currentTime;it->second.pressCount = pressCount;9 行未改动}// 消息循环线程函数void MessageLoop() {// 存储线程ID用于跨平台兼容#if defined(_WIN64) || defined(__x86_64__) || defined(__amd64__)m_threadId = (DWORD_PTR)GetCurrentThreadId();#elsem_threadId = GetCurrentThreadId();#endif// 安装全局键盘钩子m_keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, GetModuleHandle(nullptr), 0);if (!m_keyboardHook) {DWORD error = GetLastError();system("cls");cerr << "Failed to set keyboard hook. Error: " << error << endl;// 尝试以管理员权限运行提示if (error == ERROR_ACCESS_DENIED) {cerr << "Try running the program as administrator." << endl;}return;4 行未改动// 消息循环MSG msg;while (m_isRunning && GetMessage(&msg, nullptr, 0, 0)) {// 处理自定义消息if (msg.message == WM_USER + 1) {ProcessKeyEvent(msg.wParam, msg.lParam);} else if (msg.message == WM_USER + 2) {ProcessDoubleClick(msg.wParam, msg.lParam);}TranslateMessage(&msg);DispatchMessage(&msg);}23 行未改动if (m_isRunning) return true;m_isRunning = true;// 清空历史状态{lock_guard<recursive_mutex> lock(m_mutex);m_keyPressHistory.clear();}// 创建消息循环线程try {m_messageThread = thread(&GlobalKeyListener::MessageLoop, this);// 等待钩子设置完成this_thread::sleep_for(chrono::milliseconds(100));return m_keyboardHook != nullptr;} catch (const exception& e) {system("cls");cerr << "Failed to start listener: " << e.what() << endl;7 行未改动if (!m_isRunning) return;m_isRunning = false;// 发送退出消息if (m_threadId) {PostThreadMessage(m_threadId, WM_QUIT, 0, 0);}// 等待线程结束if (m_messageThread.joinable()) {try {m_messageThread.join();} catch (...) {// 忽略异常}}lock_guard<recursive_mutex> lock(m_mutex);m_keyCallbacks.clear();12 行未改动CallbackInfo info{callback, blockSystemBehavior};lock_guard<recursive_mutex> lock(m_mutex);m_keyCallbacks[combo] = info;// 如果需要阻止系统行为,添加到阻止列表if (blockSystemBehavior) {m_blockedCombos.insert(combo);}}17 行未改动// 直接阻止组合键(无需回调)void BlockCombo(int mainKey, bool ctrl = false, bool alt = false,bool shift = false, bool win = false) {KeyCombo combo{mainKey, ctrl, alt, shift, win};lock_guard<recursive_mutex> lock(m_mutex);m_blockedCombos.insert(combo);}// 解除阻止组合键void UnblockCombo(int mainKey, bool ctrl = false, bool alt = false,bool shift = false, bool win = false) {KeyCombo combo{mainKey, ctrl, alt, shift, win};lock_guard<recursive_mutex> lock(m_mutex);m_blockedCombos.erase(combo);}// 移除按键监听void UnregisterKey(int vkCode, bool ctrl = false,bool alt = false, bool shift = false, bool win = false) {KeyCombo combo{vkCode, ctrl, alt, shift, win};lock_guard<recursive_mutex> lock(m_mutex);m_keyCallbacks.erase(combo);// 同时从阻止列表中移除m_blockedCombos.erase(combo);}// 移除所有监听28 行未改动// 获取已注册的快捷键列表vector<string> GetRegisteredHotkeys() const {vector<string> hotkeys;lock_guard<recursive_mutex> lock(m_mutex);for (const auto& pair : m_keyCallbacks) {hotkeys.push_back(pair.first.toString());}return hotkeys;2 行未改动// 获取已阻止的快捷键列表vector<string> GetBlockedHotkeys() const {vector<string> hotkeys;lock_guard<recursive_mutex> lock(m_mutex);for (const auto& combo : m_blockedCombos) {hotkeys.push_back(combo.toString());}return hotkeys;}// 析构函数7 行未改动GlobalKeyListener* GlobalKeyListener::s_instance = nullptr;//本程序的窗口句柄HWND myWindow;bool isMyWinVisible = true;//颜色常量const int COLOR_BLACK = 0;12 行未改动const int COLOR_LIGHTPURPLE = 13;const int COLOR_LIGHTYELLOW = 14;const int COLOR_LIGHTWHITE = 15;//更改颜色 文字+背景void SetColorAndBackground(int ForgC, int BackC) {WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);23 行未改动CONSOLE_CURSOR_INFO cursor_info = {1, x}; // 第二个值为 0 表示隐藏光标SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);}//按钮class Button {public:int x, y;string name;int len;// 构造函数Button(int _x, int _y, string _name, int _len, bool isPrint = true): x(_x), y(_y), name(_name), len(_len) {if (isPrint) {SetCursorPosition(x, y);cout << name;}}// 输出内容void printText() {SetCursorPosition(x, y);cout << name;}// 点击监听bool isClicked() {// SetCursorPosition(x,y);POINT pt;GetCursorPosition(pt);if (KEY_DOWN(MOUSE_MOVED)) {// cout << pt.x << " " << pt.y << endl;if (pt.y == y && (pt.x >= x && pt.x <= x + len)) {return 1;}}return 0;}};string WStringToUTF8(const wstring& wstr) {if (wstr.empty()) return string();int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);string strTo(size_needed, 0);WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);return strTo;}// IsWindow 检查窗口句柄是否存在bool isHandleValid(HWND hwnd) {return ::IsWindow(hwnd);}39 行未改动// 设置透明度return SetLayeredWindowAttributes(hwnd, 0, (BYTE)alpha, LWA_ALPHA);}// 获取标题string GetWindowTitle(HWND hwnd) {wchar_t windowTitle[1024];GetWindowTextW(hwnd, windowTitle, 1024);return WStringToUTF8(windowTitle);}//查询指定窗口是否隐藏bool isWindowHidden(HWND hwnd) {if (hwnd == NULL) {return 0;}LONG style = GetWindowLong(hwnd, GWL_STYLE);return (style & WS_VISIBLE) == 0;}int deltaAlpha = 20;vector<HWND> hiddenWindow;bool isListen = 0;10 行未改动}}}, 1);// Ctrl+VK_ADD 不透明listener.RegisterComboKey(VK_ADD, 1, 0, 0, 0, [](const KeyInfo & info) {if (info.eventType == KeyEventType::KEY_UP) {HWND nowH = GetForegroundWindow();int nowA = GetWindowTransparency(nowH);if (nowA > 255 - deltaAlpha) {3 行未改动}}}, 1);// Ctrl+VK_SUBTRACT 透明listener.RegisterComboKey(VK_SUBTRACT, 1, 0, 0, 0, [](const KeyInfo & info) {if (info.eventType == KeyEventType::KEY_UP) {HWND nowH = GetForegroundWindow();int nowA = GetWindowTransparency(nowH);if (nowA < 2 * deltaAlpha) {7 行未改动// Ctrl+Space 隐藏listener.RegisterComboKey(VK_SPACE, 1, 0, 0, 0, [](const KeyInfo & info) {if (info.eventType == KeyEventType::KEY_UP) {HWND nowH = GetForegroundWindow();if (SetWindowVisibility(nowH, 0)) {if (nowH == myWindow) {isMyWinVisible = false;} else {hiddenWindow.push_back(nowH);}}}}, 1);}int main() {// 设置控制台编码为UTF-8system("chcp 65001");// 设置标题system("title 窗口透明器");// 设置长宽 70 列,18 行,最好别改system("mode con cols=70 lines=18");system("cls");// 尝试获取本程序的窗口句柄try {myWindow = GetConsoleWindow();if (myWindow == NULL) {// 无法获取,抛异常throw runtime_error("无法获取窗口句柄");}} catch (const runtime_error& e) {// 报错,退出程序SetCursorPosition(0, 0);SetColorAndBackground(COLOR_LIGHTWHITE, COLOR_RED);cerr << "ERROR:";SetColorAndBackground(COLOR_WHITE, COLOR_BLACK);cerr << e.what() << endl;system("pause");exit(0);} catch (...) {system("cls");SetCursorPosition(0, 0);SetColorAndBackground(COLOR_LIGHTWHITE, COLOR_RED);cerr << "UNKNOWN ERROR";system("pause");exit(0);}hiddenWindow.clear();// 隐藏光标SetCursorVisibility(0);system("cls");// IDE 内编译警告:SetCursorPosition(0, 0);SetColorAndBackground(COLOR_LIGHTRED, COLOR_BLACK);cout << "编译完成后,请勿通过 IDE 直接运行!\n" << endl;SetColorAndBackground(COLOR_LIGHTYELLOW, COLOR_BLACK);cout << "建议不要开启快速编辑模式!" << endl;cout << "否则像下面的按钮可能会无法点击!" << endl;Button knowWarning(0, 5, ">我明白了", 9);while (1) {if (knowWarning.isClicked()) {while (knowWarning.isClicked()) {// TODO}break;}}// 主页面system("cls");// 启动监听addListener();try {// 失败:if (!listener.Start()) {throw runtime_error("无法监听按键");} else {isListen = 1;}} catch (const runtime_error& e) {// system("cls");// SetCursorPosition(0, 0);SetColorAndBackground(COLOR_LIGHTWHITE, COLOR_RED);cerr << endl;cerr << "ERROR:";SetColorAndBackground(COLOR_WHITE, COLOR_BLACK);cerr << e.what() << endl;system("pause");exit(0);} catch (...) {// system("cls");// SetCursorPosition(0, 0);cerr << endl;SetColorAndBackground(COLOR_LIGHTWHITE, COLOR_RED);cerr << "UNKNOWN ERROR" << endl;system("pause");exit(0);}system("cls");SetColorAndBackground(COLOR_LIGHTPURPLE, COLOR_BLACK);cout << "窗口透明 v1.0.1" << endl;SetColorAndBackground(COLOR_LIGHTGREEN, COLOR_BLACK);Button help(0, 3, ">使用指南", 9);SetColorAndBackground(COLOR_LIGHTGREEN2, COLOR_BLACK);Button showList(0, 5, ">隐藏列表", 9);SetColorAndBackground(COLOR_LIGHTGREEN3, COLOR_BLACK);Button getRest(0, 7, ">暂停监听", 9);SetColorAndBackground(COLOR_WHITE, COLOR_BLACK);Button leave(0, 9, ">退出程序", 9);Button goHome(0, 17, ">返回", 5, 0);while (1) {if (help.isClicked()) {system("cls");SetCursorPosition(0, 0);SetColorAndBackground(COLOR_PURPLE, COLOR_BLACK);cout << "欢迎使用本程序!\n" << endl;SetColorAndBackground(COLOR_LIGHTYELLOW, COLOR_BLACK);cout << "透明度 0 表示完全透明,255 表示完全不透明\n" << endl;SetColorAndBackground(COLOR_LIGHTBLUE, COLOR_BLACK);cout << "Ctrl+小键盘的加号 可以让最前方的窗口更不透明!\n";cout << "Ctrl+小键盘的减号 可以让最前方的窗口更透明!\n" << endl;SetColorAndBackground(COLOR_LIGHTGREEN2, COLOR_BLACK);cout << "Ctrl+Alt+空格 可以隐藏或显示本窗口!\n" << endl;SetColorAndBackground(COLOR_LIGHTRED, COLOR_BLACK);cout << "Ctrl+空格 可以隐藏任意窗口!\n";cout << "隐藏的窗口可以在主页的“隐藏列表”中恢复!\n" << endl;SetColorAndBackground(COLOR_LIGHTPURPLE, COLOR_BLACK);cout << "点击主页“暂停/恢复监听”可以开启或暂停功能!\n" << endl;SetColorAndBackground(COLOR_WHITE, COLOR_GREEN);cout << "上课摸鱼绝佳助手,快来体验吧!!!\n" << endl;SetColorAndBackground(COLOR_WHITE, COLOR_BLACK);goHome.printText();while (1) {if (goHome.isClicked()) {goto HomeText;}}} else if (showList.isClicked()) {hiddenWindow.erase(remove_if(hiddenWindow.begin(), hiddenWindow.end(), [](const HWND & nowH) {return !isHandleValid(nowH);}), hiddenWindow.end());while (showList.isClicked()) {//TODO}int Start = 0;vector<Button> Items;Button Front(0, 16, ">上一页", 7, 0);Button Next(60, 16, ">下一页", 7, 0);Button Renew(62, 17, ">刷新", 5, 0);while (1) {hiddenWindow.erase(remove_if(hiddenWindow.begin(), hiddenWindow.end(), [](const HWND & nowH) {return !isHandleValid(nowH) || !isWindowHidden(nowH);9 行未改动system("cls");SetCursorPosition(0, 0);SetColorAndBackground(COLOR_YELLOW, COLOR_BLACK);cout << "隐藏的窗口如下,点击显示:" << endl;SetColorAndBackground(COLOR_WHITE, COLOR_BLACK);Items.clear();for (int i = 0; i < min(15, (int)hiddenWindow.size() - Start); i++) {string n = GetWindowTitle(hiddenWindow.at(Start + i));if (n == "") {n = "[无名窗口]";}Button tp(0, i + 1, n, min((int)n.size(), 70), 1);Items.push_back(tp);}SetColorAndBackground(COLOR_LIGHTBLUE, COLOR_BLACK);27 行未改动SetWindowVisibility(tp, 1);}hiddenWindow.erase(remove(hiddenWindow.begin(), hiddenWindow.end(), tp), hiddenWindow.end());while (Items[i].isClicked()) {// TODO}goto ListInner;}}}}ListInner:;}//ListOuter:// ;} else if (leave.isClicked()) {listener.Stop();exit(0);} else if (goHome.isClicked()) {while (goHome.isClicked()) {//TODO}HomeText:;system("cls");SetColorAndBackground(COLOR_LIGHTPURPLE, COLOR_BLACK);cout << "窗口透明 v1.0.1" << endl;SetColorAndBackground(COLOR_LIGHTGREEN, COLOR_BLACK);help.printText();SetColorAndBackground(COLOR_LIGHTGREEN2, COLOR_BLACK);showList.printText();SetColorAndBackground(COLOR_LIGHTGREEN3, COLOR_BLACK);getRest.printText();SetColorAndBackground(COLOR_WHITE, COLOR_BLACK);leave.printText();} else if (getRest.isClicked()) {while (getRest.isClicked()) {//TODO}if (isListen) {listener.Stop();getRest.name = ">恢复监听";isListen = 0;} else {if (listener.Start()) {addListener();getRest.name = ">暂停监听";isListen = 1;}}goto HomeText;}Sleep(2);}return 0;2 行未改动:::# 编译须知至少需要 C++11。也就是说,请你在“编译时加入下列选项”中添加:```-std=c++11```如果希望可执行文件在不同电脑上都可以运行,请在“链接时加入下列选项”中添加:```-static-libgcc-static-libstdc++```# 免责 & 后记2 行未改动作者鼓励并倡导将此程序用于正当的学习用途。如果因使用者使用不当,由此造成的一切后果作者概不负责。作者承诺在发布代码前已经尽可能地进行了检查,但本程序仍然不能排除有存在错误与漏洞的可能。如果您发现了错误与漏洞,请尽快与作者私信反馈。如果本程序的错误与漏洞对您的电脑造成了损害,作者不承担任何责任,但会积极与您配合减小损害。未经作者亲自许可,不得随意修改本程序的任何部分。本程序有关按键监听的部分代码由 DeepSeek 编写。## Updateupd on 2026.1.2 添加暂停监听的功能;修复了“隐藏列表”会显示程序自身的问题;修复了通过本程序隐藏的窗口后来通过其他方式显示,会在列表中残留选项的问题。123456789101112131415161718192021222324252627282914 行未改动444546474849505152535455565758596068 行未改动12913013113213313413513613721 行未改动1591601611621631641651661671681691701717 行未改动1791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162176 行未改动2242252262272282292302319 行未改动2412422432442452462472482492 行未改动25225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431517 行未改动33333433533633733833934034134234311 行未改动3553563573583593603613623633643653 行未改动3693703713723733743753763779 行未改动3873883893903913923933943953963973983994004014024034 行未改动40840941041141241341441541641741841942023 行未改动4444454464474484494504514524534544554564574584597 行未改动46746846947047147247347447547647747847948048148212 行未改动49549649749849950050150217 行未改动52052152252352452552652752852953053153253353453553653753853954054154254354454528 行未改动5745755765775785795805812 行未改动5845855865875885895905915925935947 行未改动60260360460560660760860961061161212 行未改动62562662762862963063163263363463563663763863964064164264364423 行未改动66866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273339 行未改动77377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684710 行未改动8588598608618628638648658668673 行未改动8718728738748758768778788798807 行未改动88888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011419 行未改动1151115211531154115511561157115811591160116111621163116411651166116711681169117027 行未改动1198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612872 行未改动12901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613172 行未改动1320132113221323132413251326132713281329133013311332133313341335133613371338133913401341# 窗口透明器退役 Oier,并非专业干工程的,做得不够优秀的地方请多指教。由 C++ 编写,用于 Windows。随便写的小玩具,也懒得大费周章上传 Github 什么的了。公布源代码,需要在自己电脑上编译一下。编译须知见后文,**请务必阅读**!使用说明程序里有。**作者还提供了编译好的可执行文件**,如果编译报错可以选择直接下载可执行文件。如遇杀毒软件误报请信任。:::info[编译好的程序]最新版本的可执行文件:<https://www.luogu.com.cn/problem/U675500>。---历史版本源代码:通过网盘分享的文件:Transparent Window链接: <https://pan.baidu.com/s/1D8NBlL00h_1eWqPJTGPCVg?pwd=AKAK> 提取码: AKAK:::发现 BUG 可找作者私信反馈,最多大概两天会看到。:::info[源代码]```cpp line-numbers// 禁用Windows宏冲突#ifdef _WIN32#ifndef NOMINMAX#define NOMINMAX14 行未改动using namespace std;// 按键事件类型enum class KeyEventType {KEY_DOWN,KEY_UP,KEY_DOUBLE_CLICK};// 按键信息结构struct KeyInfo {int vkCode; // 虚拟键码bool ctrlPressed; // Ctrl是否按下bool altPressed; // Alt是否按下bool shiftPressed; // Shift是否按下bool winPressed; // Win键是否按下68 行未改动if (GetKeyNameText(lParam, keyName, sizeof(keyName))) {result += keyName;} else {char buffer[32];snprintf(buffer, sizeof(buffer), "0x%02X", mainKey);result += buffer;}}return result;21 行未改动private:// 静态实例指针,用于钩子回调访问static GlobalKeyListener* s_instance;DWORD m_threadId; // 消息线程IDHHOOK m_keyboardHook;atomic<bool> m_isRunning;thread m_messageThread;// 回调函数映射unordered_map<KeyCombo, CallbackInfo, KeyComboHash> m_keyCallbacks;// 双击检测相关7 行未改动// 需要阻止的组合键(用于快速检查)unordered_set<KeyCombo, KeyComboHash> m_blockedCombos;// 保护共享资源的互斥锁mutable recursive_mutex m_mutex;// 私有构造函数,使用单例模式GlobalKeyListener() : m_keyboardHook(nullptr), m_isRunning(false) {s_instance = this;}// 辅助函数:向消息线程投递消息void PostMessageToThread(UINT msg, WPARAM wParam, LPARAM lParam) {if (m_isRunning && m_threadId) {PostThreadMessage(m_threadId, msg, wParam, lParam);}}// 辅助函数:构建KeyInfo对象KeyInfo MakeKeyInfo(int vkCode, KeyEventType eventType,bool ctrl, bool alt, bool shift, bool win,bool isHandled) const {KeyInfo info;info.vkCode = vkCode;info.ctrlPressed = ctrl;info.altPressed = alt;info.shiftPressed = shift;info.winPressed = win;info.eventType = eventType;info.timestamp = GetTickCount();info.isHandled = isHandled;return info;}// 键盘钩子处理函数(必须是静态的)static LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {if (nCode >= 0 && s_instance) {KBDLLHOOKSTRUCT* kbStruct = reinterpret_cast<KBDLLHOOKSTRUCT*>(lParam);6 行未改动bool winPressed = KEY_DOWN(VK_LWIN) || KEY_DOWN(VK_RWIN);// 检查是否需要阻止此按键bool shouldBlock = false;if (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) {shouldBlock = s_instance->ShouldBlockKey(vkCode, ctrlPressed, altPressed, shiftPressed, winPressed);}9 行未改动s_instance->PostDoubleClickCheck(vkCode, ctrlPressed, altPressed, shiftPressed, winPressed);}}// 如果需要阻止,返回1表示已处理if (shouldBlock) {return 1;}}2 行未改动// 检查是否需要阻止此按键bool ShouldBlockKey(int vkCode, bool ctrlPressed, bool altPressed, bool shiftPressed, bool winPressed) {lock_guard<recursive_mutex> lock(m_mutex);for (const auto& combo : m_blockedCombos) {if (combo.matches(vkCode, ctrlPressed, altPressed, shiftPressed, winPressed)) {return true;}}return false;}// 发送按键事件到消息线程void PostKeyEvent(int vkCode, KeyEventType eventType, bool ctrlPressed, bool altPressed,bool shiftPressed, bool winPressed, bool isHandled) {// 打包数据LPARAM lParam = MAKELPARAM((ctrlPressed ? 1 : 0) |((altPressed ? 1 : 0) << 1) |((shiftPressed ? 1 : 0) << 2) |((winPressed ? 1 : 0) << 3) |((isHandled ? 1 : 0) << 4),vkCode);WPARAM wParam = static_cast<WPARAM>(eventType);PostMessageToThread(WM_USER + 1, wParam, lParam);}// 发送双击检查到消息线程void PostDoubleClickCheck(int vkCode, bool ctrlPressed, bool altPressed,bool shiftPressed, bool winPressed) {// 打包修饰键状态DWORD modifiers = (ctrlPressed ? 1 : 0) |((altPressed ? 1 : 0) << 1) |((shiftPressed ? 1 : 0) << 2) |((winPressed ? 1 : 0) << 3);LPARAM lParam = MAKELPARAM(modifiers, vkCode);PostMessageToThread(WM_USER + 2, vkCode, lParam);}// 处理按键事件(在消息线程中执行)void ProcessKeyEvent(WPARAM wParam, LPARAM lParam) {KeyEventType eventType = static_cast<KeyEventType>(wParam);int vkCode = HIWORD(lParam);WORD modifiers = LOWORD(lParam);bool ctrlPressed = (modifiers & 0x01) != 0;bool altPressed = (modifiers & 0x02) != 0;bool shiftPressed = (modifiers & 0x04) != 0;bool winPressed = (modifiers & 0x08) != 0;bool isHandled = (modifiers & 0x10) != 0;KeyInfo keyInfo = MakeKeyInfo(vkCode, eventType,ctrlPressed, altPressed, shiftPressed, winPressed,isHandled);lock_guard<recursive_mutex> lock(m_mutex);// 查找匹配的回调函数vector<const CallbackInfo*> matchedCallbacks;for (const auto& pair : m_keyCallbacks) {const KeyCombo& combo = pair.first;if (combo.matches(vkCode, ctrlPressed, altPressed, shiftPressed, winPressed)) {matchedCallbacks.push_back(&pair.second);17 行未改动int vkCode = static_cast<int>(wParam);WORD modifiers = LOWORD(lParam);bool ctrlPressed = (modifiers & 0x01) != 0;bool altPressed = (modifiers & 0x02) != 0;bool shiftPressed = (modifiers & 0x04) != 0;bool winPressed = (modifiers & 0x08) != 0;DWORD currentTime = GetTickCount();lock_guard<recursive_mutex> lock(m_mutex);11 行未改动pressCount++;if (pressCount >= 2) {// 触发双击事件KeyInfo keyInfo = MakeKeyInfo(vkCode, KeyEventType::KEY_DOUBLE_CLICK,ctrlPressed, altPressed, shiftPressed, winPressed,false);// 执行双击回调auto callbackIt = m_doubleClickCallbacks.find(vkCode);if (callbackIt != m_doubleClickCallbacks.end() && callbackIt->second.callback) {3 行未改动// 防止回调异常}}pressCount = 0; // 重置计数}it->second.lastPressTime = currentTime;it->second.pressCount = pressCount;9 行未改动}// 消息循环线程函数void MessageLoop() {m_threadId = GetCurrentThreadId();// 安装全局键盘钩子m_keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, GetModuleHandle(nullptr), 0);if (!m_keyboardHook) {DWORD error = GetLastError();system("cls");cerr << "Failed to set keyboard hook. Error: " << error << endl;if (error == ERROR_ACCESS_DENIED) {cerr << "Try running the program as administrator." << endl;}return;4 行未改动// 消息循环MSG msg;while (m_isRunning && GetMessage(&msg, nullptr, 0, 0)) {if (msg.message == WM_USER + 1) {ProcessKeyEvent(msg.wParam, msg.lParam);} else if (msg.message == WM_USER + 2) {ProcessDoubleClick(msg.wParam, msg.lParam);}TranslateMessage(&msg);DispatchMessage(&msg);}23 行未改动if (m_isRunning) return true;m_isRunning = true;{lock_guard<recursive_mutex> lock(m_mutex);m_keyPressHistory.clear();}try {m_messageThread = thread(&GlobalKeyListener::MessageLoop, this);this_thread::sleep_for(chrono::milliseconds(100));return m_keyboardHook != nullptr;} catch (const exception& e) {system("cls");cerr << "Failed to start listener: " << e.what() << endl;7 行未改动if (!m_isRunning) return;m_isRunning = false;if (m_threadId) {PostThreadMessage(m_threadId, WM_QUIT, 0, 0);}if (m_messageThread.joinable()) {try {m_messageThread.join();} catch (...) {}}lock_guard<recursive_mutex> lock(m_mutex);m_keyCallbacks.clear();12 行未改动CallbackInfo info{callback, blockSystemBehavior};lock_guard<recursive_mutex> lock(m_mutex);m_keyCallbacks[combo] = info;if (blockSystemBehavior) {m_blockedCombos.insert(combo);}}17 行未改动// 直接阻止组合键(无需回调)void BlockCombo(int mainKey, bool ctrl = false, bool alt = false,bool shift = false, bool win = false) {KeyCombo combo{mainKey, ctrl, alt, shift, win};lock_guard<recursive_mutex> lock(m_mutex);m_blockedCombos.insert(combo);}// 解除阻止组合键void UnblockCombo(int mainKey, bool ctrl = false, bool alt = false,bool shift = false, bool win = false) {KeyCombo combo{mainKey, ctrl, alt, shift, win};lock_guard<recursive_mutex> lock(m_mutex);m_blockedCombos.erase(combo);}// 移除按键监听void UnregisterKey(int vkCode, bool ctrl = false,bool alt = false, bool shift = false, bool win = false) {KeyCombo combo{vkCode, ctrl, alt, shift, win};lock_guard<recursive_mutex> lock(m_mutex);m_keyCallbacks.erase(combo);m_blockedCombos.erase(combo);}// 移除所有监听28 行未改动// 获取已注册的快捷键列表vector<string> GetRegisteredHotkeys() const {vector<string> hotkeys;lock_guard<recursive_mutex> lock(m_mutex);for (const auto& pair : m_keyCallbacks) {hotkeys.push_back(pair.first.toString());}return hotkeys;2 行未改动// 获取已阻止的快捷键列表vector<string> GetBlockedHotkeys() const {vector<string> hotkeys;lock_guard<recursive_mutex> lock(m_mutex);for (const auto& combo : m_blockedCombos) {hotkeys.push_back(combo.toString());}return hotkeys;}// 析构函数7 行未改动GlobalKeyListener* GlobalKeyListener::s_instance = nullptr;//本程序的窗口句柄HWND myWindow;//本程序的标准输出句柄HANDLE hConsole;bool isMyWinVisible = true;//颜色常量const int COLOR_BLACK = 0;12 行未改动const int COLOR_LIGHTPURPLE = 13;const int COLOR_LIGHTYELLOW = 14;const int COLOR_LIGHTWHITE = 15;// 宽字符输出函数封装void WriteConStr(const wstring& str) {DWORD charsWritten;WriteConsoleW(hConsole, str.c_str(), (DWORD)str.length(), &charsWritten, NULL);}void WriteConStr(const wchar_t* str) {DWORD charsWritten;WriteConsoleW(hConsole, str, (DWORD)wcslen(str), &charsWritten, NULL);}//更改颜色 文字+背景void SetColorAndBackground(int ForgC, int BackC) {WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);23 行未改动CONSOLE_CURSOR_INFO cursor_info = {1, x}; // 第二个值为 0 表示隐藏光标SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);}//按钮类(使用宽字符)class Button {public:int x, y;wstring name; // 改为宽字符串int len;// 构造函数Button(int _x, int _y, const wstring& _name, int _len, bool isPrint = true): x(_x), y(_y), name(_name), len(_len) {if (isPrint) {SetCursorPosition(x, y);WriteConStr(name);}}// 输出内容void printText() {SetCursorPosition(x, y);WriteConStr(name);}// 点击监听bool isClicked() {if (!isMyWinVisible || GetForegroundWindow() != myWindow) {return 0;}POINT pt;GetCursorPosition(pt);if (KEY_DOWN(MOUSE_MOVED)) {if (pt.y == y && (pt.x >= x && pt.x <= x + len)) {return 1;}}return 0;}};// 将宽字符串转换为UTF-8字符串string WStringToUTF8(const wstring& wstr) {if (wstr.empty()) return string();int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);string strTo(size_needed, 0);WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);return strTo;}// 将系统本地编码(GBK)字符串转换为宽字符串wstring LocalToWString(const string& local) {if (local.empty()) return wstring();int size_needed = MultiByteToWideChar(CP_ACP, 0, local.c_str(), (int)local.size(), NULL, 0);wstring wstr(size_needed, 0);MultiByteToWideChar(CP_ACP, 0, local.c_str(), (int)local.size(), &wstr[0], size_needed);return wstr;}// IsWindow 检查窗口句柄是否存在bool isHandleValid(HWND hwnd) {return ::IsWindow(hwnd);}39 行未改动// 设置透明度return SetLayeredWindowAttributes(hwnd, 0, (BYTE)alpha, LWA_ALPHA);}// 获取窗口标题(返回宽字符串)wstring GetWindowTitleW(HWND hwnd) {wchar_t windowTitle[1024];GetWindowTextW(hwnd, windowTitle, 1024);wstring res = windowTitle;if ((int)res.size() > 33) {res = res.substr(0, 33);res += L"...";}return res;}// 获取窗口标题(兼容旧代码,返回UTF-8字符串)string GetWindowTitle(HWND hwnd) {wstring wtitle = GetWindowTitleW(hwnd);return WStringToUTF8(wtitle);}//查询指定窗口是否隐藏bool isWindowHidden(HWND hwnd) {if (hwnd == NULL) {return 0;}LONG style = GetWindowLong(hwnd, GWL_STYLE);return (style & WS_VISIBLE) == 0;}//查询指定窗口是否置顶bool IsWindowTopmost(HWND hwnd) {LONG_PTR exStyle = GetWindowLongPtr(hwnd, GWL_EXSTYLE);return (exStyle & WS_EX_TOPMOST) != 0;}//设置指定窗口是否置顶void SetWindowTopmost(HWND hwnd, bool topmost) {HWND insertAfter = topmost ? HWND_TOPMOST : HWND_NOTOPMOST;SetWindowPos(hwnd, insertAfter, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);}//关闭快速编辑模式bool noEdit() {// 获取标准输入句柄HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE);if (hInput == INVALID_HANDLE_VALUE) {return false;}DWORD mode;// 获取当前控制台模式if (!GetConsoleMode(hInput, &mode)) {return false;}mode &= ~ENABLE_QUICK_EDIT_MODE; // 清除快速编辑mode |= ENABLE_EXTENDED_FLAGS; // 启用扩展标志// mode &= ~ENABLE_INSERT_MODE;// 应用新模式if (!SetConsoleMode(hInput, mode)) {return false;}return true;}const int deltaAlpha = 20;vector<HWND> hiddenWindow;bool isListen = 0;10 行未改动}}}, 1);// Alt+VK_RIGHT 不透明listener.RegisterComboKey(VK_RIGHT, 0, 1, 0, 0, [](const KeyInfo & info) {if (info.eventType == KeyEventType::KEY_UP) {HWND nowH = GetForegroundWindow();int nowA = GetWindowTransparency(nowH);if (nowA > 255 - deltaAlpha) {3 行未改动}}}, 1);// Alt+VK_LEFT 透明listener.RegisterComboKey(VK_LEFT, 0, 1, 0, 0, [](const KeyInfo & info) {if (info.eventType == KeyEventType::KEY_UP) {HWND nowH = GetForegroundWindow();int nowA = GetWindowTransparency(nowH);if (nowA < 2 * deltaAlpha) {7 行未改动// Ctrl+Space 隐藏listener.RegisterComboKey(VK_SPACE, 1, 0, 0, 0, [](const KeyInfo & info) {if (info.eventType == KeyEventType::KEY_UP) {HWND nowH = GetForegroundWindow();if (!isWindowHidden(nowH) && SetWindowVisibility(nowH, 0)) {if (nowH == myWindow) {isMyWinVisible = false;} else {hiddenWindow.push_back(nowH);}}}}, 1);// Alt+VK_UP 置顶listener.RegisterComboKey(VK_UP, 0, 1, 0, 0, [](const KeyInfo & info) {if (info.eventType == KeyEventType::KEY_UP) {HWND nowH = GetForegroundWindow();SetWindowTopmost(nowH, 1);}}, 1);// Alt+VK_DOWN 不置顶listener.RegisterComboKey(VK_DOWN, 0, 1, 0, 0, [](const KeyInfo & info) {if (info.eventType == KeyEventType::KEY_UP) {HWND nowH = GetForegroundWindow();SetWindowTopmost(nowH, 0);}}, 1);}//退出程序时显示所有隐藏的窗口BOOL WINAPI ConsoleHandler(DWORD dwCtrlType) {switch (dwCtrlType) {case CTRL_C_EVENT:case CTRL_BREAK_EVENT:case CTRL_CLOSE_EVENT:listener.Stop();hiddenWindow.erase(remove_if(hiddenWindow.begin(), hiddenWindow.end(), [](const HWND & nowH) {return !isHandleValid(nowH);}), hiddenWindow.end());for (auto i : hiddenWindow) {SetWindowVisibility(i, 1);Sleep(2);}exit(0);return TRUE;default:return FALSE;}}int main() {// 设置控制台输出编码为UTF-8(尽管我们使用WriteConsoleW,但系统命令可能需要)// system("chcp 65001 > nul");// 设置控制台标题为宽字符SetConsoleTitleW(L"窗口透明器");// 设置长宽 70 列,18 行,最好别改system("mode con cols=70 lines=18");system("cls");// 尝试获取本程序的窗口句柄try {myWindow = GetConsoleWindow();hConsole = GetStdHandle(STD_OUTPUT_HANDLE);if (myWindow == NULL || hConsole == NULL) {// 无法获取,抛异常throw runtime_error("无法获取窗口句柄或输出句柄");}} catch (const runtime_error& e) {// 报错,退出程序SetCursorPosition(0, 0);SetColorAndBackground(COLOR_LIGHTWHITE, COLOR_RED);WriteConStr(L"ERROR:");SetColorAndBackground(COLOR_WHITE, COLOR_BLACK);// 转换错误信息为宽字符wstring errorMsg = LocalToWString(e.what());WriteConStr(errorMsg + L"\n");system("pause");exit(0);} catch (...) {system("cls");SetCursorPosition(0, 0);SetColorAndBackground(COLOR_LIGHTWHITE, COLOR_RED);WriteConStr(L"UNKNOWN ERROR");system("pause");exit(0);}hiddenWindow.clear();// 隐藏光标SetCursorVisibility(0);Button knowWarning(0, 13, L">我明白了", 9, 0);system("cls");SetCursorPosition(0, 0);// 尝试关闭快速编辑模式if (!noEdit()) {SetColorAndBackground(COLOR_LIGHTRED, COLOR_BLACK);WriteConStr(L"关闭快速编辑模式失败!\n");WriteConStr(L"请手动关闭快速编辑模式\n");WriteConStr(L"否则像下面的按钮可能会无法点击!\n\n");SetColorAndBackground(COLOR_WHITE, COLOR_BLACK);knowWarning.printText();while (1) {if (knowWarning.isClicked()) {while (knowWarning.isClicked()) {// TODO}break;}Sleep(5);}}system("cls");SetCursorPosition(0, 0);SetColorAndBackground(COLOR_LIGHTYELLOW, COLOR_BLACK);WriteConStr(L"如果对任务管理器等窗口不生效,请尝试用管理员身份运行!\n\n");SetColorAndBackground(COLOR_RED, COLOR_LIGHTYELLOW);WriteConStr(L"请不要通过 taskkill /f 关闭程序\n");WriteConStr(L"否则隐藏的窗口将再也无法显示!\n");SetColorAndBackground(COLOR_WHITE, COLOR_BLACK);knowWarning.printText();while (1) {if (knowWarning.isClicked()) {while (knowWarning.isClicked()) {// TODO}break;}Sleep(5);}system("cls");SetCursorPosition(0, 0);// 处理强制退出(显示所有隐藏窗口并退出)if (!SetConsoleCtrlHandler(ConsoleHandler, TRUE)) {// 失败SetColorAndBackground(COLOR_LIGHTYELLOW, COLOR_BLACK);WriteConStr(L"控制台事件处理函数注册失败!\n");WriteConStr(L"通过其他方式退出程序会使得已隐藏的窗口无法显形!\n");WriteConStr(L"但您仍可以通过点击主页面的“退出程序”按钮退出程序,并显示所有隐藏的窗口!\n");knowWarning.printText();while (1) {if (knowWarning.isClicked()) {while (knowWarning.isClicked()) {// TODO}break;}Sleep(5);}}// 主页面system("cls");// 启动监听addListener();try {// 失败:if (!listener.Start()) {throw runtime_error("无法监听按键");} else {isListen = 1;}} catch (const runtime_error& e) {SetColorAndBackground(COLOR_LIGHTWHITE, COLOR_RED);WriteConStr(L"\nERROR:");SetColorAndBackground(COLOR_WHITE, COLOR_BLACK);wstring errorMsg = LocalToWString(e.what());WriteConStr(errorMsg + L"\n");system("pause");exit(0);} catch (...) {WriteConStr(L"\n");SetColorAndBackground(COLOR_LIGHTWHITE, COLOR_RED);WriteConStr(L"UNKNOWN ERROR\n");system("pause");exit(0);}system("cls");SetColorAndBackground(COLOR_LIGHTPURPLE, COLOR_BLACK);WriteConStr(L"窗口透明 v1.2.4\n");SetColorAndBackground(COLOR_LIGHTGREEN, COLOR_BLACK);Button help(0, 3, L">程序功能介绍", 13);SetColorAndBackground(COLOR_LIGHTGREEN2, COLOR_BLACK);Button showList(0, 5, L">隐藏窗口列表", 13);SetColorAndBackground(COLOR_LIGHTGREEN3, COLOR_BLACK);Button getRest(0, 7, L">暂停软件功能", 13);SetColorAndBackground(COLOR_YELLOW, COLOR_BLACK);Button moreInfo(0, 9, L">更多详细信息", 13);SetColorAndBackground(COLOR_WHITE, COLOR_BLACK);Button leave(0, 11, L">退出程序", 9);Button goHome(0, 17, L">返回", 5, 0);while (1) {if (help.isClicked()) {system("cls");SetCursorPosition(0, 0);SetColorAndBackground(COLOR_PURPLE, COLOR_BLACK);WriteConStr(L"欢迎使用本程序!\n\n");SetColorAndBackground(COLOR_LIGHTBLUE, COLOR_BLACK);WriteConStr(L"Alt+方向键右 可以让最前方的窗口更不透明!\n");WriteConStr(L"Alt+方向键左 可以让最前方的窗口更透明!\n\n");SetColorAndBackground(COLOR_LIGHTGREEN2, COLOR_BLACK);WriteConStr(L"Ctrl+Alt+空格 可以隐藏或显示本窗口!\n\n");SetColorAndBackground(COLOR_LIGHTRED, COLOR_BLACK);WriteConStr(L"Ctrl+空格 可以隐藏任意窗口!\n");WriteConStr(L"隐藏的窗口可以在主页的\"隐藏窗口列表\"中恢复!\n\n");SetColorAndBackground(COLOR_LIGHTWHITE, COLOR_BLACK);WriteConStr(L"Alt+方向键上 可以置顶任意窗口!\n");WriteConStr(L"Alt+方向键下 可以取消置顶任意窗口!\n\n");SetColorAndBackground(COLOR_LIGHTPURPLE, COLOR_BLACK);WriteConStr(L"点击主页\"暂停/恢复软件功能\"可以开启或暂停功能!\n\n");SetColorAndBackground(COLOR_WHITE, COLOR_BLACK);goHome.printText();while (1) {if (goHome.isClicked()) {goto HomeText;}Sleep(2);}} else if (showList.isClicked()) {while (showList.isClicked()) {//TODO}int Start = 0;vector<Button> Items;Button Front(0, 16, L">上一页", 7, 0);Button Next(60, 16, L">下一页", 7, 0);Button Renew(62, 17, L">刷新", 5, 0);while (1) {hiddenWindow.erase(remove_if(hiddenWindow.begin(), hiddenWindow.end(), [](const HWND & nowH) {return !isHandleValid(nowH) || !isWindowHidden(nowH);9 行未改动system("cls");SetCursorPosition(0, 0);SetColorAndBackground(COLOR_YELLOW, COLOR_BLACK);WriteConStr(L"隐藏的窗口如下,点击显示:\n");SetColorAndBackground(COLOR_WHITE, COLOR_BLACK);Items.clear();for (int i = 0; i < min(15, (int)hiddenWindow.size() - Start); i++) {wstring n = GetWindowTitleW(hiddenWindow.at(Start + i));if (n == L"") {n = L"[无名窗口]";}// 确保显示宽度不超过70个字符// 注意:中文和英文字符宽度不同,这里简化为字符数Button tp(0, i + 1, n, 70, 1);Items.push_back(tp);}SetColorAndBackground(COLOR_LIGHTBLUE, COLOR_BLACK);27 行未改动SetWindowVisibility(tp, 1);}hiddenWindow.erase(remove(hiddenWindow.begin(), hiddenWindow.end(), tp), hiddenWindow.end());while (Items[i].isClicked()) {// TODO}goto ListInner;}}}Sleep(2);}ListInner:;}} else if (leave.isClicked()) {listener.Stop();hiddenWindow.erase(remove_if(hiddenWindow.begin(), hiddenWindow.end(), [](const HWND & nowH) {return !isHandleValid(nowH);}), hiddenWindow.end());for (auto i : hiddenWindow) {SetWindowVisibility(i, 1);Sleep(2);}exit(0);} else if (goHome.isClicked()) {while (goHome.isClicked()) {//TODO}HomeText:;system("cls");SetColorAndBackground(COLOR_LIGHTPURPLE, COLOR_BLACK);WriteConStr(L"窗口透明 v1.2.4\n");SetColorAndBackground(COLOR_LIGHTGREEN, COLOR_BLACK);help.printText();SetColorAndBackground(COLOR_LIGHTGREEN2, COLOR_BLACK);showList.printText();SetColorAndBackground(COLOR_LIGHTGREEN3, COLOR_BLACK);getRest.printText();SetColorAndBackground(COLOR_YELLOW, COLOR_BLACK);moreInfo.printText();SetColorAndBackground(COLOR_WHITE, COLOR_BLACK);leave.printText();} else if (getRest.isClicked()) {while (getRest.isClicked()) {//TODO}if (isListen) {listener.Stop();getRest.name = L">恢复软件功能";isListen = 0;} else {if (listener.Start()) {addListener();getRest.name = L">暂停软件功能";isListen = 1;}}getRest.printText(); // 更新按钮显示goto HomeText;}else if(moreInfo.isClicked()){system("cls");SetCursorPosition(0, 0);SetColorAndBackground(COLOR_WHITE, COLOR_BLUE);WriteConStr(L"作者:ny_Dacong 洛谷 uid:928972\n");WriteConStr(L"版本更新、更多说明请参见洛谷专栏!\n");WriteConStr(L"https://www.luogu.com.cn/article/giobg5om\n\n");SetColorAndBackground(COLOR_LIGHTRED, COLOR_LIGHTYELLOW);WriteConStr(L"常见问题:\n");SetColorAndBackground(COLOR_WHITE, COLOR_BLACK);WriteConStr(L"Q:自己编译的程序,结果字符乱码\n");WriteConStr(L"A:请修改 IDE 的字符集为 UTF-8。实在不会,可去专栏下载编译好的程序\n\n");WriteConStr(L"Q:按钮点不动/一点就卡 A:请关闭快速编辑模式\n\n");WriteConStr(L"Q:退出程序之后之前隐藏的窗口找不到了\n");WriteConStr(L"A:找不回来了。下次退出的时候请点击主页的“退出程序”按钮\n\n");SetColorAndBackground(COLOR_WHITE, COLOR_GREEN);WriteConStr(L"适当的摸鱼是为了提高效率 ^_^\n");SetColorAndBackground(COLOR_WHITE, COLOR_BLACK);goHome.printText();while (1) {if (goHome.isClicked()) {goto HomeText;}Sleep(2);}}Sleep(2);}return 0;2 行未改动:::# 编译须知至少需要 C++14。同时,代码大量使用宽字符,确保你的 IDE 保存代码时用的是 UTF-8 格式,编译时也是用的 UTF-8。也就是说,请你在“编译时加入下列选项”中添加:```-std=c++14 -finput-charset=UTF-8```:::error[注意]有一条与上面很像的编译命令,为:```-fexec-charset=UTF-8```除非你知道在干什么,否则不要添加。:::如果希望可执行文件在不同电脑上都可以运行,请在“链接时加入下列选项”中添加:```-static```# 免责 & 后记2 行未改动作者鼓励并倡导将此程序用于正当的学习用途。如果因使用者使用不当,由此造成的一切后果作者概不负责。作者承诺在发布代码前已经尽可能地进行了检查,但本程序仍然不能排除有存在错误与漏洞的可能。如果您发现了错误与漏洞,请尽快与作者私信反馈。如果本程序的错误与漏洞对您的电脑造成了损害,作者不承担任何责任,但会积极与您配合减小损害。未经作者亲自许可,不得随意修改本程序的任何部分。如果未经允许随意修改本程序,由此造成的一切后果作者概不负责。本程序有关按键监听的部分代码由 DeepSeek 编写。## Update- upd on 2026.1.2 上午:添加暂停监听的功能;修复了“隐藏列表”会显示程序自身的问题;修复了通过本程序隐藏的窗口后来通过其他方式显示,会在列表中残留选项的问题。- upd on 2026.1.2 下午:修复名称过长的窗口在隐藏列表中的显示问题;修复重复按 Ctrl+Space 会重复向隐藏列表中添加同一窗口的问题。- upd on 2026.1.2 晚上:添加置顶窗口的功能;添加按钮检测的等待时间,减少 CPU 开销;添加“管理员身份”的启动提示;修复即使程序窗口不在最上层按钮也可被点击的问题;更改了调整透明度的快捷键。- upd on 2026.1.10 晚上:使用 `snprintf` 代替 `sprintf_s`,增强兼容性;更改首页按钮文案;退出按钮添加使所有隐藏的窗口显形的功能,防止关闭程序后窗口再也无法显形的情况。- upd on 2026.1.17 下午:全面使用宽字符,尽可能修复了乱码问题;缩减了监听的等待时间,提高灵敏度。- upd on 2026.1.18 晚上:更改了编译须知。- upd on 2026.2.14 下午:提供了编译好的程序。- upd on 2026.2.23 中午:修复了通过其他方式关闭程序时,隐藏的窗口无法显示的问题。现在即使通过点击关闭窗口的按钮、按 Alt+F4 等手段关闭程序,也可以使隐藏的窗口显形;修改了启动提示;优化了键盘监听的代码,减少了代码长度;添加了对 Ctrl+C 的组合键监听,并尝试阻止其关闭程序的行为。- upd on 2026.2.23 下午:紧急修复了对 Ctrl+C 的监听会阻止包括复制在内的其它系统默认行为。- upd on 2026.2.23 晚上:修复了通过 Ctrl+C 关闭程序会让已隐藏的窗口再也无法显示的问题。如果该问题仍然出现,请确保您是通过文件夹打开的本程序,并尝试以管理员身份运行。- upd on 2026.4.5 下午:添加了通过洛谷~~网盘~~下载程序的方法。- upd on 2026.11.7 晚上:启动时尝试自动禁用快速编辑模式,感谢 @[clever_coder](luogu://user/1061905) 同学;添加更多详细信息页面,并进行了署名;