none
MFC child parent relation

    Question

  • Hi all

     

    Why is it that from a CDialog (dialog A), I can use GetParent() to get its parent (dialog B).

    But when I use EnumChildWindows to enumerate the children of dialog B, I can't get back dialog A!

     

    How can I ever get dialog A from dialog B??

     

    The same goes for when dialog B is repalced by a CMainFrame window...

    Thursday, October 04, 2007 8:09 AM

Answers

  • Thats because the Child-Parent relation of a MFC Dialog object is not same as Win32's Child Window relation, MFC CDialog/CWnd class has a pointer to parent as a member variable, which is not the same with Win32 Window. And in my opinion EnumChildWindows do not consider top level window(separate dialog which can have focus and position in taskbar) as a child window.

     

    Thursday, October 04, 2007 12:28 PM
  •  

    Few Point as I think.

     

    First Thing GetChild() and GetParent() method works in case of top Level Window as Well as Controls Window  .And with the help of EnumChildWindows you can get all those window which host on a Specified Window .

     

    SO use EnumChildWindows whenever you want to get handle of all the controls present on a Window or you can say The EnumChildWindows function enumerates the child windows that belong to the specified parent window. and GetParent() traverse Between Top level Window and yes it also works with controls.in case of GetParent() .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

     

     

    Thanx

    Friday, October 05, 2007 5:33 AM

All replies

  • Thats because the Child-Parent relation of a MFC Dialog object is not same as Win32's Child Window relation, MFC CDialog/CWnd class has a pointer to parent as a member variable, which is not the same with Win32 Window. And in my opinion EnumChildWindows do not consider top level window(separate dialog which can have focus and position in taskbar) as a child window.

     

    Thursday, October 04, 2007 12:28 PM
  •  

    Few Point as I think.

     

    First Thing GetChild() and GetParent() method works in case of top Level Window as Well as Controls Window  .And with the help of EnumChildWindows you can get all those window which host on a Specified Window .

     

    SO use EnumChildWindows whenever you want to get handle of all the controls present on a Window or you can say The EnumChildWindows function enumerates the child windows that belong to the specified parent window. and GetParent() traverse Between Top level Window and yes it also works with controls.in case of GetParent() .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

     

     

    Thanx

    Friday, October 05, 2007 5:33 AM
  • Read Window Features and Win32 Window Hierarchy and Styles.

     

    EnumChildWindows only enumerates windows with the WS_CHILD style. Does Dialog A have that style?

     

    The documentation of windows hierarchies and styles is not as clear as it could be and the use of windows hierarchies and styles by Windows is more complicated than we expect. One thing that is not clear in the documentation is that a window can be either owned or a child but never both. Yet Windows sometimes use either a window's owner or it's parent, which can be confusing.

    Friday, October 05, 2007 5:46 PM
  •  Ramkrishna Pawar wrote:
    Thats because the Child-Parent relation of a MFC Dialog object is not same as Win32's Child Window relation, MFC CDialog/CWnd class has a pointer to parent as a member variable, which is not the same with Win32 Window. And in my opinion EnumChildWindows do not consider top level window(separate dialog which can have focus and position in taskbar) as a child window.

     

    A MFC dialog is a Windows (Win32 in Win32 Windows) dialog with all the features, hierachies and styles of a Windows dialog. A dialog object that is an instance of a MFC CDialog will be derived and can be derived from, which is a relationhip that the Windows object won't have, but that is not refered to as a parent/child relationship.

    Friday, October 05, 2007 5:53 PM
  •  Pintu Shukla wrote:
    If the window is a top-level window, the return value is a handle to the owner window

    Only if it is a pop-up window.

    Friday, October 05, 2007 5:58 PM
  • Ofcourse

     Simple Samples wrote:

    Yet Windows sometimes use either a window's owner or it's parent, which can be confusing.

    According to MSDN

    Despite its name ( GetParent()), this function can return an owner window instead of a parent window. To obtain the parent window and not the owner, use GetAncestor with the GA_PARENT flag.

     

    Thanx

    Saturday, October 06, 2007 10:47 AM
  •  Pintu Shukla wrote:

    Ofcourse

     Simple Samples wrote:

    Yet Windows sometimes use either a window's owner or it's parent, which can be confusing.

    According to MSDN

    Despite its name ( GetParent()), this function can return an owner window instead of a parent window. To obtain the parent window and not the owner, use GetAncestor with the GA_PARENT flag.

     

    Thanx

    I think it is best for each developer to read the documentation themselves. Yes, GetParent sometimes returns handles for owner windows, but only when the owner window is a pop-up.

     

    The original question asked why the code does not "get back dialog A". Assuming dialog A is not a child of dialog B, GetAncestor won't get dialog B when used for dialog A. Right? So how does GetAncestor help?

    Saturday, October 06, 2007 8:46 PM
  • Simple Samples this was the answer of your Question .regarding OP i think i had already given him the solution.

    Thanx

    Sunday, October 07, 2007 9:08 AM
  • Thats because the Child-Parent relation of a MFC Dialog object is not same as Win32's Child Window relation, MFC CDialog/CWnd class has a pointer to parent as a member variable, which is not the same with Win32 Window. And in my opinion EnumChildWindows do not consider top level window(separate dialog which can have focus and position in taskbar) as a child window.

     

    Well, I respectfully disagree. Child-Parent relation of a MFC Dialog object exactly the same as Win32's Child Window.

    MFC is just a wrapper. There is no any relationship in MFC CWnd object, until window is attached to this object. All windows attached to an MFC object are just regular windos (you called Win32’s windows).

    Wednesday, December 09, 2009 9:36 PM
  • I think all confusion here has a source in naming parameter passed to Create member of CWnd derived classes as well as API. this parameter in all documentation is called "parent".

    Parent-child relationship is established only between a window hosting another with WS_CHILD style. Child window is confined to a client area of the parent.

     

    Windows created without WS_CHILD style, with window handle or CWnd pointer passed as parent parameter, are top-level windows owned by the owner of a handle. There is no child parent relationship here.

     

    EnumWindowsenumerates all top level windows (owned or not) created before EnumWindows call was made.

    Wednesday, December 09, 2009 9:38 PM