3.添加基类为CCmdTarget的类CConnObject.在类声明文件CConnObject1.h中加上#include “IConnObject.h”,在类声明中写入:
protected:
……
//声明实现IEventServer接口的嵌套类
BEGIN_INTERFACE_PART(EventServer,IEventServer)
STDMETHOD(DoSomething)();
END_INTERFACE_PART(EventServer)
DECLARE_INTERFACE_MAP()
//声明实现连接点的嵌套类
BEGIN_CONNECTION_PART(CConnObject,SampleConnPoint)
CONNECTION_IID(IID_IEventSink)
END_CONNECTION_PART(SampleConnPoint)
DECLARE_CONNECTION_MAP()
DECLARE_OLECREATE(CConnObject)
说明:BEGIN_CONNECTION_PART和END_CONNECTION_PART宏声明了实现连接点的嵌套类 SampleConnPoint,并且是基于CConnectionPoint类的,如果需要重载CConnectionPoint类的成员函数或者添加 自己的成员函数,可以在这两个宏中声明.这里,CONNECTION_IID宏重载了CConnectionPoint::GetIID()函数.使用 DECLARE_CONNECTION-MAP()宏声明连接点映射表.
4.在类CConnObject的实现文件中写入
IMPLEMENT_OLECREATE(CConnObject,"ConnObject",
0xee888b01, 0xea9c, 0x11d3, 0x97, 0xb5, 0x52, 0x54, 0xab, 0x19, 0x19, 0x30);
BEGIN_INTERFACE_MAP(CConnObject,CCmdTarget)
INTERFACE_PART(CConnObject,IID_IEventServer,EventServer)
INTERFACE_PART(CConnObject,IID_IConnectionPointContainer,ConnPtContainer)
END_INTERFACE_MAP()
BEGIN_CONNECTION_MAP(CConnObject,CCmdTarget)
CONNECTION_PART(CConnObject,IID_IEventSink,SampleConnPoint)
END_CONNECTION_MAP()
说明:A.必须在接口映射中写入INTERFACE_PART(CConnObject, IID_IConnectionPointContainer,ConnPtContainer)以实现 IConnectionPointContainer接口.注意,CCmdTarget类内嵌有才ConnPtContainer类以实现 IConnectionPointContainer接口,并用m_xConnPtContainer加以记录.
B.用BEGIN_CONNECTION_MAP和END_CONNECTION_MAP宏实现连接点映射.CONNECTION_PART定义了实现连接点的类.
5.在CConnObject::CConnObject()