BHO处理OnSubmit事件
-
07-07-2019 - |
题
基本上我想开发一个BHO来验证表单上的某些字段,并在适当的字段中自动放置一次性电子邮件(更多是为了我自己的知识)。所以在DOCUMENTCOMPLETE事件中我有这个:
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...
}
如何使用IHTMLFormElement接口侦听onsubmit事件?
解决方案
如果你有指向要为其接收事件的元素的指针,你将 QueryInterface()
它为 IConnectionPointContainer
,然后连接到它:
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);
}
}
一些注意事项:
- 您可能希望缓存
dwCookie
和cpc
,因为稍后当您调用pcp-&gt; Unadvise()
断开连接时需要它们水槽。 - 在上面的
pcp-&gt; Advise()
的调用中,我传递了这个。您可以使用任何实现IDispatch
的对象,该对象可能是也可能不是此对象。设计留给你。 -
riid
将是您要接收的事件调度接口。在这种情况下,您可能需要DIID_HTMLFormElementEvents2
。
醇>
以下是断开连接的方法:
pcp->Unadvise(dwCookie);
如果您还有其他问题,请与我们联系。
编辑-1:
是的,那个DIID错了。它应该是: DIID_HTMLFormElementEvents2
。
以下是我发现它的方式:
C:\Program Files (x86)\Microsoft Visual Studio 8\VC\PlatformSDK>findstr /spin /c:"Events2" *.h | findstr /i /c:"form"
不隶属于 StackOverflow