Microsoft KB Archive/156051

= Windows messages are not sent to the CWnd-derived class when you create ActiveX Controls dynamically though CWnd::CreateControl in Visual C++ =

Article ID: 156051

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 4.0 Standard Edition

 Microsoft Visual C++ 4.1 Subscription

 Microsoft Visual C++ 4.2 Enterprise Edition

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

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

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

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

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

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

 Microsoft Visual C++ .NET 2003 Standard Edition</li></ul>

 Microsoft Visual C++ .NET 2002 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q156051

<div class="notice_section">

Note Microsoft Visual C++ .NET 2002 and Microsoft Visual C++ .NET 2003 support both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code.

<div class="symptoms_section">

SYMPTOMS
When you create ActiveX Controls dynamically via CWnd::CreateControl, Windows messages are not sent to your CWnd-derived class. For example, if you create a handler for WM_KILLFOCUS, it is not called.

<div class="cause_section">

CAUSE
CWnd::CreateControl does not subclass the HWND associated with the control.

<div class="resolution_section">

RESOLUTION
To work around this problem, you must subclass the control after it has been created.

The following steps illustrate how to insert an ActiveX Control and create a message handler for it: <ol> From the Project menu in Visual Studio, select Add to Project, and then select Components and Controls. This will bring up a dialog box. Select the folder Registered ActiveX Controls. Select a control to insert. As a result, a C++ wrapper class will be generated.</li> Declare a member variable that is a pointer to the class just added in the class that will create the control.</li> <li> In the header file of the wrapper class generated by Component Gallery, add these lines at the end of the class declaration: //AFX_MSG DECLARE_MESSAGE_MAP Replace CMyClass with the actual class name. </li> <li> In the .cpp file of the wrapper class generated by Component Gallery, add these lines after the #include section: BEGIN_MESSAGE_MAP(CMyClass, CWnd) //AFX_MSG_MAP END_MESSAGE_MAP Again, replace CMyClass with the actual class name. </li> <li>Delete the .clw file for your project, and then start the class wizard. This will give you a message box that says "The class wizard database for this project does not exist, do you wish to create it from the source files?" Click yes. In the dialog box that opens, click Add All to add all project files.</li> <li>Use class wizard to add your message handler for the wrapper class.</li></ol>

After modifying the wrapper class and adding the message handler, you must create the control and subclass it. The following code shows how to dynamically create an Microsoft FlexGrid control and subclass it so that the MFC wrapper class can receive Windows messages: void CSDIApp2View::OnInitialUpdate {   CView::OnInitialUpdate; m_pFlexGrid = new CMSFlexGrid;

CRect rect; GetClientRect(&rect); m_pFlexGrid->Create(NULL, WS_CHILD | WS_VISIBLE, rect, this, IDC_FLEXGRID); HWND hWnd = m_pFlexGrid->Detach; m_pFlexGrid->SubclassWindow(hWnd); }

<div class="status_section">

STATUS
This behavior is by design.

Additional query words: subclass AfxWndProc message routing map architecture

Keywords: kbproperties kbtshoot kbarchitecture kbcontainer kbctrlcreate kbprb KB156051

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.