谢谢

解决方案 »

  1.   

    BOOL MyDlg::GetAllLinks(CMyHtmlView *pView)
    {
    IHTMLDocument2 * pDoc = NULL;
    IHTMLElementCollection  * pAllElem = NULL;
    IHTMLElement *pElem = NULL;
    IHTMLAnchorElement * pAnchor = NULL;
    IHTMLElement* ptext;
    long p;
    BSTR bstrSrc;
    VARIANT name;
    name.vt = VT_I4;

    try{
    pDoc = (IHTMLDocument2*)(pView->GetHtmlDocument());
    if(pDoc!=NULL)
    {
    pDoc->get_links(&pAllElem);

    if(pAllElem!=NULL)
    {
    pAllElem->get_length(&p);
    if(p>0)
    {
    for(int i=0; i<p; i++)
    {
    name.lVal = i;
    if(pAllElem->item(name, name, (LPDISPATCH*)&pElem)==S_OK)
    {
    if(pElem != NULL)
    {
    pElem->QueryInterface(&pAnchor);
    if(pAnchor!=NULL)
    {
    pAnchor->get_href(&bstrSrc);
    CString sHref(bstrSrc);  //url
    SysFreeString(bstrSrc);

    pAnchor->QueryInterface(&ptext);
    ptext->get_innerText(&bstrSrc);
    CString sText(bstrSrc); //text
    SysFreeString(bstrSrc);
    ptext->Release();

    pAnchor->Release();
    pAnchor = NULL;
    }
    pElem->Release();
    pElem = NULL;
    }
    }
    }
    }
    }
    }
    }catch(...){}

    try{
    if(pDoc != NULL)
    pDoc->Release();
    if(pAllElem != NULL)
    pAllElem->Release();
    }catch(...){}

    return TRUE;
    }