首页
社区
课程
招聘
[求助]如何获取父窗口而不是拥有者窗口?
2011-4-9 15:12 5780

[求助]如何获取父窗口而不是拥有者窗口?

2011-4-9 15:12
5780
GetParent函数对于子窗口会获取父窗口句柄,对于顶级窗口会转为获取拥有者窗口。
GetAncestor(hWnd, GA_PARENT),看MSDN说明:
hwnd
[in] Handle to the window whose ancestor is to be retrieved. If this parameter is the desktop window, the function returns NULL.
gaFlags
[in] Specifies the ancestor to be retrieved. This parameter can be one of the following values.
GA_PARENT
Retrieves the parent window. This does not include the owner, as it does with the GetParent function.
根据说明貌似是可以完成任务的,实际上对于一个顶级窗口调用此函数,也会返回一个非空的句柄,调用GetWindowThreadProcessId,可知此句柄是隶属于csrss.exe进程的。
除非自己封装下
HWND GetParentNotOwner(HWND hWnd)
{
      HWND hParent = GetParent(hWnd);
      if ((hParent != NULL) && (GetAncestor(hWnd, GA_PARENT) != hParent))
           hParent = NULL;
      return hParent;
}
请问,除此以外,还有其他直接的方法获取父窗口吗?

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 0
打赏
分享
最新回复 (2)
雪    币: 359
活跃值: (41)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
wzanthony 2011-4-9 15:42
2
0
原来顶级窗口GetAncestor(hWnd, GA_PARENT)返回的是GetDesktopWindow(),那就再多做一步判断好了……
雪    币: 132
活跃值: (214)
能力值: ( LV6,RANK:80 )
在线值:
发帖
回帖
粉丝
yiruirui 1 2011-4-22 00:39
3
0
首先对你第一部分的获取父亲窗口的内容表示赞同,因为GetAncestor的第二个参数如果指定为GA_PARENT,这个返回的句柄是不包括拥有者窗口的,而GetParent会当是顶级窗口的时候返回拥有者窗口,因此你的封装对比是正确的。
接下来你说GetAncestor(hwnd,GA_PARENT)返回的是GetDesktopWindow(),这个有什么依据吗?这个函数返回的是桌面窗口的句柄啊,这能相当?求证。
游客
登录 | 注册 方可回帖
返回