ActiveX Controls: Methods
| Overview | How Do I | FAQ | | Sample
An ActiveX control fires events to communicate between itself and its control container. A container can also communicate with a control by means of methods and properties. Methods and properties provide an exported interface for use by other applications, such as Automation clients and ActiveX control containers. For more information on ActiveX control properties, see the article ActiveX Controls: Properties.
Methods are similar in usage and purpose to the member functions of a C++ class. There are two types of methods your control can implement: stock and custom. Similar to stock events, stock methods are those methods for which provides an implementation. For more information on stock methods, see the article ActiveX Controls: Adding Stock Methods to an ActiveX Control. Custom methods, defined by the developer, allow additional customization of the control. For more information, see the article ActiveX Controls: Adding Custom Methods to an ActiveX Control.
The Microsoft Foundation Class Library (MFC) implements a mechanism that allows your control to support stock and custom methods. The first part is class COleControl. Derived from CWnd, COleControl member functions support stock methods that are common to all ActiveX controls. The second part of this mechanism is the dispatch map. A dispatch map is similar to a message map; however, instead of mapping a function to a Windows message ID, a dispatch map maps virtual member functions to IDispatch IDS.
For a control to support various methods properly, its class must declare a dispatch map. This is accomplished by the following line of code located in control class header (.H) file:
DECLARE_DISPATCH_MAP()
The main purpose of the dispatch map is to establish the relationship between the method names used by an external caller (such as the container) and the member functions of the control’s class that implement the methods. Once the dispatch map has been declared, it needs to be defined in the control’s implementation (.CPP) file. The following lines of code define the dispatch map:
BEGIN_DISPATCH_MAP(CSampleCtrl, COleControl)
//{{AFX_DISPATCH_MAP(CSampleCtrl)
...
//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()
If ControlWizard was used to create the project, these lines were added automatically. If ControlWizard was not used, you must add these lines manually. The two comment lines allow ClassWizard to automatically insert macros into the dispatch map.
The following articles discuss Methods in detail: