当使用 ATL 的宏处理 DWebBrowserEvents2 时处理 HTMLElementEvents2 [英] Handle HTMLElementEvents2 when DWebBrowserEvents2 has been handled using ATL's macros

查看:22
本文介绍了当使用 ATL 的宏处理 DWebBrowserEvents2 时处理 HTMLElementEvents2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 VS2008、C++ 创建一个浏览器帮助程序对象.我的类是从 IDispEventImpl 派生出来的

I'm creating a Browser Helper Object using VS2008, C++. My class has been derived from IDispEventImpl among many others

class ATL_NO_VTABLE CHelloWorldBHO :
    public CComObjectRootEx<CComSingleThreadModel>,
    public CComCoClass<CHelloWorldBHO, &CLSID_HelloWorldBHO>,
    public IObjectWithSiteImpl<CHelloWorldBHO>,
    public IDispatchImpl<IHelloWorldBHO, &IID_IHelloWorldBHO, &LIBID_HelloWorldLib, /*wMajor =*/ 1, /*wMinor =*/ 0>,
    public IDispEventImpl<1, CHelloWorldBHO, &DIID_DWebBrowserEvents2, &LIBID_SHDocVw, 1, 1>

{
.
.
.
BEGIN_SINK_MAP(CHelloWorldBHO)
     SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_DOCUMENTCOMPLETE, OnDocumentComplete)
     SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_BEFORENAVIGATE2, BeforeNavigate2)//Handle BeforeNavigate2
END_SINK_MAP()
.
.
.
}

从上面的代码可以明显看出,我的 DWebBrowserEvents2 是使用 ATL 的宏处理的.现在我想处理 HTMLElementEvents2(检测点击、滚动条等)为此,我 QueryInterface() IHTMLElement 的 IHTMLDocument2 对象,IConnectionPointContainer 的 QueryInterface() 并调用 IConnectionPointContainer::FindConnectionPoint(DIID_HTMLElementEvents2).(请参阅 msdn 的文章关于处理 HTMLElementEvents2).问题是,当我在我的类中覆盖 IDispatch::Invoke 时,DWebBrowserEvents2 句柄(使用 ATL 宏创建)失败.有没有办法在不覆盖 Invoke 的情况下处理 HTMLElementEvents2,或者以只处理 HTMLElementEvents2 的方式实现 invoke?
谢谢,任何帮助将不胜感激.

As apparent from the code above, my DWebBrowserEvents2 are handled using the ATL's macros. Now I want to handle HTMLElementEvents2 (to detect clicks, scrollbars, etc.) For that, I QueryInterface() the IHTMLDocument2 object for IHTMLElement, QueryInterface() that for IConnectionPointContainer and call IConnectionPointContainer::FindConnectionPoint(DIID_HTMLElementEvents2). (See msdn's article on handling HTMLElementEvents2). The problem is, when I overwrite IDispatch::Invoke in my class, the DWebBrowserEvents2 handles (created using ATL macros) fail. Is there a way to handle HTMLElementEvents2 without overwriting Invoke, or implement invoke in such a way that it only handles HTMLElementEvents2?
Thanks, Any help will be appreciated.

推荐答案

实际上没有必要重写 Invoke 或获取 IConnectionPointContainer.由于这是一个 ATL 项目,实现另一个 IDispEventImpl:

There is no real need to override Invoke or get IConnectionPointContainer. Since this is an ATL project, Implementing another IDispEventImpl:

public IDispEventImpl<2, CHelloWorldBHO, &DIID_HTMLTextContainerEvents2, &LIBID_MSHTML, 4, 0>

可以解决问题.然后,将条目下沉为:

does the trick. Then, sink the entry as:

SINK_ENTRY_EX(2, DIID_HTMLTextContainerEvents2, DISPID_ONSCROLL, OnScroll)

在 OnDocumentComplete 中,调用 IWebBrowser2::get_Document、IHTMLDocument2::get_body,然后调用 DispEventAdvise 开始接收事件.

In OnDocumentComplete, call IWebBrowser2::get_Document, IHTMLDocument2::get_body, and then call DispEventAdvise to start receiving events.

请注意,我使用了 DIID_HTMLTextContainerEvents2 而不是 DIID_HTMLElementEvents.那是因为 body 对象不支持 HTMLElementEvents2,而且就我的目的(处理滚动)而言,这很好用!

Note that I've used DIID_HTMLTextContainerEvents2 instead of DIID_HTMLElementEvents. That's because the body object does not support HTMLElementEvents2, and for my purpose (to handle scrolling) this works just fine!

这篇关于当使用 ATL 的宏处理 DWebBrowserEvents2 时处理 HTMLElementEvents2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆