分类: C/C++
2008-08-05 13:53:23
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 所得到的子窗口,而无法得到真正指向最“深层”的窗口,也就是说如果有两个窗口重叠,就无法得到下面的窗口,这样的情况是经常出现的(如图一所示)。
#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; }通过先找父窗口,然后一直向下直到找到子窗口,得到它的句柄。得到句柄以后,就可以对这个窗口做许多事情了。我在附上的源代码中实现了修改窗口文字的功能(程序画面如图二)。