Share via


ActiveX Controls: Events

OverviewHow Do IFAQSample

ActiveX controls use events to notify a container that something has happened to the control. Common examples of events include clicks on the control, data entered using the keyboard, and changes in the control’s state. When these actions occur, the control fires an event to alert the container.

MFC supports two kinds of events: stock and custom. Stock events are those events that class handles automatically. For a complete list of stock events, see the article ActiveX Controls: Adding Stock Events to an ActiveX Control. Custom events allow a control the ability to notify the container when an action specific to that control occurs. Some examples would be a change in the internal state of a control or receipt of a certain window message.

For your control to fire events properly, your control class must map each event of the control to a member function that should be called when the related event occurs. This mapping mechanism (called an event map) centralizes information about the event and allows ClassWizard to easily access and manipulate the control’s events. This event map is declared by the following macro, located in the header (.H) file of the control class declaration:

DECLARE_EVENT_MAP()

The following figure shows the ActiveX Events tab in ClassWizard. You use this tab to add custom and stock events.

The ActiveX Events tab

Once the event map has been declared, it must be defined in your control’s implementation (.CPP) file. The following lines of code define the event map, allowing your control to fire specific events:

BEGIN_EVENT_MAP(CSampleCtrl, COleControl)
//{{AFX_EVENT_MAP(CSampleCtrl)
...
//}}AFX_EVENT_MAP
END_EVENT_MAP()

If you use ControlWizard to create the project, it automatically adds these lines. If you do not use ControlWizard, you must add these lines manually.

With ClassWizard, you can add stock events supported by class COleControl or custom events that you define. For each new event, ClassWizard automatically adds the proper entry to the control’s event map and the control’s .ODL file.

Two other articles discuss events in detail:

See Also   ActiveX Controls: Methods,