오후 1:32 2002-09-19
조경민
=================================================================

PRB: Messages Not Received by Dynamically Created Control

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
Microsoft Visual C++, 32-bit Enterprise Edition, version 4.2
Microsoft Visual C++, 32-bit Professional Edition, version 4.2
Microsoft Visual C++, 32-bit Enterprise Edition, version 5.0
Microsoft Visual C++, 32-bit Professional Edition, version 5.0
Microsoft Visual C++, 32-bit Enterprise Edition, version 6.0
Microsoft Visual C++, 32-bit Professional Edition, version 6.0
Microsoft Visual C++, 32-bit Learning Edition, version 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:

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 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
Issue type : kbprb
Technology : kbAudDeveloper kbMFC


Last Reviewed: May 3, 2001
ⓒ 2001 Microsoft Corporation. All rights reserved. Terms of Use.




--------------------------------------------------------------------------------
Send feedback to MSDN.Look here for MSDN Online resources.

'KB > MFC/Win32' 카테고리의 다른 글

[winsock] 소켓 타임 아웃 주기  (0) 2004.04.28
새창으로 웹브라우저 띄우기  (0) 2004.04.28
다이얼로그 팁  (0) 2004.04.28
VC++에서 제공하는 property를 에뮬레이트  (0) 2004.03.19
msn6 코드 스니펫  (0) 2004.03.19

+ Recent posts