18.04.2015 Views

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

VISUAL C++<br />

Connection point mechanism for hooking source<br />

to sink objects<br />

The connection is now complete; methods<br />

will be called on any listening sinks by the<br />

source. The sink will typically hold onto<br />

an interface pointer to the source, so when<br />

a sink has finished listening it can be<br />

released from the source object by calling<br />

IConnectionPoint::Unadvise. This is implemented<br />

with AtlUnadvise.<br />

IDispatch events versus pure COM events<br />

An outbound interface can be a pure dispatch interface. This means instead of<br />

the source calling directly onto a method in a sink, the call is made via the<br />

IDispatch::Invoke mechanism. The IDispatch mechanism has a performance overhead<br />

to package parameters compared to a pure vtable COM call. However, there<br />

are some situations where this must be used. ActiveX controls must implement<br />

their default outbound interface as a pure IDispatch interface; for example,<br />

IMapControlEvents2 is a pure dispatch interface. Also, Microsoft Visual Basic 6 can<br />

only be a source of pure IDispatch events. The connection point<br />

mechanism is the same as for pure COM mechanisms, the main<br />

difference being in how the events are fired.<br />

ATL provides some macros to assist with listening to IDispatch<br />

events; this is discussed on MSDN under ‘Event Handling and<br />

ATL’. There are two templates available, IDispEventImpl and<br />

IDispEventSimpleImpl, that are discussed in the following sections.<br />

Visual Studio C++ Class Wizard. Adding event<br />

handler to an ActiveX control on a dialog box.<br />

Using IDispEventImpl to listen to events<br />

The ATL template IDispEventImpl will use a type library to<br />

“crack” the IDispatch calls and process the arguments into C++<br />

method calls. The Visual Studio Class wizard can provide this<br />

mechanism automatically when adding an ActiveX control to a<br />

dialog box. Right-click the Control and click Events. In the Class<br />

wizard, choose the resource ID of the control, choose the event,<br />

then click Add Handler.<br />

The following code illustrates the event handling code added by the wizard, with<br />

some modifications to ensure advise and unadvise are performed.<br />

#pragma once<br />

There is a bug in the wizard: it does not add<br />

the advise and unadvise code to the dialog box.<br />

To fix this issue, add a message handler for<br />

OnDestroy. Then in the OnInitDialog handler,<br />

call AtlAdviseSinkMap with a TRUE second<br />

parameter to begin listening to events. Place a<br />

corresponding call to AtlAdviseSinkMap (with<br />

FALSE as the second parameter) in the<br />

OnDestroy handler. This is discussed further in<br />

the MSDN article “BUG: ActiveX Control Events<br />

Are Not Fired in ATL Dialog (Q190530)”.<br />

#include "resource.h" // Main symbols<br />

#include <br />

////////////////////////////////////////////////////////////////////////////<br />

// CMyDialog<br />

class CMyDialog :<br />

public CAxDialogImpl,<br />

public IDispEventImpl<br />

{<br />

130 • <strong>ArcGIS</strong> <strong>Engine</strong> <strong>Developer</strong> <strong>Guide</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!