基本上,我想开发一个 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 接口(interface)来监听 onsubmit 事件?
最佳答案
一旦您有了指向要为其接收事件的元素的指针,您将为 IConnectionPointContainer
QueryInterface()
,然后连接到该元素:
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->Unadvise()
断开连接时需要它们下沉。 - 在上面对
pcp->Advise()
的调用中,我传递了这个。您可以使用任何实现IDispatch
的对象,它可能是也可能不是这个对象。设计留给您。 riid
将是您要接收的事件调度接口(interface)。在这种情况下,您可能需要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"
关于c++ - BHO 处理 OnSubmit 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1418476/