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

王朝vc·作者佚名  2006-01-17
窄屏简体版  字體: |||超大  

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

湛江海洋大学 计算机系2000软件5班 曾华山

http://hosen.yeah.net

下载源代码

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

}

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

图二

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航