我使用ATL编写了一个ActiveX控件,在一个网页中进行调用。我想在ActiveX中得到那个网页的URL,然后处理这个网页,但是如何在
ActiveX中得到网页的URL呢?

解决方案 »

  1.   

    用getparent得到父窗口的hwnd用shellwindow object可以得到当前系统中运行着的ie实例(返回一个internet explorer接口,这个接口可得到该窗口句柄,该实例的当前url),你把你用getparent得到的句柄跟从shellwindow中得到的句柄进行比较,如果相等就取那个实例的url
      

  2.   

    关于shellwindow对象的示例代码可从这贴子得到
    http://expert.csdn.net/Expert/topic/2944/2944033.xml?temp=.1045801
      

  3.   

    void CDoHTMLAppDlg::Spy()
    {
    SHDocVw::IShellWindowsPtr m_spSHWinds; CComPtr<IDispatch> spDispatch;
    CComQIPtr<IHTMLDocument2, &IID_IHTMLDocument2> pDoc2;
    CComPtr<IHTMLElement> pElement;
    CComPtr<IHTMLElementCollection> pElementCol;
    CComPtr<IHTMLFormElement> pFormElement;
    CComPtr<IHTMLInputTextElement> pInputElement;

        if(m_spSHWinds==NULL)
    {
    if(m_spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows))!=S_OK)
    {
    AfxMessageBox("失败...",MB_ICONINFORMATION);
                CoUninitialize();
    }
    }

    if(m_spSHWinds)
    {
    int n=m_spSHWinds->GetCount();
    for(int i=0;i<n;i++)
    {
    _variant_t v=(long)i;
    IDispatchPtr spDisp=m_spSHWinds->Item(v);
    SHDocVw::IWebBrowser2Ptr spBrowser(spDisp);//生成一个IE窗口的智能指针
    if(spBrowser)
    {
    //
    VARIANT id,index;
    if(SUCCEEDED(spBrowser->get_Document(&spDispatch)))
    pDoc2 = spDispatch;
    if(pDoc2!=NULL)
    {
    if(SUCCEEDED(pDoc2->get_forms(&pElementCol)))
    {
    long p=0;
    if(SUCCEEDED(pElementCol->get_length(&p)))
    if(p!=0)
    {
    for(long i=0;i<=(p-1);i++)
    {
    V_VT(&id)=VT_I4;
    V_I4(&id)=i;
    V_VT(&index)=VT_I4;
    V_I4(&index)=0;
    if(SUCCEEDED(pElementCol->item(id,index,&spDispatch)))
    if(SUCCEEDED(spDispatch->QueryInterface(IID_IHTMLFormElement,(void**)&pFormElement)))
    {
    long q=0;
    if(SUCCEEDED(pFormElement->get_length(&q)))
    for(long j=0;j<=(q-1);j++)
    {
    V_VT(&id)=VT_I4;
    V_I4(&id)=j;
    V_VT(&index)=VT_I4;
    V_I4(&index)=0;
    if(SUCCEEDED(pFormElement->item(id,index,&spDispatch)))
    if(SUCCEEDED(spDispatch->QueryInterface(IID_IHTMLInputTextElement,(void**)&pInputElement)))
    {
    CComBSTR value;
    CComBSTR type;
    pInputElement->get_type(&type);
    CString strtype(type);
    strtype.MakeUpper();
    if(strtype.Find("TEXT")!=-1)
    {
    pInputElement->get_value(&value);
    CString str(value);
    if(!str.IsEmpty())
    {
    m_List.AddString(_bstr_t("文本信息:")+_bstr_t(value));
    Log((LPCTSTR)(_bstr_t("文本信息:")+_bstr_t(value)));
    //AfxMessageBox(_bstr_t("文本信息:")+_bstr_t(value)+_bstr_t("\n"),MB_ICONINFORMATION);
    }
    }
    else if(strtype.Find("PASSWORD")!=-1)
    {
    pInputElement->get_value(&value);
    CString str(value);
    if(!str.IsEmpty())
    {
    m_List.AddString(_bstr_t("密码信息:")+_bstr_t(value));
    Log((LPCTSTR)(_bstr_t("密码信息:")+_bstr_t(value)));
    //AfxMessageBox(_bstr_t("密码信息:")+_bstr_t(value)+_bstr_t("\n"),MB_ICONINFORMATION);
    }
    }
    }
    }
    }
    }
    }
    }

    }
    //
    _bstr_t bsName=spBrowser->GetLocationName();//窗口名称
                    //AfxMessageBox(bsName,MB_ICONINFORMATION);
    spBrowser->AddRef();
                    void * pData=spBrowser;
    //AfxMessageBox((DWORD)(pData),MB_ICONINFORMATION);
    }
    }
    }
    }