在使用 ATL 的宏处理 DWebBrowserEvents2 时处理 HTMLElementEvents2

2022-01-14 00:00:00 com visual-studio-2008 c++ atl bho

我正在使用 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(以检测点击、滚动条等).为此,我查询 IHTMLElement 的 IHTMLDocument2 对象,查询 IConnectionPointContainer 的 QueryInterface() 并调用 IConnectionPointContainer::FindConnectionPoint(DIID_HTMLElementEvents2).(请参阅 msdn 的文章 处理 HTMLElementEvents2).问题是,当我在我的类中覆盖 IDispatch::Invoke 时,DWebBrowserEvents2 句柄(使用 ATL 宏创建)失败.有没有办法在不覆盖 Invoke 的情况下处理 HTMLElementEvents2,或者以只处理 HTMLElementEvents2 的方式实现调用?
谢谢,任何帮助将不胜感激.

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!

相关文章