Partager via


MFC ActiveX Controls: Events

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.

Events are also called messages.

MFC supports two kinds of events: stock and custom. Stock events are those events that class COleControl handles automatically. For a complete list of stock events, see the article MFC ActiveX Controls: Adding Stock Events. 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 Visual Studio 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()

After 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(CMyAxUICtrl, COleControl)


...


END_EVENT_MAP()

If you use the MFC ActiveX Control Wizard to create the project, it automatically adds these lines. If you do not use the MFC ActiveX Control Wizard, you must add these lines manually.

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

Two other articles discuss events in detail:

See Also

Concepts

MFC ActiveX Controls

MFC ActiveX Controls: Methods

COleControl Class