Reputation: 556
The MSDN says the following about the GetParent
function:
To obtain the parent window and not the owner, instead of using
GetParent
, useGetAncestor
with theGA_PARENT
flag.
But when calling GetAncestor(hWnd, GA_PARENT);
for a window that doesn't have a parent, it returns the desktop window, while GetParent
returns NULL
.
So what is the correct way of getting a parent (and not the owner), and getting NULL
if there are none?
Of course I could check whether GetAncestor
returns the desktop window, but that seems like a hack to me.
Upvotes: 13
Views: 15655
Reputation: 549
Updated in the year 2020 considering the latest Win32 documentation:
HWND GetRealParent(HWND hWnd)
{
HWND hWndOwner;
// To obtain a window's owner window, instead of using GetParent,
// use GetWindow with the GW_OWNER flag.
if (NULL != (hWndOwner = GetWindow(hWnd, GW_OWNER)))
return hWndOwner;
// Obtain the parent window and not the owner
return GetAncestor(hWnd, GA_PARENT);
}
Upvotes: 7
Reputation: 11
A slightly better version that goes both "routes" and if it can't find a proper parent it will return the window itself(as to avoid null references). Using GetParent instead of GetAncestor worked in my case and returned the window I was after.
public static IntPtr GetRealParent(IntPtr hWnd)
{
IntPtr hParent;
hParent = GetAncestor(hWnd, GetAncestorFlags.GetParent);
if (hParent.ToInt64() == 0 || hParent == GetDesktopWindow())
{
hParent = GetParent(hWnd);
if (hParent.ToInt64() == 0 || hParent == GetDesktopWindow())
{
hParent = hWnd;
}
}
return hParent;
}
Upvotes: 1
Reputation: 556
Here's what I came up with:
//
// Returns the real parent window
// Same as GetParent(), but doesn't return the owner
//
HWND GetRealParent(HWND hWnd)
{
HWND hParent;
hParent = GetAncestor(hWnd, GA_PARENT);
if(!hParent || hParent == GetDesktopWindow())
return NULL;
return hParent;
}
Upvotes: 12