如何在C ++ XE2中监听使用IWebBrowser2运行IE的事件? [英] How do I listen the event of Running IE with IWebBrowser2 in C++ XE2?

查看:434
本文介绍了如何在C ++ XE2中监听使用IWebBrowser2运行IE的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我想自动运行的IE。我已经成功附加运行IE使用下面的代码(我假设一个标签内只有一个IE)

Currently I want to automate the running IE. I have successfully attached the running IE using below code (I assume there is only one IE within one tab)

#include "atl/atlbase.h"
#include <exdisp.h>
#include <mshtml.h>
CComQIPtr<IWebBrowser2> pCurIE;    
void __fastcall TForm4::Button3Click(TObject *Sender)
{
    bool SuccessToHook = false;
    CComPtr<IShellWindows> m_spSHWinds;
    if (FAILED(m_spSHWinds.CoCreateInstance( __uuidof( ShellWindows)))){

        return ;
    }


    LONG nCount;
    m_spSHWinds->get_Count( &nCount);
    ShowMessage(nCount);
    for (int i = 0; i < nCount; i++) {
        CComPtr<IDispatch> pDisp;
        m_spSHWinds->Item( CComVariant(i), &pDisp);
        CComQIPtr<IWebBrowser2> pIE(pDisp);
        if (pIE == NULL){

            continue ;
        }
        CComPtr<IDispatch> pDispDoc;
        pIE->get_Document(&pDispDoc);
        CComQIPtr<IHTMLDocument2> pHtmlDoc(pDispDoc);
        if (pHtmlDoc){
            pCurIE = pIE;
            SuccessToHook = true;
            break ;
        }
    }
    ShowMessage(SuccessToHook ? "Success to hook." : "Failed to hook." );
}



现在我可以控制当前正在运行的IE浏览和读取当前状态。但因为我想显示消息时,像onDocumentComplete事件触发事件。我不知道如何听我的当前代码事件。一个简单的示例代码与BCB将非常感谢,因为有一些样品与VC ++,但我的项目是在C + + XE2。

Now I can control the current running IE like navigating and read the current state. But as I want to show messages when the events like onDocumentComplete Event are fired. I have no idea how to listen the event following my current code. A simple sample code with BCB will be very appreciated as there are some samples with VC++ but my project is on C++ XE2.

感谢@Remy Lebeau和这个链接,我终于解决了我的问题。
我在这里留下我的代码,希望对其他人有帮助。

Thanks @Remy Lebeau and this link, I finally solve my problem. I leave my code here and hope it maybe helpful for someone else.

从TEventDispatcher派生的类

A class derived from TEventDispatcher

#include <exdisp.h>
#include <exdispid.h>
#include <mshtml.h>
#include <mshtmdid.h>
#include <utilcls.h>
//---------------------------------------------------------------------------
class TForm4;
class EventHandler:public TEventDispatcher<EventHandler,&DIID_DWebBrowserEvents2>{
    private:
        bool connected;
        TForm4 *theform;
        IUnknown* server;
    protected:
        HRESULT InvokeEvent(DISPID id, TVariant *params){
            switch(id){
                case DISPID_DOCUMENTCOMPLETE:
                    ShowMessage("On Document Complete");
                    break;
                default:
                    break;
            }
        }
    public:
        EventHandler(){
            connected = false; //not connected;
            theform = false; //backptr to form is null
        }
        ~EventHandler(){
            if (connected)
                Disconnect();
        }
        void Connect(TForm4 *form,  IUnknown* srv){
            server = srv;
            theform = form; //back pointer to form to do stuff with it.
            server->AddRef(); //addref the server
            ConnectEvents(server);
        }
        void Disconnect(){
            DisconnectEvents(server);   //disconnect the events
            server->Release();
        }
};

开始聆听

void __fastcall TForm4::Button5Click(TObject *Sender)
{
    Event = new EventHandler();
    Event->Connect(this, pCurIE);
}

停止聆听

void __fastcall TForm4::Button6Click(TObject *Sender)
{
    Event->Disconnect();
}


推荐答案

在您的代码中实现 DWebBrowserEvents2 接口。然后您可以查询浏览器的 IConnectionPointContainer 接口,调用 IConnectionPointContainer :: FindConnectionPoint()方法, code>与 DWebBrowserEvents2 对应的IConnectionPoint ,并调用 IConnectionPoint :: Advise() method传递给你的类的一个实例。完成使用这些事件后,不要忘记调用 IConnectionPoint :: Unadvise()

You have to write a class in your code that implements the DWebBrowserEvents2 interface. Then you can query the browser for its IConnectionPointContainer interface, call the IConnectionPointContainer::FindConnectionPoint() method to find the IConnectionPoint that cooresponds to DWebBrowserEvents2, and call the IConnectionPoint::Advise() method passing it an instance of your class. Don't forget to call IConnectionPoint::Unadvise() when you are done using the events.

你可以从VCL的 TEventDispatcher 在utilcls.h中派生类。其 ConnectEvents() DisconnectEvents()方法处理 IConnectionPoint 给你的东西。然后,您将覆盖抽象 InvokeEvent()方法(每个浏览器的事件都有自己的 DISPID 值)。

To help you with that, you can derive your class from the VCL's TEventDispatcher class in utilcls.h. Its ConnectEvents() and DisconnectEvents() methods handle the IConnectionPoint stuff for you. You would then just override the abstract InvokeEvent() method (each of the browser's events have their own DISPID values).

这篇关于如何在C ++ XE2中监听使用IWebBrowser2运行IE的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆