Handling HTML events using MFC/C++
From: Gelmir Tinehtelë (gt_at_nospam.com)
Date: 05/26/04
- Next message: Gelmir Tinehtelë: "Handling HTML events using MFC/C++"
- Previous message: Ravinder: "Hlep One More Time Modeless Dialog"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 26 May 2004 17:30:54 +0200
Hello guys!
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;
}
- Next message: Gelmir Tinehtelë: "Handling HTML events using MFC/C++"
- Previous message: Ravinder: "Hlep One More Time Modeless Dialog"
- Messages sorted by: [ date ] [ thread ]