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;
}
请问,除此以外,还有其他直接的方法获取父窗口吗?