MFC IE 事件挂载

来源:互联网 发布:北京网络安全员招聘网 编辑:程序博客网 时间:2024/05/01 02:42

void CIEToolhelperDlg::GetIWebBrowser2_3(CString url)
{
     if (SUCCEEDED(OleInitialize(NULL)))
        {
        CComPtr<IConnectionPointContainer> spConnectionPointer;  
        CComPtr<IConnectionPoint> spConnp;
    //    CComQIPtr < IWebBrowser2 > spWebBrowser2;
        IWebBrowser2 * spWebBrowser3;
        CComPtr < IUnknown > spUnk;
        spUnk.CoCreateInstance(CLSID_InternetExplorer,spUnk, CLSCTX_LOCAL_SERVER);
           CoCreateInstance(CLSID_InternetExplorer, NULL, CLSCTX_LOCAL_SERVER,
                               IID_IWebBrowser2, (void**)&spWebBrowser3);


        if(spWebBrowser3)
        {
                 VARIANT vEmpty;
               VariantInit(&vEmpty);

               BSTR bstrURL = url.AllocSysString();

               HRESULT hr = spWebBrowser3->Navigate(bstrURL, &vEmpty, &vEmpty, &vEmpty, &vEmpty);
               if (SUCCEEDED(hr))
               {
                   spWebBrowser3->put_Visible(VARIANT_TRUE);
               }
               else
               {
                    spWebBrowser3->Quit();
               }
        }
                // ieevent=new IEEventInterface;
                // pSink=new EventSink;
               //     pSink->m_ie=spWebBrowser3;
                 this->m_sink2=new EventSink2;
                 this->m_sink2->m_ie=spWebBrowser3;

            //    IID iid = IID_IUnknown;
             //    LPUNKNOWN pUnkSink=this->pSink->GetIDispatch(FALSE);
                 LPUNKNOWN pUnkSink=this->m_sink2->GetIDispatch(FALSE);
             if(!AfxConnectionAdvise(spWebBrowser3, DIID_DWebBrowserEvents2, pUnkSink, FALSE, &dwCookie))
               {
                  this->MessageBox("connect to IE event faild");
                 }   
          //    spWebBrowser2.Release();


                 ///
        
            
                 ///
    //      OleUninitialize();
        }
    
}



////////////sink



BEGIN_DISPATCH_MAP(EventSink2, CCmdTarget)
    //{{AFX_DISPATCH_MAP(EventSink2)
        // NOTE - the ClassWizard will add and remove mapping macros here.
  DISP_FUNCTION_ID(EventSink2,"DocumentComplete",DISPID_DOCUMENTCOMPLETE,DocumentComplete,VT_EMPTY,VTS_DISPATCH   VTS_PVARIANT)
    DISP_FUNCTION_ID(EventSink2,"onclick", DISPID_HTMLELEMENTEVENTS2_ONCLICK,OnClick,VT_VARIANT,VTS_DISPATCH)

    //}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()

// Note: we add support for IID_IEventSink2 to support typesafe binding
//  from VBA.  This IID must match the GUID that is attached to the
//  dispinterface in the .ODL file.

// {0BBBA45B-EF3F-423F-BBCD-9ED9697F74A4}
static const IID IID_IEventSink2 =
{ 0xbbba45b, 0xef3f, 0x423f, { 0xbb, 0xcd, 0x9e, 0xd9, 0x69, 0x7f, 0x74, 0xa4 } };

BEGIN_INTERFACE_MAP(EventSink2, CCmdTarget)
    INTERFACE_PART(EventSink2, IID_IEventSink2, Dispatch)
END_INTERFACE_MAP()

/////////////////////////////////////////////////////////////////////////////
// EventSink2 message handlers

void EventSink2::DocumentComplete(IDispatch *pDisp, VARIANT *URL)
{
    HRESULT hr;
    IUnknown* pUnkBrowser = NULL;
    IUnknown* pUnkDisp = NULL;
    IDispatch* pDocDisp = NULL;
    IHTMLDocument2* pDoc = NULL;

    // Is this the DocumentComplete event for the top frame window?
    // Check COM identity: compare IUnknown interface pointers.
    hr = m_ie->QueryInterface(IID_IUnknown, (void**)&pUnkBrowser);

    if (SUCCEEDED(hr))
    {
    
        hr = pDisp->QueryInterface(IID_IUnknown, (void**)&pUnkDisp);

        if (SUCCEEDED(hr))
        {
            
            if (pUnkBrowser == pUnkDisp)
            {
            
                // This is the DocumentComplete event for the top frame.
                // This page is loaded, so we can access the DHTML Object Model.
                hr = m_ie->get_Document(&pDocDisp);

                if (SUCCEEDED(hr))
                {
                    // Obtained the document object.
                    pDocDisp->QueryInterface(IID_IHTMLDocument2, (void**)&pDoc);
                    if (SUCCEEDED(hr))
                    {
                        // Obtained the IHTMLDocument2 interface for the document object
                        ProcessDocument(pDoc);
                        //AfxMessageBox(_T("finish....."));
                    }

                    pDocDisp->Release();
                }
            }

            pUnkDisp->Release();
        }

        pUnkBrowser->Release();
    }




}

void EventSink2::ProcessDocument(IHTMLDocument2 *pDoc)
{
     HRESULT hr;
    IHTMLElementCollection* pElemColl = NULL;

    hr = pDoc->get_all(&pElemColl);
    if (SUCCEEDED(hr))
    {
        // Obtained element collection.
        ProcessElementCollection(pElemColl);
        pElemColl->Release();
    }

}

void EventSink2::ProcessElementCollection(IHTMLElementCollection *pElemColl)
{
    HRESULT hr;
    IDispatch* pElemDisp = NULL;
    IHTMLElement* pElem = NULL;
    _variant_t varID("su");
    _variant_t varIdx(0L);

    hr = pElemColl->item(varID, varIdx, &pElemDisp);

    if (SUCCEEDED(hr))
    {
        hr = pElemDisp->QueryInterface(IID_IHTMLElement, (void**)&pElem);

        if (SUCCEEDED(hr))
        {
            // Obtained element with ID of "myID".
            ConnectEvents(pElem);
            pElem->Release();
        }

        pElemDisp->Release();
    }

}

BOOL EventSink2::OnClick(IHTMLEventObj *pEvtObj)
{
       ::AfxMessageBox("Button clicked!");
        return TRUE;


}

void EventSink2::ConnectEvents(IHTMLElement *pElem)
{
    HRESULT hr;
    IConnectionPointContainer* pCPC = NULL;
    IConnectionPoint* pCP = NULL;
    DWORD dwCookie;

    // Check that this is a connectable object.
    hr = pElem->QueryInterface(IID_IConnectionPointContainer, (void**)&pCPC);

    if (SUCCEEDED(hr))
    {
        // Find the connection point.
        hr = pCPC->FindConnectionPoint(DIID_HTMLElementEvents2, &pCP);

        if (SUCCEEDED(hr))
        {
            // Advise the connection point.
            // pUnk is the IUnknown interface pointer for your event sink
            hr = pCP->Advise(this->GetIDispatch(FALSE), &dwCookie);
         if (SUCCEEDED(hr))
            {
                // Successfully advised
            }

            pCP->Release();
        }

        pCPC->Release();
    }

}