精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
锐英源精品开源心得,转载请注明:“锐英源www.wisestudy.cn,孙老师作品,电话13803810136。”需要全文内容也请联系孙老师。
If you have ever done any development work involving the use of COM objects, chances are you would have encountered the need for COM object event handling. Visual Basic users will know how simple it is to connect with the event interface(s) of the COM (or ActiveX) objects. The VB IDE lays out the event handling function codes nicely for the user. All the user has to do is to fill in the details of the event handling functions. 如果你有机会开发COM对象,会遇到事件处理。VB用户知道以COM(或ActiveX)事件接口来连接是如此简单的。VB的IDE为用户安排了整洁的事件处理代码。用户所要做的就是添加具体的代码。
For Visual C++ users, this is not always so straight forward. If your COM object happens to be an ActiveX control and you are using MFC, then yes, the Visual C++ IDE provides Wizards that can help you generate event handler function stubs. All the necessary codes (e.g. inserting the event sink map and event entry macros) are done automatically for you.对于VC用户,并不很直接。如果你的COM对象恰好是ActiveX控件,且你使用了MFC,那么是的,VC的IDE提供了向导,向导也能生成事件处理函数的存根。所有需要的代码(也就是说,添加事件sink映射和事件入口宏)对你来说是自动的。
But what if your COM object is not an ActiveX control? What if you are using straight COM objects which also fire events and you need to handle those events?但是如果你的COM对象不是一个ActiveX控件?如果你直接使用COM对象且对象同样触发事件和你需要处理事件时会怎样?
If you are an MFC user, you may want to tinkle with the various MFC macros to see if you can fit in an event handler function into your code either manually or via the Wizard. I personally believe this is possible. But you need to be armed with an intimate knowledge of MFC and its generated macros.如果你是MFC用户,你可能想变着法对不同的MFC宏看看,看它是否配合事件处理函数到你的代码里,或者变着法用手工或用向导。我个人相信,你有这个心思。但是你需要对MFC精通也要掌握MFC的宏。
If you do not use MFC, you may want to experiment with ATL codes (e.g. IDispEventImpl, BEGIN_SINK_MAP, SINK_ENTRY_EX, etc.) to perform event handling. The ATL macro codes are certainly not simple but they are well-documented in MSDN and they do provide standard handling mechanisms.如果你不想用MFC,你可能要精通ATL代码(也就是说IDispEventImpl,BEGIN_SINK_MAP,SINK_ENTRY_EX等等)来实现事件处理。ATL宏代码不可能简单,但是在MSDN里文档比较全,且他们确实提供了标准的处理机制。
In this article, I will go back to basics and seek to explain the fundamental principles of how event handling is done in COM. I will also provide a C+ class which serves as a basic and simple (at least in terms of code overhead) facilitator for COM Object Event Handling.本文会回到基本点,过一遍COM里事件处理怎样工作的基本原则。我也会提供一个C++类,虽然类基本且简单(至少提前以代码方式表示一下),但是它实现了此类COM对象。
I do this via a special custom-developed template class named TEventHandler which I have used in many projects. This class uses COM first principles and primitives and avoids the use of complicated macros. The sections following expound this class in detail. I assume that the reader is suitably conversant with C++, ATL and the concepts of template classes. However, before we start discussing the TEventHandler class, let us explore the fundamental principles of Event Handling in COM.通过一个特殊的定制开发模板类,命名为TEventHandler,这个类我在多个工程里用过了。这个类使用COM首要的原则,避免使用复杂的宏。后面会有详细介绍。假定读者要熟悉C++,ATL和模板类的概念。然后,开始讨论TEventHanler类前,让我们看看事件处理的基本原则。