Spy++的搜索窗口程序

 

使用SendMessageTimeOut WM_GETTEXT来获取窗口内容,SendMessage WM_GETTEXTLENGTH得到窗口内容的长度。

大家都用过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;
}     

    通过先找父窗口,然后一直向下直到找到子窗口,得到它的句柄。得到句柄以后,就可以对这个窗口做许多事情了。我在附上的源代码中实现了修改窗口文字的功能(程序画面如图二)。


图二

希望对您有所启发和帮助。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值