HOWTO: Handling Events for ActiveX Controls in a CWnd

Last reviewed: June 26, 1997
Article ID: Q147740
The information in this article applies to:
  • Microsoft Visual C++, 32-bit Edition, versions 4.0, 5.0

SUMMARY

ActiveX controls are most often used in CDialog- or CFormView-derived classes. There are occasions that call for an ActiveX Control to be a child of a window that is not associated with a dialog template, such as a CWnd. Adding event handlers for a control created as a child of a CWnd requires a slightly different approach than adding the same handlers for the control when placed on a dialog. This article provides step-by-step instructions for adding event handlers to the CWnd-derived class that parents the control.

MORE INFORMATION

Step-by-Step Example

Use the following steps to create an ActiveX control dynamically in a CWnd- derived window. The control used here is the Circ3 control from the Circle Tutorial, but the approach is valid for any OLE control. After creating the control, handlers are created for Circ3's ClickIn and ClickOut events.

  1. Create the application that is to contain the ActiveX control. This application must include support for ActiveX controls. Select the window that is to be the parent of the ActiveX control. This window could be an SDI or MDI view or any window of a CWnd-derived class.

  2. Insert the Circ3 control from the Component Gallery. At this point, ClassWizard will add a wrapper class for the Circ3 control and will add Circ3 to the toolbar in the dialog editor.

  3. On the Insert menu, click Resource, and then click Dialog to insert a new dialog resource into the project. Select Circ3 from the toolbar, and add it to your dialog. Note the control ID will be something like IDC_CIRC3CTRL1.

  4. With the dialog still open in the editor, press CTRL+W to bring up ClassWizard. At this point, you will see the "Adding a Class" dialog box. Choose "Select an existing class," and specify the CWnd-derived class. Click Yes to confirm that this is not a dialog class.

  5. In ClassWizard, select the object ID of the Circ3 control in the "Object ID's" Pane. You will see the control's events listed in the messages window. Select ClickIn, and click ADD FUNCTION. Then select ClickOut, and click ADD FUNCTION. ClassWizard adds the appropriate EVENTSINK_MAP, ON_EVENT macros, and event handlers to the class specified in Step 4.

    NOTE: Add all necessary handlers at this time. In order to add more handlers later, you must delete the dialog resource and repeat this process.

  6. Add a CCirc3 member variable named m_circ to the CWnd-derived class declaration as shown below. You must include the header file of the CCirc3 wrapper class for this declaration:

    CCirc3 m_circ;

  7. Use ClassWizard to override the Create() virtual function for the CWnd- derived class. In the handler, call Create() to create the Circ3 child control as shown below.

    NOTE: If you are creating the ActiveX control in a view, you may override OnInitialUpdate() instead of Create().

          BOOL MyCWnd::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName,
    
            DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID,
            CCreateContext* pContext)
          {
          BOOL result = CWnd::Create(lpszClassName, lpszWindowName, dwStyle,
            rect, pParentWnd, nID, pContext);
    
          if(result != 0)   //Create the Circ3 ActiveX control.
            result = m_circ.Create("Test", WS_VISIBLE, CRect(1,1,130, 120),
               this, IDC_CIRC3CTRL1);
    
          return result;
          }
    
       IMPORTANT: Parameter 5 is the ID for the control. This ID must match the
       ID used in the ON_EVENT macro created by ClassWizard. This is the ID of
       the Circ3 control created in Step 3.
    
    

  8. After adding the necessary event handlers, you may delete the temporary dialog resource from the project. After you delete the dialog resource, you will need to add the m_circ member variable and a #define for IDC_CIRC3CTRL1, the ID of the Circ3 control.

Optional: Another approach often used to add event handlers to CWnd-derived classes is to add controls and event handlers to a dialog class, and then cut and paste the appropriate code into the View.h and View.cpp files making modifications as necessary. Note, however, that the previous method is less prone to errors.

REFERENCES

"MFC 4.0 Helps You Contain Your OLE Controls," MSDN Nov/Dec 1995.

OLE Control Containers: Using AppWizard to Create a Container Application OLE Control Containers: Programming OLE Controls in an OLE Control Container - Visual C++ Books Online, MFC Encyclopedia


Keywords : CDKIss kbcode MfcOLE
Technology : kbole kbMfc
Version : 4.0 5.0
Platform : WINDOWS
Issue type : kbhowto


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: June 26, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.