基本上,我想开发一个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事件?
发布于 2009-09-13 18:42:04
一旦你有了指向你想要接收事件的元素的指针,你就可以为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的任何对象,它可能是也可能不是此对象。留给you.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"https://stackoverflow.com/questions/1418476
复制相似问题