如何制作自己的网站 可放广告,app网站建设教程视频教程,html5做网站优势,亚马逊网站网址是多少介绍
在截图工具中你会发现#xff0c;接触到窗口后会自动圈出目标窗口,个别强大一点的还能进行元素识别可以自动圈出元素#xff0c;那么今天简单分析一下QTc如何获取窗口并圈出当前鼠标下的窗口。 介绍1.如何获取所有窗口2.比较函数3.实现窗口判断 结尾 1.如何获取所有窗口…介绍
在截图工具中你会发现接触到窗口后会自动圈出目标窗口,个别强大一点的还能进行元素识别可以自动圈出元素那么今天简单分析一下QTc如何获取窗口并圈出当前鼠标下的窗口。 介绍1.如何获取所有窗口2.比较函数3.实现窗口判断 结尾 1.如何获取所有窗口
1.我们需要调用windows接口EnumWindowsProc回调函数来获取所有顶级窗口需要包含windows.h,以及pro文件链接win库。
win32 {LIBS -luser32 -ldwmapi # 使用WindowsAPI需要链接库
}// 窗口信息结构体
struct WindowInfo
{HWND hwnd; // 窗口句柄int zOrder; // 窗口的Z序值越小越在顶层RECT rect; // 窗口区域POINT pos; //窗口坐标
};std::vectorWindowInfo windows; //窗口信息列表排序的
// 获取所有顶级窗口的回调函数
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{std::vectorWindowInfo* windows reinterpret_caststd::vectorWindowInfo*(lParam);if (IsWindowVisible(hwnd)) // 如果窗口可见{char title[256];GetWindowTextA(hwnd, title, sizeof(title));// 过滤掉一些不需要的窗口QString str QString::fromLocal8Bit(title);if (str.isEmpty() || str ZOrder || str YMagnifier){return TRUE; // 继续下一个窗口}// 获取窗口区域RECT rect;GetWindowRect(hwnd, rect);int zOrder GetWindowZOrder(hwnd);windows-push_back({hwnd, zOrder, rect});}return TRUE;
}2.比较函数
1.那么我们获取窗口后我们需要知道窗口的层级自然要获取鼠标下最顶层的窗口句柄。
// 比较函数用于排序窗口信息
bool CompareWindowInfo(const WindowInfo a, const WindowInfo b)
{return a.zOrder b.zOrder;
}3.实现窗口判断
1.第一步我们先调用接口
// 枚举所有顶级窗口
EnumWindows(EnumWindowsProc, reinterpret_castLPARAM(windows));// 按照Z序排序窗口信息
std::sort(windows.begin(), windows.end(), CompareWindowInfo);// 获取鼠标当前位置
POINT cursorPos;
GetCursorPos(cursorPos);2.然后比遍历调用PtInRect判断列表的窗口位置是否匹配当前鼠标位置
匹配的话就可以拿坐标宽高来绘制了
// 寻找处于鼠标位置下的最顶层窗口
for (const auto window : windows)
{if (PtInRect(window.rect, cursorPos)){// 窗口左上角坐标int x window.rect.left;int y window.rect.top;// 窗口宽度和高度int width window.rect.right - window.rect.left;int height window.rect.bottom - window.rect.top;// 判断窗口是否位于主显示器上if (!IsWindowOnPrimaryMonitor(window.hwnd)) {x (image.width() x);}expectArea.setX(qAbs(x));expectArea.setY(qAbs(y));expectArea.setWidth(width);expectArea.setHeight(height);refreshScreenShotArea();isSelectWin true;break;}
}
结尾
1.通过以上代码就可以实现获取鼠标当下的窗口如想获取元素后面会更新新的文章。