ActiveX Control Containers: Handling Events from an ActiveX Control

OverviewHow Do ISample

This article discusses using ClassWizard to install event handlers for ActiveX controls in an ActiveX control container. The event handlers are used to receive notifications (from the control) of certain events and perform some action in response. This notification is called “firing” the event.

Note   This article uses a dialog-based ActiveX control container project named Container and an embedded control named Circ2 as examples in the procedures and code.

Using the Message Maps tab in ClassWizard, you can create a map of events that can occur in your ActiveX control container application. This map, called an “event sink map,’’ is created and maintained by ClassWizard when you add event handlers to the control container class. Each event handler, implemented with an event map entry, maps a specific event to a container event handler member function. This event handler function is called when the specified event is fired by the ActiveX control object.

For more information on event sink maps, see in the Class Library Reference.

To create an event handler function

  1. On the View menu, click ClassWizard.

  2. Click the Message Maps tab.

  3. In the Class name box, select the dialog box class that contains the ActiveX control. For this example, use CContainerDlg.

  4. In the Object IDs box, select the control ID of the embedded ActiveX control. For this example, use IDC_CIRC2CTRL1.

    The Messages box displays a list of events that can be fired by the embedded ActiveX control. Any member function shown in bold already has handler functions assigned to it.

  5. Select the message you want the application to handle. For this example, select ClickIn.

  6. Click Add Function.

    A suggested name for the handler function appears in the Member Functions box. For this example, use the suggested name.

  7. Click OK to close the Add Function dialog box.

  8. Click Edit Code to jump to the event handler code in the implementation (.CPP) file of CContainerDlg or Close to close ClassWizard.

Event Handler Modifications to the Project

When you use ClassWizard to add event handlers, an event sink map is declared and defined in your project. The following statements are added to the control .CPP file the first time an event handler is added. This code declares an event sink map for the dialog box class (in this case, CContainerDlg):

BEGIN_EVENTSINK_MAP(CContainerDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CContainerDlg)
    //}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

As you use ClassWizard to add events, an event map entry (ON_EVENT) is added to the event sink map and an event handler function is added to the container’s implementation (.CPP) file.

The following example declares an event handler, called OnClickInCirc2Ctrl, for the Circ2 control’s ClickIn event:

BEGIN_EVENTSINK_MAP(CContainerDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CContainerDlg)
   ON_EVENT(CContainerDlg, IDC_CIRC2CTRL1, 1 /*
ClickIn */, OnClickInCirc2ctrl, VTS_I4 VTS_I4)
   //}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

In addition, the following template is added to the CContainerDlg class implementation (.CPP) file for the event handler member function:

BOOL CContainerDlg::OnClickInCirc2ctrl1(OLE_XPOS_PIXELS nX, OLE_YPOS_PIXELS nY)
{
   // use nX and nY here
}

For more information on event sink macros, see in the Class Library Reference.