PRB: Messages Not Received by Dynamically Created Control

ID: Q156051


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, 4.2, 5.0, 6.0


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 kbGrpMFCATL kbArchitecture
Version : winnt:4.0,4.1,4.2,5.0,6.0
Platform : winnt
Issue type : kbprb


Last Reviewed: October 15, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.