一、找到事件对用的DISPID
二、添加委托类
1 #pragma once
2 #include "oaidl.h"
3 #include <WinUser.h >
4 #include "testEventCtrl.h"
5 #define WM_NEWUSER 4
6 #define WM_CONNECT 6
7
8 class Skin :
9 public IDispatch
10 {
11 public:
12 Skin(void);
13 ~Skin(void);
14
15 CtestEventCtrl *pCtrl;
16
17 STDMETHODIMP Skin::QueryInterface(const struct _GUID &iid,void ** ppv)
18 {
19 *ppv=this;
20 return S_OK;
21 }
22
23 ULONG __stdcall Skin::AddRef(void)
24 { return 1; } // 做个假的就可以,因为反正这个对象在程序结束前是不会退出的
25
26 ULONG __stdcall Skin::Release(void)
27 { return 0; } // 做个假的就可以,因为反正这个对象在程序结束前是不会退出的
28
29 STDMETHODIMP Skin::GetTypeInfoCount(unsigned int *)
30 { return E_NOTIMPL; } // 不用实现,反正也不用
31
32 STDMETHODIMP Skin::GetTypeInfo(unsigned int,unsigned long,struct ITypeInfo ** )
33 { return E_NOTIMPL; } // 不用实现,反正也不用
34
35 STDMETHODIMP Skin::GetIDsOfNames(const IID &,LPOLESTR *,UINT,LCID,DISPID *)
36 { return E_NOTIMPL; } // 不用实现,反正也不用
37
38 STDMETHODIMP Skin::Invoke(
39 long dispID,
40 const struct _GUID &,
41 unsigned long,
42 unsigned short,
43 struct tagDISPPARAMS * pParams,
44 struct tagVARIANT *,
45 struct tagEXCEPINFO *,
46 unsigned int *)
47 { // 只需要实现这个就足够啦
48 switch(dispID) // 根据不同的dispID,完成不同的回调函数
49 {
50 case 4:
51 {
52 VARIANT varlValue;
53 long lValue = 0;
54 varlValue = (pParams->rgvarg)[0];
55 lValue = V_I4(&varlValue);
56
57 ::PostMessage(pCtrl->m_hWnd,WM_NEWUSER,NULL,lValue);
58 }
59 break;
60 case 6:
61
62 ::PostMessage(pCtrl->m_hWnd,WM_CONNECT,NULL,NULL);
63 break;
64
65 default:
66 break;
67 }
68 return S_OK;
69 }
70
71 };
三、控制类中接收派发的消息
1 BEGIN_MESSAGE_MAP(CtestEventCtrl, COleControl)
2 ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
3 ON_MESSAGE(WM_NEWUSER,OnNewUser)
4 ON_MESSAGE(WM_CONNECT,OnConnect)
5 END_MESSAGE_MAP()
四、查找连接点
1 IZKEMPtr m_ZkFace;
2
3 HRESULT hr = S_OK;
4 hr = m_ZkFace.CreateInstance(__uuidof(CZKEM));
5
6
7 if(hr==S_FALSE)
8 {
9 AfxMessageBox(_T("初始化失败!"));
10 return -1;
11 }
12
13
14 IConnectionPointContainer *pCPC;
15 hr = m_ZkFace->QueryInterface(IID_IConnectionPointContainer,(void **)&pCPC);
16 if(!SUCCEEDED(hr))
17 {
18 return -1;
19 }
20 IConnectionPoint *pCP;
21 const IID iIID=__uuidof(_IZKEMEvents);
22 hr = pCPC->FindConnectionPoint(iIID,&pCP);
23 //获取连接点
1 if(!SUCCEEDED(hr) )
2 {
3 return -1;
4 }
5 pCPC->Release();
6
7 IUnknown *pSinkUnk;
8 Skin *pSink = new Skin();
9 pSink->pCtrl=this;
10 hr = pSink->QueryInterface(IID_IUnknown,(void **)&pSinkUnk);
11 DWORD dwAdvise;
12 hr = pCP->Advise(pSinkUnk,&dwAdvise);//注册其接收接口指针