怎样编写一个类Spy++的搜索窗口程序

本文介绍如何使用RealChildWindowFromPoint获取深层窗口句柄,并提供了一个改进函数实现Spy++工具的效果。通过递归搜索子窗口,可以定位到最底层的窗口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

大家都用过Vc自代的工具Spy++,它可以通过鼠标来捕捉窗口,得到窗口的信息。

在MSDN里面找到了一个相关的API:RealChildWindowFromPoint:
HWND RealChildWindowFromPoint(
  HWND hwndParent,            // handle to window
  POINT ptParentClientCoords  // client coordinates
);
The RealChildWindowFromPoint function retrieves a handle to the child window at the 
specified point. The search is restricted to immediate child windows; grandchildren 
and deeper descendant windows are not searched.  
正如Msdn所说的,RealChildWindowFromPoint函数只能够查找到由 ptParentClientCoords 所得到的子窗口,而无法得到真正指向最“深层”的窗口,也就是说如果有两个窗口重叠,就无法得到下面的窗口

(这两个红框框起来的窗口就重叠了,如果用 RealChildWindowFromPoint 就只能得到综合设置的窗口,而无法的到“不出现登陆提示框”的复选框)。所以你只简单的调用这个函数是无法实现Spy++的功能的。

下面的这个函数就可以取得最“深层”的窗口,实现Spy++的功能。#define ZOU_PROCESS_ERROR(condition) if(!(condition)) goto Exit0; //错误处理宏定义 int CGetWindowInfo::GetRealWindow(HWND *phWnd, POINT ptPoint) { int nResult = false; int nRetCode = false; HWND hWndTop = NULL; HWND hWndChild = NULL; POINT ptCooChild = {0}; LONG lWindowStyle = 0; //先得到ptPoint指向的(子)窗口 hWndTop = ::WindowFromPoint(ptPoint); ZOU_PROCESS_ERROR(hWndTop); ptCooChild = ptPoint; lWindowStyle = GetWindowLong(hWndTop, GWL_STYLE); //通过这个判断找到最上层的父窗口(也就是上面图片中“QQ设置”的主窗口) if( !GetParent(hWndTop) || GetDesktopWindow() == GetParent(hWndTop) || !(lWindowStyle & WS_CHILDWINDOW)) { *phWnd = hWndTop; } else { *phWnd = GetParent(hWndTop); } // 转换相对坐标 ::ScreenToClient(*phWnd, &ptCooChild); //从父窗口一层一层往下查找子窗口,直到找到最底层的子窗口 while (TRUE){ hWndChild = RealChildWindowFromPoint(*phWnd, ptCooChild); if (hWndChild && (hWndChild != *phWnd)) *phWnd = hWndChild; else break; } nResult = true; Exit0: return nResult; }
通过先找父窗口,然后一直向下直到找到子窗口,得到它的句柄。得到句柄以后,就可以对这个窗口做许多事情了。

希望对您有所启发和帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值