Basically I want to develop a BHO that validates certain fields on a form and auto-places disposable e-mails in the appropriate fields (more for my own knowledge). So in the DOCUMENTCOMPLETE event I have this:
for(long i = 0; i < *len; i++)
{
VARIANT* name = new VARIANT();
name->vt = VT_I4;
name->intVal = i;
VARIANT* id = new VARIANT();
id->vt = VT_I4;
id->intVal = 0;
IDispatch* disp = 0;
IHTMLFormElement* form = 0;
HRESULT r = forms->item(*name,*id,&disp);
if(S_OK != r)
{
MessageBox(0,L"Failed to get form dispatch",L"",0);// debug only
continue;
}
disp->QueryInterface(IID_IHTMLFormElement2,(void**)&form);
if(form == 0)
{
MessageBox(0,L"Failed to get form element from dispatch",L"",0);// debug only
continue;
}
// Code to listen for onsubmit events here...
}
How would I use the IHTMLFormElement interface to listen for the onsubmit event?
Once you have the pointer to the element you want to sink events for, you would QueryInterface()
it for IConnectionPointContainer
and then connect to that:
REFIID riid = DIID_HTMLFormElementEvents2;
CComPtr<IConnectionPointContainer> spcpc;
HRESULT hr = form->QueryInterface(IID_IConnectionPointContainer, (void**)&spcpc);
if (SUCCEEDED(hr))
{
CComPtr<IConnectionPoint> spcp;
hr = spcpc->FindConnectionPoint(riid, &spcp);
if (SUCCEEDED(hr))
{
DWORD dwCookie;
hr = pcp->Advise((IDispatch *)this, &dwCookie);
}
}
Some notes:
dwCookie
and cpc
, since you need them later when you call pcp->Unadvise()
to disconnect the sink.pcp->Advise()
above, I pass this. You can use any object you have that implements IDispatch
, which may or may not be this object. Design left to you.riid
will be the event dispinterface you want to sink. In this case, you probably want DIID_HTMLFormElementEvents2
.Here's how to disconnect:
pcp->Unadvise(dwCookie);
Let me know if you have further questions.
Edit-1:
Yeah, that DIID was wrong. It should be: DIID_HTMLFormElementEvents2
.
Here is how I found it:
C:\Program Files (x86)\Microsoft Visual Studio 8\VC\PlatformSDK>findstr /spin /c:"Events2" *.h | findstr /i /c:"form"