当前位置: 首页 > news >正文

QtC++截图支持窗口获取

介绍

在截图工具中你会发现,接触到窗口后会自动圈出目标窗口,个别强大一点的还能进行元素识别可以自动圈出元素,那么今天简单分析一下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::vector<WindowInfo> windows; //窗口信息列表排序的
// 获取所有顶级窗口的回调函数
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{std::vector<WindowInfo>* windows = reinterpret_cast<std::vector<WindowInfo>*>(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_cast<LPARAM>(&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.通过以上代码就可以实现获取鼠标当下的窗口,如想获取元素,后面会更新新的文章。

http://www.lryc.cn/news/434789.html

相关文章:

  • 料品档案没有配置主供应商信息
  • springboot属性加载优先级和常见命令行属性
  • Math Reference Notes: 因式定理
  • Kubernetes------Service
  • C#的LINQ语句
  • 项目实战系列三: 家居购项目 第三部分
  • 【WPF】Border的使用
  • 机器学习(西瓜书)第 4 章 决策树
  • 8、值、指针、引用作为参数或返回值
  • 向量——通俗地解释
  • 新书宣传:《量子安全:信息保护新纪元》
  • Android Framework(五)WMS-窗口显示流程——窗口布局与绘制显示
  • 【计网】计算机网络基础
  • 秃姐学AI系列之:实战Kaggle比赛:图像分类(CIFAR-10)
  • nginx: [error] invalid PID number ““ in “/run/nginx.pid“
  • Java使用Apache POI向Word文档中填充数据
  • Gitflow基础知识
  • NLP基础及其代码-tokenizer
  • OpenCV结构分析与形状描述符(8)点集凸包计算函数convexHull()的使用
  • 灰光模块,彩光模块-介绍
  • python-新冠病毒
  • 2023年408真题计算机网络篇
  • 分类学习器(Classification Learner App)MATLAB
  • DolphinDB 基准性能测试工具:金融模拟数据生成模块合集
  • BUUCTF—[BJDCTF2020]The mystery of ip
  • leecode100题-双指针-三数之和
  • 计算机毕业设计PySpark+Django考研分数线预测 考研院校推荐系统 考研推荐系统 考研爬虫 考研大数据 Hadoop 大数据毕设 机器学习 深度学习
  • Go语言多态实践以及gin框架c.BindJSON序列化遇到的坑
  • SpringCloud神领物流学习笔记:项目概述(一)
  • RocketMQ异步报错:No route info of this topic