如何编程实现网页及其图片的下载?就象IE中的另存为差不多,将网页存在本地,同时网页内有关的连接也指向本地。

解决方案 »

  1.   

    HRESULT CApp::GetAllImgs()
    {
    HRESULT hr;
    if ((!m_pMSHTML)||(READYSTATE_COMPLETE != m_lReadyState))
    {
    ODS("Shouldn't get here 'til MSHTML changes readyState to READYSTATE_COMPLETE\n");
    //DebugBreak();
    return E_UNEXPECTED;
    }
    IHTMLElementCollection* p_imgColl = NULL;
    if (SUCCEEDED(hr = m_pMSHTML->get_all( &p_imgColl )))
    {
    long cElems=0;
    // retrieve the count of elements in the collection
    if (SUCCEEDED(hr = p_imgColl->get_length( &cElems )))
    {
    for ( int i=0; i<cElems; i++ )
    {
    VARIANT vIndex;
    vIndex.vt = VT_UINT;
    vIndex.lVal = i;
    VARIANT var2 = { 0 };
    LPDISPATCH pDisp;
    if (SUCCEEDED(hr = p_imgColl->item( vIndex, var2, &pDisp )))
    {
    bool b_print_tag=true;
    if(b_print_tag)
    {
    IHTMLImgElement* pImage = NULL; 
    if (SUCCEEDED(hr = pDisp->QueryInterface( IID_IHTMLImgElement, (LPVOID*)&pImage )))
    {
    BSTR bstr;
    pImage->get_src(&bstr);
    pImage->Release();
    b_print_tag=false;
    if (bstr)
    {
    SaveImg(bstr);
    SysFreeString(bstr);
    }
    }
    }
    pDisp->Release();
    } // item
    } // for
    } // get_length
    p_imgColl->Release();
    }
    IHTMLElementCollection* pColl = NULL; if (SUCCEEDED(hr = m_pMSHTML->get_all( &pColl )))
    {
    long cElems=0;
    // retrieve the count of elements in the collection
    if (SUCCEEDED(hr = pColl->get_length( &cElems )))
    {
    for ( int i=0; i<cElems; i++ )
    {
    VARIANT vIndex;
    vIndex.vt = VT_UINT;
    vIndex.lVal = i;
    VARIANT var2 = { 0 };
    LPDISPATCH pDisp;
    if (SUCCEEDED(hr = pColl->item( vIndex, var2, &pDisp )))
    {
    IHTMLInputElement* pInput=NULL; 
    if (SUCCEEDED(hr = pDisp->QueryInterface( IID_IHTMLInputElement, (LPVOID*)&pInput )))
    {
    BSTR bstr;
    pInput->get_src(&bstr);
    pInput->Release();
    if (bstr&&SysStringLen(bstr)>4)
    {
    SaveImg(bstr);
    SysFreeString(bstr);
    }
    }
    pDisp->Release();
    } // item
    } // for
    } // get_length
    pColl->Release();
    } // get_all
    // We're done so post ourselves a quit to terminate the message pump.
    PostQuitMessage(0);
    return hr;
    }
      

  2.   

    #import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF", "EndOfFile")
    #import <cdosys.dll> no_namespace rename("EOF", "EndOfFile")
    ............
    void CSavemhtDlg::OnOK() 
    {
    // save url as a single file, in fact I don't know if it is mht file, but it can be opened by IE, can someone tell me?
    CoInitialize(NULL);
    {
    IMessagePtr       iMsg(__uuidof(Message));
    IConfigurationPtr iConf(__uuidof(Configuration));
    iMsg->Configuration = iConf;
    try
    {
      iMsg->CreateMHTMLBody(
       "http://example.microsoft.com", 
       cdoSuppressNone,
       "domain\\username",
       "password");
    }
    catch(_com_error err)
    {
      // handle exception
    }
    _StreamPtr pStream=iMsg->GetStream();
    pStream->SaveToFile("test.mht",adSaveCreateOverWrite);
    }
    CoUninitialize();
    }
      

  3.   

    masterz(MS MVP) 您好,谢谢您的回复,我试了一下,
    为什么报出如下的错误:
    c:\HtmlDown\Debug\cdosys.tlh(255): error C2440: “初始化” : 无法从“unsigned short *”转换为“const BSTR”
    ...
    挺多的。