冲击钻打桩机用电功率:COM开发中用到的宏说明集合

来源:百度文库 编辑:偶看新闻 时间:2024/04/24 04:41:22
class CMyClass : public CCmdTarget{ ... protected:// Connection point for ISample interface 使用连接宏定义内嵌的连接点对象,以下实现了连接点对象类XSampleConnPt的声明 BEGIN_CONNECTION_PART(CMyClass, SampleConnPt) CONNECTION_IID(IID_ISampleSink) END_CONNECTION_PART(SampleConnPt) DECLARE_CONNECTION_MAP()};#define BEGIN_CONNECTION_PART(theClass, localClass) \ //theClass用于计算偏移量 class X##localClass : public CConnectionPoint { public: X##localClass() { m_nOffset = offsetof(theClass, m_x##localClass); }#define CONNECTION_IID(iid) REFIID GetIID() { return iid; }#define END_CONNECTION_PART(localClass) } m_x##localClass; friend class X##localClass;
#define DECLARE_CONNECTION_MAP() \ //如果你的控制支持附加的连接点,就在你的类声明的末尾使用DECLARE_CONNECTION_MAP()宏private: static const AFX_CONNECTIONMAP_ENTRY _connectionEntries[]; protected: static const AFX_CONNECTIONMAP connectionMap; static const AFX_CONNECTIONMAP* PASCAL GetThisConnectionMap(); virtual const AFX_CONNECTIONMAP* GetConnectionMap() const; \BEGIN_CONNECTION_PART 和 END_CONNECTION_PART 宏声明嵌入类 XSampleConnPt(从 CConnectionPoint 派生),该类实现此特定的连接点。如果您想重写任何CConnectionPoint 成员函数或添加您自己的成员函数,请在这两个宏之间声明它们。例如,将 CConnectionPoint::GetIID 成员函数放在这两个宏之间时,CONNECTION_IID 宏重写该成员函数。
在第二个示例中,代码插入到控件的实现文件(.cpp 文件)中。该代码实现连接映射,该连接映射包括连接点 SampleConnPt:
以下为实现连接映射表
BEGIN_CONNECTION_MAP(CMyClass, CMyBaseClass)//CMyBaseClass为基类:常为CCmdTarget CONNECTION_PART(CMyClass, IID_ISampleSink, SampleConnPt)END_CONNECTION_MAP()
#define BEGIN_CONNECTION_MAP(theClass, theBase) const AFX_CONNECTIONMAP* PASCAL theClass::GetThisConnectionMap() { return &theClass::connectionMap; } const AFX_CONNECTIONMAP* theClass::GetConnectionMap() const { return &theClass::connectionMap; } AFX_COMDAT const AFX_CONNECTIONMAP theClass::connectionMap = { &theBase::GetThisConnectionMap, &theClass::_connectionEntries[0], }; AFX_COMDAT const AFX_CONNECTIONMAP_ENTRY theClass::_connectionEntries[] = { \#define CONNECTION_PART(theClass, iid, localClass) { &iid, offsetof(theClass, m_x##localClass) }, #define END_CONNECTION_MAP() { NULL, (size_t)-1 } }; \
如果类有一个以上的连接点,请在 BEGIN_CONNECTION_MAP 和 END_CONNECTION_MAP 宏之间插入附加的 CONNECTION_PART 宏。
最后,在类的构造函数中添加对 EnableConnections 的调用。例如:
CMyClass::CMyClass(){EnableConnections();...}
插入了此代码后,CCmdTarget 派生的类公开 ISampleSink 接口的一个连接点。下图阐释了该示例。
由 MFC 实现的一个连接点

连接点通常支持“多路广播”- 向连接到同一接口的多个接收器广播的能力。下面的示例段落说明如何循环访问连接点上的每一个接收器来多路广播:
void CMyClass::CallSinkFunc(){ const CPtrArray* pConnections = m_xSampleConnPt.GetConnections(); ASSERT(pConnections != NULL); int cConnections = pConnections->GetSize(); ISampleSink* pSampleSink; for (int i = 0; i < cConnections; i++) { pSampleSink = (ISampleSink*)(pConnections->GetAt(i)); if(pSampleSink != NULL) pSampleSink->SinkFunc(); }}
该示例通过调用 CConnectionPoint::GetConnections 检索 SampleConnPt 连接点上的当前连接集。然后它循环访问这些连接并在每一个活动连接上调用 ISampleSink::SinkFunc。
请参见
MFC COM