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
The Add Event dialog box appears.
ClickIn
.OLE_XPOS_PIXELS
).OLE_YPOS_PIXELS
).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);
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
...
}