2012-02-07 16:26 mfc工程中添加com事件接口

一般地,使一个mfc类支持IDispatch接口(添加自动化支持)可以通过以下步骤:
1.在CMyDialog的类声明中(即.h)加入
DECLARE_DISPATCH_MAP()
DECLARE_INTERFACE_MAP()
两个宏,再在源文件(.cpp)中添加
BEGIN_INTERFACE_MAP( CMyDialog, CDialog
    INTERFACE_PART( CMyDialog, IID_IDispatch, Dispatch
END_INTERFACE_MAP()

BEGIN_DISPATCH_MAP( CMyDialog, CDialog
    //这里添加属性或方法
END_DISPATCH_MAP()
并在构造函数调用EnableAutomation();即可.在BEGIN_DISPATCH_MAP和END_DISPATCH_MAP宏之间加入想加的DISP_FUNCTION之类的宏以建立映射.如果觉得手动添加DISP_FUNCTION之类的宏麻烦,可如下修改即可使用ClassWizard来对这个类添加方法和属性了

在.cpp中
BEGIN_DISPATCH_MAP( CMyDialog, CDialog
    //{{AFX_DISPATCH_MAP( CMyDialog
    //}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()
在.h中,在类声明中的任何一个地方加入
    //{{AFX_DISPATCH( CMyDialog
    //}}AFX_DISPATCH
如上后就可通过ClassWizard来添加方法和属性了.

示例如下:
用ClassWizaed添加一个Automation类

.h中
class CProxyEvents : public CCmdTarget
{
DECLARE_DYNCREATE(CProxyEvents)

CProxyEvents();           // protected constructor used by dynamic creation

// Attributes
public:
void OnOption(VARIANT *Option);
void OnData(VARIANT *Data);
void OnError(int ErrorCode);
void OnConnectStatus(int ConnectStatus);
void OnErrorMessage(BSTR ErrorMessage);
void OnNews(BSTR News);
void OnKeepAlive(BSTR AckResponse);
HWND hwnd;
// Operations
public:
virtual ~CProxyEvents();
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CProxyEvents)
public:
virtual void OnFinalRelease();
//}}AFX_VIRTUAL

// Implementation
protected:

// Generated message map functions
//{{AFX_MSG(CProxyEvents)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG

DECLARE_MESSAGE_MAP()
// Generated OLE dispatch map functions
//{{AFX_DISPATCH(CProxyEvents)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_DISPATCH
DECLARE_DISPATCH_MAP()
DECLARE_INTERFACE_MAP()
};

.cpp中
BEGIN_MESSAGE_MAP(CProxyEvents, CCmdTarget)
//{{AFX_MSG_MAP(CProxyEvents)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BEGIN_DISPATCH_MAP(CProxyEvents, CCmdTarget)
DISP_FUNCTION_ID(CProxyEvents, "Option",8,OnOption, VT_EMPTY, VTS_PVARIANT)
DISP_FUNCTION_ID(CProxyEvents, "Data",2,OnData, VT_EMPTY, VTS_PVARIANT)

// [id(3), helpstring("method Error"] void Error(int ErrorCode);
DISP_FUNCTION_ID(CProxyEvents, "Error",3,OnError, VT_EMPTY, VTS_I4)
// [id(4), helpstring("method ConnectStatus"] void ConnectStatus(int ConnectStatus);
DISP_FUNCTION_ID(CProxyEvents, "ConnectStatus",4,OnConnectStatus, VT_EMPTY, VTS_I4)
// [id(5), helpstring("method ErrorMessage"] void ErrorMessage(BSTR ErrorMessage);
DISP_FUNCTION_ID(CProxyEvents, "ErrorMessage",5,OnErrorMessage, VT_EMPTY, VTS_BSTR)
// [id(6), helpstring("method News"] void News(BSTR News);
DISP_FUNCTION_ID(CProxyEvents, "News",6,OnNews, VT_EMPTY, VTS_BSTR)
// [id(7), helpstring("method KeepAlive"] void KeepAlive(BSTR AckResponse);
DISP_FUNCTION_ID(CProxyEvents, "KeepAlive",7,OnKeepAlive, VT_EMPTY, VTS_BSTR)
END_DISPATCH_MAP()

// Note: we add support for IID_IProxyEvents to support typesafe binding
//  from VBA.  This IID must match the GUID that is attached to the
//  dispinterface in the .ODL file.

// {1F2E2D8A-7CE3-4BCA-A360-C66C46A86626}

BEGIN_INTERFACE_MAP(CProxyEvents, CCmdTarget)
INTERFACE_PART(CProxyEvents, DIID__IServerProxyEvents, Dispatch)
END_INTERFACE_MAP()

代码中用
    LPUNKNOWN unknown=pProxyEvents->GetIDispatch(FALSE);
    m_dwCookie=0;
    int ret=AfxConnectionAdvise(m_ServerProxy, DIID__IServerProxyEvents, unknown, FALSE, &m_dwCookie);建立连接.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值