如何用VC的ATL实现的word事件响应!?

xyz_mw 2004-09-16 01:54:38
wei.miao@trusdata.com 谢谢!
...全文
99 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
xyz_mw 2004-11-17
  • 打赏
  • 举报
回复
问题已经解决很久,不过还是谢谢!
kingzai 2004-11-16
  • 打赏
  • 举报
回复
http://www.itsoul.net/article/list.asp?id=27
ChinaAngely 2004-11-16
  • 打赏
  • 举报
回复
http://www.codeproject.com/com/adwordaddin.asp

Handling Events
Probably in your addin, you'd also be interested in handling some of Word's events.A case in point is the Application objects DocumentOpen event, with DISPID=4, which is handled here. Word has a complex object model and you will find a host of other such events. If you use the good old OLE/COM Object Viewer to view msword9.olb, you'd find IDL like :

....

[id(0x00000003), helpcontext(0x00061a83)]
void DocumentChange();
[id(0x00000004), helpcontext(0x00061a84)]
void DocumentOpen([in] Document* Doc);

......

As before we will use ATL's IDispEventSimpleImpl<> template class to implement our sink. For brevity, only the changes necessary to the earlier code has been mentioned.

extern _ATL_FUNC_INFO DocumentOpenInfo;

class ATL_NO_VTABLE CAddin :
public CComObjectRootEx,
public CComCoClass,
public ISupportErrorInfo,
public IDispatchImpl,
public IDispatchImpl<_IDTExtensibility2, &IID__IDTExtensibility2,
&LIBID_AddInDesignerObjects>,
public IDispEventSimpleImpl<1,CAddin,
&__uuidof(MSWord::ApplicationEvents2)>
{
public:
....
....

void __stdcall DocumentOpen(IDispatchPtr ptr)
{
CComQIPtr<_Document> spDoc(ptr);
ATLASSERT(spDoc);
....
....
}

BEGIN_SINK_MAP(CAddin)
SINK_ENTRY_INFO(1,__uuidof(MSWord::ApplicationEvents2),4,
DocumentOpen,&DocumentOpenInfo)
END_SINK_MAP()

private:
CComPtr<MSWord::_Application> m_spApp;
};

DocumentOpenInfo is defined at the top of CAddin.cpp as

_ATL_FUNC_INFO DocumentOpenInfo = {CC_STDCALL,VT_EMPTY,1,
{VT_DISPATCH|VT_BYREF}};

All that remains for us to do is to add the code to setup and break down the connection. Using the ATL template class, therefore all we have to do is call DispEventAdvise() and DispEventUnadvise(). Our CAddin's OnConnection() and OnDisconnection(), needless to say, is the right place for doing this.

CComQIPtr<_Application> spApp(Application);
ATLASSERT(spApp);
m_spApp = spApp;
HRESULT hr = DispEventAdvise(m_spApp);
if(FAILED(hr))
return hr;


and in OnDisconnection(),

DispEventUnadvise(m_spApp);
m_spApp = NULL;

5,139

社区成员

发帖
与我相关
我的任务
社区描述
其他开发语言 Office开发/ VBA
社区管理员
  • Office开发/ VBA社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧