c++ - BHO 处理 OnSubmit 事件

标签 c++ internet-explorer events bho

基本上,我想开发一个 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);
    }
}

一些注意事项:

  1. 您可能想要缓存 dwCookiecpc,因为稍后调用 pcp->Unadvise() 断开连接时需要它们下沉。
  2. 在上面对 pcp->Advise() 的调用中,我传递了这个。您可以使用任何实现 IDispatch 的对象,它可能是也可能不是这个对象。设计留给您。
  3. 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/

相关文章:

css - IE11 按钮文本溢出按钮,应该扩大按钮宽度,如果太大则移动到新行

javascript - Angular 按键事件的问题

c++ - 在 C++ 中,您可以先构造子成员,然后在父构造函数中使用吗?

c++ - 混淆段错误

c++ - QGLBuffer::map 返回 NULL?

c++ - 是否可以从适应的结构生成融合图?

css - 带 CSS 显示的元素 : none; breaking layout - causing misalignment

jquery - getJSON Facebook 调用在 IE 中不起作用

linux - 网络流量的软件中断

google-maps - 如何为 google maps api v3 对象创建自定义事件?