1

IE can fire onscroll event when using Document Mode: IE7 or IE8, but fail to fire onscroll event when using Document Mode: IE9. I also register the onscroll event on documentElement, it reacts the same way.

The BHO class uses:

public IDispEventImpl<3, CHelloWorldBHO, &DIID_HTMLWindowEvents2, &LIBID_MSHTML, 4, 0>. 

And sink the event:

BEGIN_SINK_MAP(CHelloWorldBHO)
SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_DOCUMENTCOMPLETE, OnDocumentComplete)
SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_NAVIGATECOMPLETE2, OnNavigateComplete2)
SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_WINDOWSTATECHANGED, OnWindowStateChanged)
SINK_ENTRY_EX(3, DIID_HTMLWindowEvents2, DISPID_HTMLWINDOWEVENTS2_ONSCROLL, OnScroll)
END_SINK_MAP()

Then Advise it to the window object in Document Complete:

CComQIPtr<IHTMLDocument2> spHTMLDoc = spDispDoc;
CComQIPtr<IHTMLWindow2> spTempWindow2;
spHTMLDoc->get_parentWindow(&spTempWindow2);

IDispEventImpl<3, CHelloWorldBHO, &DIID_HTMLWindowEvents2, &LIBID_MSHTML, 4, 0>::DispEventAdvise(spTempWindow2);

I have been working on this problem for quite a while. Any ideas? Thanks so much!

1 Answer 1

4

With IE9 and document mode = IE9 you need to use the "new" Events.

hr = _spDocument->get_parentWindow(reinterpret_cast<IHTMLWindow2 **>(&_spWindow));
if (SUCCEEDED(hr) && _spWindow)
{
    // If document mode is lower than 9 we can had the OnScrollEvent the old way
    if(_uiDocumentMode < 9)
    {
        hr = IDispEventImpl<2, CIEPage, &DIID_HTMLWindowEvents2, &LIBID_MSHTML, 4, 0>::DispEventAdvise(_spWindow);                      
        if(SUCCEEDED(hr))
        {
            ...
        }
    }
    else
    {
        CComPtr<IEventTarget> spIEventTarget;
        hr = _spWindow->QueryInterface(IID_IEventTarget, reinterpret_cast<void **>(&spIEventTarget));   
        if (SUCCEEDED(hr) && spIEventTarget)
        {   
            _spEventScroll = new CIE9UIEvent(); // This class must derive from IDispatchEx

            CComPtr<IDispatch> spIDispatch;
            HRESULT hr = _spEventScroll->QueryInterface(IID_IDispatch, reinterpret_cast<void **>(&spIDispatch)); // Get the IDispatch
            if (SUCCEEDED(hr) && spIDispatch)
            {
                // If _spEventScroll is used instead of spIDispatch, an exception will occurr!!!
                hr = _spIEventTarget->addEventListener(_bstr_t("scroll"), spIDispatch,  VARIANT_TRUE);
                if (SUCCEEDED(hr))
                {
                    ...
                }
            }
        }
    }
}

Now on your derived class...

//////////////////////////////////////////////////////////////////////
// InvokeEx
//////////////////////////////////////////////////////////////////////
HRESULT STDMETHODCALLTYPE CIE9UIEvent::InvokeEx(             
            __in  DISPID dispIdMember,            
            __in  LCID lcid,            
            __in  WORD wFlags,            
            __in  DISPPARAMS* pDispParams,            
            __out_opt  VARIANT *pvarRes,            
            __out_opt  EXCEPINFO *pei,            
            __in_opt  IServiceProvider *pspCaller)
{
    if(dispIdMember == 0 && pDispParams->cArgs == 2)
    {   
        if(pDispParams->rgvarg[1].vt == VT_DISPATCH && pDispParams->rgvarg[1].pdispVal)
        {
            CComPtr<IDOMEvent> spIDOMEvent;
            HRESULT hr = pDispParams->rgvarg[1].pdispVal->QueryInterface(IID_IDOMEvent, reinterpret_cast<void **>(&spIDOMEvent));
            if(SUCCEEDED(hr) && spIDOMEvent)
            {
                ...
            }
        }
    }

    return S_OK;
}

Last step is to add in your class QueryInterface method the IDispatchEx

//////////////////////////////////////////////////////////////////////
// QueryInterface
//////////////////////////////////////////////////////////////////////
HRESULT STDMETHODCALLTYPE CIE9EventListener::QueryInterface(REFIID riid, __RPC__deref_out void __RPC_FAR *__RPC_FAR *ppvObject)
{
    *ppvObject = NULL;
    if(IsEqualGUID(riid, IID_IUnknown))
        *ppvObject = reinterpret_cast<void**>(this);

    if(IsEqualGUID(riid, IID_IDispatch))
        *ppvObject = reinterpret_cast<void**>(this);

    if(IsEqualGUID(riid, IID_IDispatchEx))
        *ppvObject = reinterpret_cast<void**>(this);


    if(*ppvObject)
    {
        ((IUnknown*)*ppvObject)->AddRef();
        return S_OK;
    }

    return E_NOINTERFACE;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.