Delegate and interface map macros

MFC supports these macros for delegate and interface maps:

Name Description
BEGIN_DELEGATE_MAP Begins a delegate map.
BEGIN_INTERFACE_MAP Begins the definition of the interfaced map.
CommandHandler Delegate Registers callback methods with a command source.
END_DELEGATE_MAP Ends a delegate map.
END_INTERFACE_MAP Ends the interface map in the implementation file.
EVENT_DELEGATE_ENTRY Creates an entry in the delegate map.
INTERFACE_PART Used between the BEGIN_INTERFACE_MAP macro and the END_INTERFACE_MAP macro for each interface your object will support.
MAKE_DELEGATE Attaches an event handler to a managed control.

BEGIN_DELEGATE_MAP

Begins a delegate map.

Syntax

BEGIN_DELEGATE_MAP(  CLASS );

Parameters

CLASS
The class in which the managed control is hosted.

Remarks

This macro marks the beginning of a list of delegate entries, which compose a delegate map. For an example of how this macro is used, see EVENT_DELEGATE_ENTRY.

Requirements

Header: msclr\event.h

BEGIN_INTERFACE_MAP

Begins the definition of the interfaced map when used in the implementation file.

Syntax

BEGIN_INTERFACE_MAP( theClass, baseClass )

Parameters

theClass
The class in which the interface map is to be defined

baseClass
The class from which theClass derives from.

Remarks

For each interface that is implemented, there is one or more INTERFACE_PART macro invocations. For each aggregate that the class uses, there is one INTERFACE_AGGREGATE macro invocation.

For more information on interface maps, see Technical Note 38.

Requirements

Header: afxwin.h

CommandHandler Delegate

Registers callback methods with a command source.

Syntax

delegate void CommandHandler(  UINT^ cmdID  );

Parameters

cmdID
The command ID.

Remarks

This delegate registers callback methods with a command source. When you add a delegate to the command source object, the callback method becomes a handler for commands coming from the specified source.

For more information, see How to: Add Command Routing to the Windows Forms Control.

For more information on using Windows Forms, see Using a Windows Form User Control in MFC.

Requirements

Header: afxwinforms.h (defined in assembly atlmfc\lib\mfcmifc80.dll)

CommandUIHandler

Registers callback methods with a user interface update command message.

Syntax

delegate void CommandUIHandler(  unsigned int cmdID, ICommandUI^ cmdUI);

Parameters

cmdID
The command ID.

cmdUI
The command message ID.

Remarks

This delegate registers callback methods with a user interface update command message. CommandUIHandler is similar to CommandHandler except that this delegate is used with user interface object update commands. User interface update commands should be mapped one-to-one with message handler methods.

For more information on using Windows Forms, see Using a Windows Form User Control in MFC.

Requirements

Header: afxwinforms.h (defined in assembly atlmfc\lib\mfcmifc80.dll)

END_DELEGATE_MAP

Ends a delegate map.

Syntax

END_DELEGATE_MAP();

Remarks

This macro marks the end of a list of delegate entries, which compose a delegate map. For an example of how this macro is used, see EVENT_DELEGATE_ENTRY.

Requirements

Header: msclr\event.h

END_INTERFACE_MAP

Ends the interface map in the implementation file.

Syntax

END_INTERFACE_MAP( )

Remarks

For more information about interface maps, see Technical Note 38.

Requirements

Header: afxwin.h

EVENT_DELEGATE_ENTRY

Creates an entry in the delegate map.

Syntax

EVENT_DELEGATE_ENTRY(MEMBER, ARG0, ARG1);

Parameters

MEMBER
The event handler method to be attached to the control.

ARG0
The first argument of the managed event handler method, such as Object^.

ARG1
The second argument of the managed event handler method, such as EventArgs^.

Remarks

Each entry in the delegate map corresponds to a managed event handler delegate created by MAKE_DELEGATE.

Example

The following code example shows how to use EVENT_DELEGATE_ENTRY to create an entry in the delegate map for the OnClick event handler; also see the code example in MAKE_DELEGATE. For more information, see How to: Sink Windows Forms Events from Native C++ Classes.

BEGIN_DELEGATE_MAP(CMyView)
   EVENT_DELEGATE_ENTRY(OnClick, System::Object^, System::EventArgs^)
END_DELEGATE_MAP()

Requirements

Header: msclr\event.h

INTERFACE_PART

Used between the BEGIN_INTERFACE_MAP macro and the END_INTERFACE_MAP macro for each interface your object will support.

Syntax

INTERFACE_PART( theClass, iid, localClass)

Parameters

theClass
The name of the class that contains the interface map. iid
The IID that is to be mapped to the embedded class. localClass
The name of the local class.

Remarks

It allows you to map an IID to a member of the class indicated by theClass and localClass.

For more information on interface maps, see Technical Note 38.

Requirements

Header: afxwin.h

MAKE_DELEGATE

Attaches an event handler to a managed control.

Syntax

MAKE_DELEGATE( DELEGATE,  MEMBER) ;

Parameters

DELEGATE
The type of the managed event handler delegate, such as EventHandler.

MEMBER
The name of the event handler method to be attached to the control.

Remarks

This macro creates a managed event handler delegate of type DELEGATE and of the name MEMBER. The managed event handler delegate allows a native class to handle managed events.

Example

The following code example shows how to call MAKE_DELEGATE to attach an OnClick event handler to an MFC control MyControl. For a broader explanation of how this macro works in an MFC application, see How to: Sink Windows Forms Events from Native C++ Classes.

// CMyView derives from CWinFormsView.
void CMyView::OnInitialUpdate()
{
   CWinFormsView::OnInitialUpdate();

   GetControl()->Click += MAKE_DELEGATE(System::EventHandler, OnClick);
}

Requirements

Header: msclr\event.h

See also

How to: Sink Windows Forms Events from Native C++ Classes
How to: Add Command Routing to the Windows Forms Control
Macros and Globals