Adding the ClickIn Event

Events, like properties, are added to a control using ClassWizard. When you add a custom event, ClassWizard creates the code necessary to declare the event; however, the control developer must write additional code to fire the event.

An event can also define parameters it can pass to the control’s container when it is fires. Event parameters can be added using ClassWizard. In this topic, you will define two parameters for the ClickIn event: x and y, which represent the x and y coordinates of the mouse position when the left mouse button is clicked.

To add the ClickIn event

  1. On the View menu, click ClassWizard.

  2. Click the ActiveX Events tab.

  3. From the Class name drop-down list box, select CCircCtrl.

  4. Click Add Event.

    The Add Event dialog box appears.

  5. In the edit control of the External name drop-down combo box, type ClickIn.

  6. In the Parameter list, use the grid control to add a parameter, called x (with a type of OLE_XPOS_PIXELS).

  7. Use the grid control to add a second parameter, called y (with a type of OLE_YPOS_PIXELS).

  8. Click OK to close the Add Event dialog box.

    This returns you to the ActiveX Events tab. Notice that the implementation of the ClickIn event appears as:

    Implementation:
    void FireClickIn(OLE_XPOS_PIXELS x, OLE_YPOS_PIXELS y);
    
  9. Click OK to confirm your choices and close ClassWizard.

ClassWizard creates the code to add the ClickIn event, modifying both the CCircCtrl class files and the CIRC.ODL file.

ClassWizard modifies the CCircCtrl class event map in CIRCCTL.CPP to add a macro entry for the ClickIn event:

BEGIN_EVENT_MAP(CCircCtrl, COleControl)
    //{{AFX_EVENT_MAP(CCircCtrl)
    EVENT_CUSTOM("ClickIn", FireClickIn, VTS_XPOS_PIXELS  VTS_YPOS_PIXELS)
    //}}AFX_EVENT_MAP
END_EVENT_MAP()

The macro EVENT_CUSTOM associates the ClickIn event name with FireClickIn, the function that actually fires the event, and with the type definitions for the x and y parameters that ClickIn uses.

ClassWizard adds an inline function to the CCircCtrl class declaration in CIRCCTL.H which, when called, fires the ClickIn event. The FireClickIn function simply calls the FireEvent function to do its work. Event functions like FireClickIn are added to provide a type-safe way of firing an event.

class CCircCtrl : public COleControl
{
    ...
    //{{AFX_EVENT(CCircCtrl)
    void FireClickIn(OLE_XPOS_PIXELS x, OLE_YPOS_PIXELS y)
        {FireEvent(eventidClickIn,EVENT_PARAM(VTS_XPOS_PIXELS  VTS_YPOS_PIXELS),
    x, y);}
    //}}AFX_EVENT
    ...
}