tags:

views:

179

answers:

3

I'd like to obtain the same values via code. However I'd like to obtain the top-most or root windows in the hierarchy

I seem to have got the Root Parent with

HWND rootWinHandle = GetAncestor(activatedWinHandle, GA_PARENT);

However I can't get the owner window correctly. Tried

HWND rootOwnerWinHandle = GetAncestor(activatedWinHandle, GA_ROOTOWNER);

For a particular modeless dialog, Spy++ returns the Main Exe window whereas the above line returns the input i.e. activatedWinHandle. Am I looking at the wrong api ?
I'd like to obtain this without MFC if possible... coz nothing else in my project requires it.

+1  A: 

Try GetParent(). I believe this will return the owner window of a window without the WS_CHILD style, and the parent window of a window with WS_CHILD.

Anthony Johnson
+1  A: 

See the GW_OWNER flag for GetWindow.

The GetParent documentation states:

If the window is a child window, the return value is a handle to the parent window. If the window is a top-level window, the return value is a handle to the owner window.

sean e
Yeah weird winapi. In the end, Parent => GetParent(hWnd) and Owner => GetWindow(hWnd, GW_OWNER) is what worked. GetAncestor with all its flags doesn't return the same values for some reason
Gishu
A: 

Only bit of insight i can add it from Raymond Chen:

Remember that owner and parent are two different things.

Modal dialogs disable their OWNERs. All top-level windows have the desktop as their PARENT.

From: What's so special about the desktop window?

Ian Boyd