views:

89

answers:

1

In my native windows mobile app I've got a window that creates a dialog. Lets say my window handle is hMainWnd.

I create the dialog using DialogBoxParam() and passing in hMainWnd as the dialog's parent:

DialogBoxParam(_,_,hMainWnd,_,_);

Let's say the dialog's handle is hDlgWnd. From within the dialog, GetParent() returns hMainWnd as expected:

//We're inside the dialog created above
HWND hParent = GetParent(hDlgWnd); //hParent == hMainWnd

Here's the odd thing, calling GetWindow() to find the children of hMainWnd returns NULL, signifying that it has no children. I would expect the function to return hDlgWnd

//We're inside the main window    
HWND hChild = GetWindow(hMainWnd, GW_CHILD); //hChild == NULL

How can a child know its parent when the parent doesn't know its child?

+3  A: 

GetWindow with GW_CHILD apparently does not retrieve descendant windows, only child windows. From MSDN:

The retrieved handle identifies the child window at the top of the Z order, if the specified window is a parent window; otherwise, the retrieved handle is NULL. The function examines only child windows of the specified window. It does not examine descendant windows

What is the difference between a child and a descendant? I don't know but EnumChildWindows might get you what you need.

Nick Whaley
Sorry, WS_CHILD was a typo. I've fixed it and attempted to clarify the question. How'd I do?
Gordon Wilson
+1 from me. EnumChildWindows() is what you want.
Andreas Magnusson