版本比较
辅助摸鱼的一个小玩具——窗口透明器
标题
增加 1 行删除 1 行
11改良版窗口透明器,摸鱼绝佳助手!辅助摸鱼的一个小玩具——窗口透明器正文
增加 195 行删除 91 行
1234556789101011121314101 行未改动全部展开116117118119120120121122123124315 行未改动全部展开440441442443444444445446447448376 行未改动全部展开82582682782882983083183283383483583683783883984084184284384484584623 行未改动87087187287386087487587687786487887988086786888188288388487188588688788887588989089187889289389488189589688389789889990090190288790390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995090595195295395495539 行未改动99599699799895395499910001001100295710031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104112 行未改动10541055105610579899901058105910601061106210633 行未改动1067106810691070100210031071107210731074107510767 行未改动108410851086108710191088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115103110321033103410351116111711181119112011211122112311241125112610401127112811291130104411311132113311341048113511361137105111381139105311401141114211431144114511461147114811491150106011511152115311541155115611571067115811591160116111621072116311641165107511661167116811691077107810791170117111721173117411751176108311771178117911801181118211831089118411851186109210931187118811891190109611911192119311941195119611971103110411981106110711991200110912011202120312041205120612071113111411151208120911171210121112121213121412151216121711251218121912201128122112221130122312241132122512261134122712281136122912301231123212331234123511431144114512361237114711481238123912401150124112421152115312431244124512461247124811551249125011571251125212531254125512561257125812591260116611671168116912611262126312641265126611761177117812671268126912701271127212739 行未改动128312841285128611961287128812891290129112011202120312921293129412951205129612971298129913001301130227 行未改动1330133113321333124113341335133613371338133913401341134213431344125112521345134613471348134913501351135213531354135513561357135813591360136113621264136313641365136613676 行未改动13741375137613771279137813791380138113821284138313841385138613871388138912901390139113921393139421 行未改动14161417141814191320142014211422142314241425132614261427142814291430# 窗口透明器
退役 Oier,并非专业干工程的,做得不够优秀的地方请多指教。
由 C++ 编写,用于 Windows。公布源代码,需要在自己电脑上编译一下。编译须知见后文,使用说明程序里有。由 C++ 编写,用于 Windows。随便写的小玩具,也懒得大费周章上传 Github 什么的了。公布源代码,需要在自己电脑上编译一下。编译须知见后文,使用说明程序里有。
发现 BUG 可找作者私信反馈,最多大概两天会看到。
:::info[源代码]```cpp```cpp line-numbers// 禁用Windows宏冲突#ifdef _WIN32 #ifndef NOMINMAX #define NOMINMAX101 行未改动全部展开 if (GetKeyNameText(lParam, keyName, sizeof(keyName))) { result += keyName; } else { char buffer[32]; sprintf_s(buffer, "0x%02X", mainKey); snprintf(buffer, sizeof(buffer), "0x%02X", mainKey); result += buffer; } } return result;315 行未改动全部展开 return string(mbBuffer); } // 返回虚拟键码 char buffer[32]; sprintf_s(buffer, "Key 0x%02X", vkCode); snprintf(buffer, sizeof(buffer), "Key 0x%02X", vkCode); return string(buffer); } } }376 行未改动全部展开const int COLOR_LIGHTPURPLE = 13;const int COLOR_LIGHTYELLOW = 14;const int COLOR_LIGHTWHITE = 15;
// 宽字符输出函数封装void WriteConStr(const wstring& str) { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); DWORD charsWritten; WriteConsoleW(hConsole, str.c_str(), (DWORD)str.length(), &charsWritten, NULL);}
void WriteConStr(const wchar_t* str) { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); 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; string name; wstring name; // 改为宽字符串 int len;
// 构造函数 Button(int _x, int _y, string _name, int _len, bool isPrint = true): x(_x), y(_y), name(_name), len(_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); cout << name; WriteConStr(name); } }
// 输出内容 // 输出内容 void printText() { SetCursorPosition(x, y); cout << name; WriteConStr(name); }
// 点击监听 // 点击监听 bool isClicked() {// SetCursorPosition(x,y); if (!isMyWinVisible || GetForegroundWindow() != myWindow) { return 0; } 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; }};
// 将UTF-8字符串转换为宽字符串wstring UTF8ToWString(const string& utf8) { if (utf8.empty()) return wstring();
int size_needed = MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), (int)utf8.size(), NULL, 0); wstring wstr(size_needed, 0); MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), (int)utf8.size(), &wstr[0], size_needed); return wstr;}
// 将宽字符串转换为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;}
// 将宽字符串转换为系统本地编码(GBK)字符串string WStringToLocal(const wstring& wstr) { if (wstr.empty()) return string();
int size_needed = WideCharToMultiByte(CP_ACP, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL); string strTo(size_needed, 0); WideCharToMultiByte(CP_ACP, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL); return strTo;}
// IsWindow 检查窗口句柄是否存在// IsWindow 检查窗口句柄是否存在bool isHandleValid(HWND hwnd) { return ::IsWindow(hwnd);}
39 行未改动 // 设置透明度 return SetLayeredWindowAttributes(hwnd, 0, (BYTE)alpha, LWA_ALPHA);}
// 获取标题string GetWindowTitle(HWND hwnd) {// 获取窗口标题(返回宽字符串)wstring GetWindowTitleW(HWND hwnd) { wchar_t windowTitle[1024]; GetWindowTextW(hwnd, windowTitle, 1024); return WStringToUTF8(windowTitle); 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);}
int deltaAlpha = 20;
vector<HWND> hiddenWindow;
12 行未改动 } } }, 1);
// Ctrl+VK_ADD 不透明 listener.RegisterComboKey(VK_ADD, 1, 0, 0, 0, [](const KeyInfo & info) {// 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);
// Ctrl+VK_SUBTRACT 透明 listener.RegisterComboKey(VK_SUBTRACT, 1, 0, 0, 0, [](const KeyInfo & info) {// 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 (SetWindowVisibility(nowH, 0)) { 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);}
int main() {// 设置控制台编码为UTF-8 system("chcp 65001");// 设置标题 system("title 窗口透明器");// 设置长宽 70 列,18 行,最好别改 // 设置控制台输出编码为UTF-8(尽管我们使用WriteConsoleW,但系统命令可能需要)// system("chcp 65001 > nul");
// 设置控制台标题为宽字符 SetConsoleTitleW(L"窗口透明器");
// 设置长宽 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:"; WriteConStr(L"ERROR:"); SetColorAndBackground(COLOR_WHITE, COLOR_BLACK); cerr << e.what() << endl;
// 转换错误信息为宽字符 wstring errorMsg = LocalToWString(e.what()); WriteConStr(errorMsg + L"\n");
system("pause"); exit(0); } catch (...) { system("cls"); SetCursorPosition(0, 0); SetColorAndBackground(COLOR_LIGHTWHITE, COLOR_RED); cerr << "UNKNOWN ERROR"; WriteConStr(L"UNKNOWN ERROR"); system("pause"); exit(0); }
hiddenWindow.clear();
// 隐藏光标 // 隐藏光标 SetCursorVisibility(0);
system("cls");
// IDE 内编译警告: // IDE 内编译警告: SetCursorPosition(0, 0); SetColorAndBackground(COLOR_LIGHTRED, COLOR_BLACK); cout << "编译完成后,请勿通过 IDE 直接运行!\n" << endl; WriteConStr(L"编译完成后,请勿通过 IDE 直接运行!\n\n"); WriteConStr(L"请通过文件夹内双击程序运行!\n\n"); WriteConStr(L"如果对任务管理器等窗口不生效,请尝试用管理员身份运行!\n\n"); SetColorAndBackground(COLOR_LIGHTYELLOW, COLOR_BLACK); cout << "建议不要开启快速编辑模式!" << endl; cout << "否则像下面的按钮可能会无法点击!" << endl; Button knowWarning(0, 5, ">我明白了", 9); WriteConStr(L"建议不要开启快速编辑模式!\n"); WriteConStr(L"否则像下面的按钮可能会无法点击!\n");
Button knowWarning(0, 9, L">我明白了", 9); while (1) { if (knowWarning.isClicked()) { while (knowWarning.isClicked()) {// TODO // TODO } break; } Sleep(3); }
// 主页面 // 主页面 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:"; WriteConStr(L"\nERROR:"); SetColorAndBackground(COLOR_WHITE, COLOR_BLACK); cerr << e.what() << endl;
wstring errorMsg = LocalToWString(e.what()); WriteConStr(errorMsg + L"\n");
system("pause"); exit(0); } catch (...) {// system("cls");// SetCursorPosition(0, 0); cerr << endl; WriteConStr(L"\n"); SetColorAndBackground(COLOR_LIGHTWHITE, COLOR_RED); cerr << "UNKNOWN ERROR" << endl; WriteConStr(L"UNKNOWN ERROR\n"); system("pause"); exit(0); }
system("cls");
SetColorAndBackground(COLOR_LIGHTPURPLE, COLOR_BLACK); cout << "窗口透明 v1.0.1" << endl; WriteConStr(L"窗口透明 v1.2.0\n");
SetColorAndBackground(COLOR_LIGHTGREEN, COLOR_BLACK); Button help(0, 3, ">使用指南", 9); Button help(0, 3, L">程序使用指南", 13); SetColorAndBackground(COLOR_LIGHTGREEN2, COLOR_BLACK); Button showList(0, 5, ">隐藏列表", 9); Button showList(0, 5, L">隐藏窗口列表", 13); SetColorAndBackground(COLOR_LIGHTGREEN3, COLOR_BLACK); Button getRest(0, 7, ">暂停监听", 9); Button getRest(0, 7, L">暂停软件功能", 13); SetColorAndBackground(COLOR_WHITE, COLOR_BLACK); Button leave(0, 9, ">退出程序", 9); Button leave(0, 9, L">退出程序并显示所有隐藏的窗口", 29);
Button goHome(0, 17, ">返回", 5, 0); Button goHome(0, 17, L">返回", 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; WriteConStr(L"欢迎使用本程序!\n\n"); SetColorAndBackground(COLOR_LIGHTBLUE, COLOR_BLACK); cout << "Ctrl+小键盘的加号 可以让最前方的窗口更不透明!\n"; cout << "Ctrl+小键盘的减号 可以让最前方的窗口更透明!\n" << endl; WriteConStr(L"Alt+方向键右 可以让最前方的窗口更不透明!\n"); WriteConStr(L"Alt+方向键左 可以让最前方的窗口更透明!\n\n"); SetColorAndBackground(COLOR_LIGHTGREEN2, COLOR_BLACK); cout << "Ctrl+Alt+空格 可以隐藏或显示本窗口!\n" << endl; WriteConStr(L"Ctrl+Alt+空格 可以隐藏或显示本窗口!\n\n"); SetColorAndBackground(COLOR_LIGHTRED, COLOR_BLACK); cout << "Ctrl+空格 可以隐藏任意窗口!\n"; cout << "隐藏的窗口可以在主页的“隐藏列表”中恢复!\n" << endl; 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); cout << "点击主页“暂停/恢复监听”可以开启或暂停功能!\n" << endl; WriteConStr(L"点击主页\"暂停/恢复软件功能\"可以开启或暂停功能!\n\n"); SetColorAndBackground(COLOR_WHITE, COLOR_GREEN); cout << "上课摸鱼绝佳助手,快来体验吧!!!\n" << endl; WriteConStr(L"适当的摸鱼是为了提高效率 ^_^\n"); SetColorAndBackground(COLOR_WHITE, COLOR_BLACK); goHome.printText(); while (1) { if (goHome.isClicked()) { goto HomeText; } Sleep(5); } } 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); 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); cout << "隐藏的窗口如下,点击显示:" << endl; WriteConStr(L"隐藏的窗口如下,点击显示:\n"); 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 = "[无名窗口]"; wstring n = GetWindowTitleW(hiddenWindow.at(Start + i)); if (n == L"") { n = L"[无名窗口]"; } Button tp(0, i + 1, n, min((int)n.size(), 70), 1); // 确保显示宽度不超过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 // TODO } goto ListInner; } } } Sleep(3); }ListInner: ; }//ListOuter:// ; } 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); cout << "窗口透明 v1.0.1" << endl; WriteConStr(L"窗口透明 v1.2.0\n"); SetColorAndBackground(COLOR_LIGHTGREEN, COLOR_BLACK); help.printText(); SetColorAndBackground(COLOR_LIGHTGREEN2, COLOR_BLACK); showList.printText();6 行未改动 //TODO } if (isListen) { listener.Stop(); getRest.name = ">恢复监听"; getRest.name = L">恢复软件功能"; isListen = 0; } else { if (listener.Start()) { addListener(); getRest.name = ">暂停监听"; getRest.name = L">暂停软件功能"; isListen = 1; } } getRest.printText(); // 更新按钮显示 goto HomeText; } Sleep(2); Sleep(5); } return 0;}```21 行未改动作者鼓励并倡导将此程序用于正当的学习用途。如果因使用者使用不当,由此造成的一切后果作者概不负责。
作者承诺在发布代码前已经尽可能地进行了检查,但本程序仍然不能排除有存在错误与漏洞的可能。如果您发现了错误与漏洞,请尽快与作者私信反馈。如果本程序的错误与漏洞对您的电脑造成了损害,作者不承担任何责任,但会积极与您配合减小损害。
未经作者亲自许可,不得随意修改本程序的任何部分。未经作者亲自许可,不得随意修改本程序的任何部分。如果未经允许随意修改本程序,由此造成的一切后果作者概不负责。
本程序有关按键监听的部分代码由 DeepSeek 编写。
## Update
upd on 2026.1.2 添加暂停监听的功能;修复了“隐藏列表”会显示程序自身的问题;修复了通过本程序隐藏的窗口后来通过其他方式显示,会在列表中残留选项的问题。- 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 下午:全面使用宽字符,尽可能修复了乱码问题。