Microsoft KB Archive/150204

= How to retrieve the actual parent window of an ActiveX control =

Article ID: 150204

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 1.5 Professional Edition

 Microsoft Visual C++ 1.51

 Microsoft Visual C++ 1.52 Professional Edition

 Microsoft Visual C++ 2.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.1</li></ul>

 Microsoft Visual C++ 2.2</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul>

 Microsoft Visual C++ 4.1 Subscription</li></ul>

 Microsoft Visual C++ 5.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 5.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

<ul> <li>Microsoft Visual C++ 6.0 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q150204

<div class="summary_section">

SUMMARY
An ActiveX control is created as a child of the window of the container most closely associated with the site object. This article describes how to obtain the parent window of an ActiveX control, and why calling GetParent or CWnd::GetParent for an ActiveX control may not return its actual parent window.

<div class="moreinformation_section">

MORE INFORMATION
An ActiveX control typically gains access to the following container windows:
 * Frame window: the outer-most container window where the container's main menu resides. An ActiveX control retrieves this window handle by calling either IOleInPlaceFrame::GetWindow or IOleInPlaceSite::GetWindowContext.
 * Site window: the container window that contains the ActiveX control's view. An ActiveX control retrieves this container window by calling IOleInPlaceSite::GetWindow.

However, ActiveX control containers generated using MFC and Microsoft Visual Basic 4.0 or 5.0 use the same window for the frame and the site object.

When the MFC framework creates an ActiveX control's window, it retrieves the window associated with the container's site object by calling IOleInPlaceSite::GetWindow. The returned window is made the parent of the ActiveX control, except in the following two cases, where the framework creates a reflector window that reflects the notification messages:
 * The ActiveX control subclasses a Windows control, and the container does not support message reflection.
 * The control container does not support automatic clipping of its controls.

In these two scenarios, the ActiveX control is made a child of the reflector window whose parent is the window returned from IOleInPlaceSite::GetWindow.

Note If IOleObject::DoVerb with OLEIVERB_OPEN is invoked on an ActiveX control, and if in-place activation is not possible, an outer frame window is created and becomes the parent of the ActiveX control.

Sample code
// The following code should return the // actual parent window of the ActiveX control. HWND CMyOleControl::GetActualParent {      HWND hwndParent = 0;

// Get the window associated with the in-place site object, // which is connected to this ActiveX control. if (m_pInPlaceSite != NULL) m_pInPlaceSite->GetWindow(&hwndParent);

return hwndParent;    // Return the in-place site window handle. }

<div class="references_section">