弹窗拦截cpp源码
本帖最后由 Panel 于 2023-12-14 11:27 编辑## 用户自定义拦截指定弹窗
```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.52pojie.cn");
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;
::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;
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; // 继续列举下一个窗口
}
```
本窗口隐藏以后,如果鼠标切换到其他窗口,快捷键就把其他窗口给隐藏掉。看了源码的确是这样,建议运行后保存自身句柄,快捷键直接对自身句柄进行操作,这样就不会对其他窗口误操作隐藏了。 楼主你好,这段代码有通用性的吗, FruitBaby 发表于 2023-12-14 14:05
楼主你好,这段代码有通用性的吗,
有啊,很多广告拦截思路都这样 Panel 发表于 2023-12-14 14:08
有啊,很多广告拦截思路都这样
我好好研究研究哈,向大佬学习 学习了!! 感谢分享源码,参考下 感谢分享!! 学习了zsbd
感谢分享源码,参考下 感谢分享源码,参考下
页:
[1]
2