Windows messages are not sent to the CWnd-derived class when you create ActiveX Controls dynamically though CWnd::CreateControl in Visual C++ (156051)
The information in this article applies to:
- The Microsoft Foundation Classes (MFC), when used with:
- Microsoft Visual C++, 32-bit Editions 4.0
- Microsoft Visual C++, 32-bit Editions 4.1
- Microsoft Visual C++, 32-bit Enterprise Edition 4.2
- Microsoft Visual C++, 32-bit Enterprise Edition 5.0
- Microsoft Visual C++, 32-bit Enterprise Edition 6.0
- Microsoft Visual C++, 32-bit Professional Edition 4.2
- Microsoft Visual C++, 32-bit Professional Edition 5.0
- Microsoft Visual C++, 32-bit Professional Edition 6.0
- Microsoft Visual C++, 32-bit Learning Edition 6.0
- Microsoft Visual C++ .NET (2003)
- Microsoft Visual C++ .NET (2002)
This article was previously published under Q156051 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. 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:
- 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.
- Declare a member variable that is a pointer to the class
just added in the class that will create the control.
- 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. - 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. - 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.
- 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 behavior is by design.
Modification Type: | Major | Last Reviewed: | 9/2/2005 |
---|
Keywords: | kbProperties kbtshoot kbArchitecture kbContainer kbCtrlCreate kbprb KB156051 kbAudDeveloper |
---|
|