Using CWnd::GetParent in a Dialog Box

Last reviewed: October 10, 1997
Article ID: Q118610
7.00 | 1.00 1.50 1.51 1.52 | 1.00 2.00 2.10 4.00
MS-DOS | WINDOWS             | WINDOWS NT
kbprg

The information in this article applies to:

  • The Microsoft Foundation Classes (MFC) included with:

        - Microsoft C/C++ version 7.0
        - Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51, and 1.52
        - Microsoft Visual C++ 32-bit Edition, versions 1.0, 2.0, 2.1, and 4.0
    

SUMMARY

The window object returned by CWnd::GetParent in a modal dialog box is the top-level window that owns the dialog box, usually the main window. It is not necessarily the window object that has been passed to the constructor or Create() of the CDialog object. If you need to access the parent window that was passed in the constructor of the dialog-box object, then you need to save a copy of this pointer in the dialog-box object.

MORE INFORMATION

When Windows creates a new popup window or a top-level window, such as a dialog-box window, it is passed the handle of the parent window for the dialog box. If the specified parent window is not a top-level window, then Windows repeatedly calls GetParent until it finds the handle of a top-level window that contains the specified parent window. Windows then sets this top-level window to be the parent of the new popup or top-level window. So, after the new window is created, if you call GetParent in that window, you will get the pointer to the top-level window object that contains the original parent window that you passed in the constructor or Create() call.

This behavior can cause problems in an MFC application if you try to create a dialog box with a view window as the parent and then try to access the view inside the dialog box by calling GetParent. Because the view window is not a top-level window, Windows automatically converts the handle of the view window to the handle of the top-level window that contains the view, which is usually the main window of the application. Then when you call GetParent, it gets the handle of the main window of the application and then finds the MFC object associated with the main window. If you need to access the view window from the dialog-box object, then you should store the pointer to the view in a member variable of the dialog box. To do this, save a pointer to the view window in the constructor of the dialog box or override the Create() function and have it save the pointer.


Additional reference words: 7.00 1.00 1.50 2.00 2.10 2.50 2.51 2.52 3.00
3.10 kbinf 4.00
KBCategory: kbprg
KBSubcategory: MfcUI
Keywords : MfcUI kbprg
Technology : kbMfc
Version : 7.00 | 1.00 1.50 1.51 1.52 | 1
Platform : MS-DOS NT WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 10, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.