Nobody know how to handle HTML events using MFC/C++?

From: Gelmir Tinehtelë (gt_at_nospam.com)
Date: 05/28/04


Date: Fri, 28 May 2004 13:29:58 +0200

Hello guys, my apologizes for resending this post again, but my question
remains unanswered, just like the problem not resolved :-( Maybe this is not
proper group for that kind of question? If not, please indicate appropriate
one.
I attached my original post below, please reply.

Post from 2004-05-06:

I have following situation: I created application and embedded Microsoft Web
Browser ActiveX, so I can modify HTML content, iterate through element
collections (suppose I have button with id="myButton" - I already can change
its caption, change elements, pictures, etc.).

The problem is that I can not capture events from within the ActiveX control
(here: Web Browser). There is a HTMLElementEvent2 interface, but I don't
know how to create my own implementation of Event Sink. Please look at code
below and help me with this, or point to proper paper or sample code. Help
me please, I have deadline incoming... All I need is to handle "onclick"
event inside the browser.
The code is taken from MSDN, from MSHTML Tutorial:

Everything works perfect till ConnectEvents function; I have
IHTMLElementCollection, but how to put it together with
CMyClass::ConnectEvents(IHTMLElement* pElem) and CEventSink::Invoke?
Help me please.

Best!

// Ok, in this function I can retrieve all relevant data, particulary
IHTMLElement* pElem - works fine. But what about ConnectEvents(pElem)
method?
void CMyClass::ProcessElementCollection(IHTMLElementCollection* pElemColl)
{
    IDispatch* pElemDisp = NULL;
    IHTMLElement* pElem = NULL;
    _variant_t varID("myID", VT_BSTR);
    _variant_t varIdx(0, VT_I4);

    hr = pElemColl->item(varID, varIdx, &pElemDisp);

    if (SUCCEEDED(hr))
    {
        hr = pElemDisp->QueryInterface(IID_IHTMLElement, (void**)&pElem);

        if (SUCCEEDED(hr))
        {
            // Obtained element with ID of "myID".
            ConnectEvents(pElem);
            pElem->Release();
        }

        pElemDisp->Release();
    }
}//Okay, maybe this would work, but what then?void
CMyClass::ConnectEvents(IHTMLElement* pElem)
{
    HRESULT hr;
    IConnectionPointContainer* pCPC = NULL;
    IConnectionPoint* pCP = NULL;
    DWORD dwCookie;

    // Check that this is a connectable object.
    hr = pElem->QueryInterface(IID_IConnectionPointContainer,
(void**)&pCPC);

    if (SUCCEEDED(hr))
    {
        // Find the connection point.
        hr = pCPC->FindConnectionPoint(DIID_HTMLElementEvents2, &pCP);

        if (SUCCEEDED(hr))
        {
            // Advise the connection point.
            // pUnk is the IUnknown interface pointer for your event sink
            hr = pCP->Advise(pUnk, &dwCookie);

            if (SUCCEEDED(hr))
            {
                // Successfully advised
            }

            pCP->Release();
        }

        pCPC->Release();
    }
}

//Comment from MSDN:

The following sample code demonstrates how you would detect the firing of an
HTMLElementEvents2::onclick event in your implementation of
IDispatch::Invoke.

//Actually, WHAT IS THIS?! How to implement that?

STDMETHODIMP CEventSink::Invoke(DISPID dispidMember,
                                REFIID riid,
                                LCID lcid,
                                WORD wFlags,
                                DISPPARAMS* pdispparams,
                                VARIANT* pvarResult,
                                EXCEPINFO* pexcepinfo,
                                UINT* puArgErr)
{
    switch (dispidMember)
    {
        case DISPID_HTMLELEMENTEVENTS2_ONCLICK:
        OnClick();
        break;

        default:
        break;
    }

    return S_OK;
}