本帖最后由 翼国游者 于 2023-12-16 12:01 编辑
广告拦截 弹窗拦截cpp源码分享用户自定义拦截指定弹窗#include <afxwin.h> #include <iostream> #include <fstream> #include <string> #include <thread> #define RULE_FILE "rules.data" using namespace std;
HWND hWnd; string wndtitle; BOOL showflag = false;
bool Init(); void GetWnd(HWND& wnd, string& title); void WriteRules(string rule); bool IsHit(string title); void FindAd(); void ShowHide(); BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam);
int main() { SetWindowTextA(GetForegroundWindow(),"Panel AD Killer made by Panel www.dslt.tech");
if (!Init()) { return 0; }
HANDLE hThread; hThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)FindAd, NULL, 0, NULL);
// 消息循环 MSG msg = { 0 }; while (GetMessage(&msg, NULL, 0, 0) != 0) { if (msg.wParam == 1) { GetWnd(hWnd, wndtitle); WriteRules(wndtitle); }if (msg.wParam == 2) { ShowHide(); } if (msg.message == WM_CLOSE) {
ShowWindow(hWnd, SW_HIDE); } } }
bool Init() { BOOL flag; if (RegisterHotKey(NULL, 1, MOD_CONTROL | MOD_ALT, 'E')) { cout << "初始化成功1\n"; flag = true; } else { cout << "初始化失败,热键可能被占用1\n"; flag = false; } if (RegisterHotKey(NULL, 2, MOD_CONTROL | MOD_ALT, 'T')) { cout << "初始化成功2\n"; flag = true; } else { cout << "初始化失败,热键可能被占用2\n"; flag = false; } return flag; } void GetWnd(HWND& wnd, string& title) {
POINT pNow = { 0,0 }; if (GetCursorPos(&pNow)) { HWND hwndPointNow = NULL; hwndPointNow = WindowFromPoint(pNow); wnd = hwndPointNow; if (hwndPointNow) { char szWindowTitle[50]; ::GetWindowTextA(hwndPointNow, szWindowTitle, sizeof(szWindowTitle)); title = string(szWindowTitle); //cout << hex << (int)hwndPointNow << endl; cout << szWindowTitle << endl; } else cout << "Error!!" << endl; } } void WriteRules(string rule) { ofstream file(RULE_FILE,ios::binary|ios::app); if (!file.is_open()) { std::cerr << "配置文件无法打开" << std::endl; return; } file << rule << std::endl; file.close(); } bool IsHit(string title) { ifstream inputFile(RULE_FILE,ios::app);
if (!inputFile.is_open()) { std::cerr << "无法打开文件" << std::endl; return false; // 返回错误代码 }
std::string line; while (std::getline(inputFile, line)) { if (line == title) { inputFile.close(); return true; } }
return false; } void FindAd() { do { EnumWindows(EnumWindowsProc, 0); Sleep(1000); } while (true); } void ShowHide() { showflag = !showflag; if (showflag) { ShowWindow(GetForegroundWindow(), SW_HIDE); } else { ShowWindow(GetForegroundWindow(), SW_SHOW); } } BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) { char windowTitle[256];
GetWindowTextA(hwnd, windowTitle, sizeof(windowTitle));
if (string(windowTitle) == "") { return true; }
if (string(windowTitle).find("shadow") != string::npos) { PostMessage(hwnd, WM_CLOSE, 0, 0); }
if (IsHit(string(windowTitle))) { PostMessage(hwnd, WM_CLOSE, 0, 0); }
return TRUE; // 继续列举下一个窗口 }
注:若转载请注明大神论坛来源(本贴地址)与作者信息。
|