Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1220181
  • 博文数量: 950
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 13070
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-04 09:23
文章分类

全部博文(950)

文章存档

2011年(1)

2008年(949)

我的朋友

分类: C/C++

2008-08-04 09:30:44

下载本文示例代码

下载源代码


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

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


图二

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

下载本文示例代码
阅读(243) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~