代码之家  ›  专栏  ›  技术社区  ›  Christopher Tarquini

bho句柄onsubmit事件

  •  2
  • Christopher Tarquini  · 技术社区  · 16 年前

    基本上,我想开发一个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事件?

    1 回复  |  直到 16 年前
        1
  •  1
  •   i_am_jorf    16 年前

    一旦您有了指向要接收事件的元素的指针,您就可以 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);
        }
    }
    

    一些注释:

    1. 你可能想缓存 dwCookie cpc ,因为您稍后打电话时需要它们 pcp->Unadvise() 断开水槽。
    2. 在召唤 pcp->Advise() 上面,我通过了这个。您可以使用您拥有的任何实现对象 IDispatch ,可能是或不是此对象。设计留给你。
    3. riid 将是您要接收的事件DispInterface。在这种情况下,你可能想要 DIID_HTMLFormElementEvents2 .

    以下是断开连接的方法:

    pcp->Unadvise(dwCookie);
    

    如果您还有其他问题,请告诉我。

    编辑1:

    是的,那个迪伊德错了。应该是: DIID HTMLFormElements2 .

    以下是我的发现:

    C:\Program Files (x86)\Microsoft Visual Studio 8\VC\PlatformSDK>findstr /spin /c:"Events2" *.h | findstr /i /c:"form"