PRB: Messages Not Received by Dynamically Created Control

Last reviewed: July 10, 1997
Article ID: Q156051
4.00 4.10 4.20 WINDOWS kbprg kbprb

The information in this article applies to:

  • The Microsoft Foundation Classes (MFC), included with: Microsoft Visual C++, 32-bit Edition, versions 4.0, 4.1, 4.2

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() doesn't subclass the HWND associated with the control.

STATUS

This problem is by design.

WORKAROUND

As a workaround, use a CFormView or CDialog and add the control to the dialog box resource at design time via the Resource Editor. You can then add a member variable using Class Wizard. The C++ class wrapper created by Component Gallery doesn't have a message map, so you must add this manually.

The following steps illustrates how to insert an ActiveX Control and create a message handler for it:

  1. While editing the dialog box resource, on the Visual C++ menu, click Insert/Component to insert a control and generate a C++ wrapper class.

  2. Add the control to your dialog box resource by dragging it from the Controls toolbar.

  3. Add a member variable for it using Class Wizard

  4. In the header file 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.

  5. In the .cpp file 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.
    
    

  6. Start Class Wizard, click Add Class/From A File, and add this class.

  7. Add your message handler.

MORE INFORMATION

When you insert a control via Component Gallery, the C++ class wrapper has Create() functions that also call CWnd::CreateControl().

The workaround above works because adding a member variable adds a call to DDX_Control(). DDX_Control() calls SubclassWindow() for you.

If you try to call SubclassWindow() after calling CreateControl(), you will get several ASSERTs, because the HWND associated with the control has already been attached to a different MFC class (CWnd member of COleControlSite).


KBCategory: kbprg kbprb
KBSubcategory: MfcOLE
Additional reference words: 4.00 4.10 4.20 kbdsi
Keywords : MfcOLE kbprb kbprg
Technology : kbMfc
Version : 4.00 4.10 4.20
Platform : 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: July 10, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.