我正在运行BHO,我正在尝试使用HTMLDocumentEvents2捕获OnClick事件 然而,事件似乎永远不会到达处理程序
这就是我在做的事情:
class ATL_NO_VTABLE CBlastBhoBHO :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CBlastBhoBHO, &CLSID_BlastBhoBHO>,
public IObjectWithSiteImpl<CBlastBhoBHO>,
public IDispatchImpl<IBlastBhoBHO, &IID_IBlastBhoBHO, &LIBID_BlastBhoLib, /*wMajor =*/ 1, /*wMinor =*/ 0>,
public IDispEventImpl<0, CBlastBhoBHO, &DIID_HTMLDocumentEvents2, &LIBID_MSHTML, 1, 0>,
public IDispEventImpl<1, CBlastBhoBHO, &DIID_DWebBrowserEvents2, &LIBID_SHDocVw, 1, 1>
BEGIN_SINK_MAP(CBlastBhoBHO)
SINK_ENTRY_EX(0, DIID_HTMLDocumentEvents2, DISPID_HTMLDOCUMENTEVENTS2_ONCLICK, OnClickTrigger)
SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_DOCUMENTCOMPLETE, OnDocumentComplete)
END_SINK_MAP()
// Handlers
void STDMETHODCALLTYPE CBlastBhoBHO::OnDocumentComplete( IDispatch *pDisp, VARIANT *url) { // works fine }
void STDMETHODCALLTYPE CBlastBhoBHO::OnClickTrigger(IHTMLEventObj *pEvtObj) { // Never called }
// From SetSite()
ATL::IDispEventSimpleImpl<1,CBlastBhoBHO,&DIID_DWebBrowserEvents2>::DispEventAdvise(m_spWebBrowser);
// From OnDocumentComplete
CComQIPtr<IHTMLDocument3> doc_;
m_spWebBrowser->get_Document((IDispatch**) &doc_);
ATL::IDispEventSimpleImpl<0,CBlastBhoBHO,&DIID_HTMLDocumentEvents2>::DispEventAdvise(doc_);
我尝试了几种变体(使用HTMLElementEvents2等)或对不同对象提供建议或者对来自SetSite的文档提供建议。我的处理程序永远不会被调用。
非常感谢任何帮助。我只需要onClick事件。
答案 0 :(得分:1)
可能是因为LIBID_MSHTML的版本不匹配, 尝试将其更改为版本4.0,即更改所有
IDispEventImpl<0, CBlastBhoBHO, &DIID_HTMLDocumentEvents2, &LIBID_MSHTML, 1, 0>
到
IDispEventImpl<0, CBlastBhoBHO, &DIID_HTMLDocumentEvents2, &LIBID_MSHTML, 4, 0>
答案 1 :(得分:0)
尝试此功能 -
VARIANT_BOOL attachEvent( _bstr_t event, IDispatch *pdisp)
<强>例如: - 强>
MSHTML::IHTMLDocument3Ptr pDoc3 = 0;
pDoc.QueryInterface(IID_IHTMLDocument3,&pDoc3);
IDispatchEx evtClick;
pDoc3->attachEvent(_bstr_t(L"onclick"),&evtClick);