DOCUMENT:Q156051 11-JUN-2002 [visualc] TITLE :PRB: Messages Not Received by Dynamically Created Control PRODUCT :Microsoft C Compiler PROD/VER::4.0,4.1,4.2,5.0,6.0 OPER/SYS: KEYWORDS:kbActiveX kbCOMt kbContainer kbCtrlCreate kbMFC kbVC400 kbVC410 kbVC420 kbVC500 kbVC600 ====================================================================== ------------------------------------------------------------------------------- The information in this article applies to: - The Microsoft Foundation Classes (MFC), used with: - Microsoft Visual C++, 32-bit Editions, versions 4.0, 4.1 - Microsoft Visual C++, 32-bit Enterprise Edition, versions 4.2, 5.0, 6.0 - Microsoft Visual C++, 32-bit Professional Edition, versions 4.2, 5.0, 6.0 - Microsoft Visual C++, 32-bit Learning Edition, version 6.0 - Microsoft Visual C++.NET (2002) ------------------------------------------------------------------------------- NOTE: Microsoft Visual C++ NET (2002) supported both the managed code model that is provided by the .NET Framework and the unmanaged native Windows code model. The information in this article applies to unmanaged Visual C++ code only. 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. CAUSE ===== CWnd::CreateControl() does not subclass the HWND associated with the control. 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: 1. 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. 2. Declare a member variable that is a pointer to the class just added in the class that will create the control. 3. In the header file of the wrapper class generated by Component Gallery, add these lines at the end of the class declaration: //{{AFX_MSG(CMyClass) //}}AFX_MSG DECLARE_MESSAGE_MAP() Replace CMyClass with the actual class name. 4. 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(CMyClass) //}}AFX_MSG_MAP END_MESSAGE_MAP() Again, replace CMyClass with the actual class name. 5. 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. 6. Use class wizard to add your message handler for the wrapper class. 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); } STATUS ====== This is by design. Additional query words: subclass AfxWndProc message routing map architecture ====================================================================== Keywords : kbActiveX kbCOMt kbContainer kbCtrlCreate kbMFC kbVC400 kbVC410 kbVC420 kbVC500 kbVC600 kbDSupport kbGrpDSMFCATL kbArchitecture Technology : kbAudDeveloper kbMFC Version : :4.0,4.1,4.2,5.0,6.0 Issue type : kbprb ============================================================================= 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. Copyright Microsoft Corporation 2002.