动态修改网页之后,如何把IHTMLDocument2中内容保存到文件?---------------(现在无法把修改结果保存下来)

解决方案 »

  1.   

    QueryInterface for IPersistFile
    IPersistFile->Save();
      

  2.   

    hr=piDoc->QueryInterface(IID_IPersistFile, (void**)piPF);
    if(SUCCEEDED(hr))
    {
     piPF->Save(OLESTR("1.htm"), TRUE);
     piPF->Release();
    }
      

  3.   

    to: NDY_W(carpe diem)------   谢谢你提供的思路!
       不过,这样子只能把修改之前的 html源码保存;无法把经过修改的结果也保存下来!
    =================================================================================  IHTMLElement *pActiveElement = NULL;
      hr = pHTMLDocument2->get_activeElement(&pActiveElement);
      if(hr == S_OK && pActiveElement != NULL)
      {
    //AfxMessageBox("get the activeElement"); BSTR bstr;
    CString str;
    CString msg;
    //innerHTML
    hr = pActiveElement->get_innerHTML(&bstr);
    if(hr == S_OK)
    {
    //get content of active element
    str = bstr;
    msg = "innerHTML: " + str ;
    AfxMessageBox(msg); 
    }
    //outerHTML
    hr = pActiveElement->get_outerHTML(&bstr);
    if(hr == S_OK)
    {
    str = bstr;
    msg = "\nouterHTML: " + str;
    AfxMessageBox(msg); 
    }
    //id
    hr = pActiveElement->get_id(&bstr);
    if(hr == S_OK)
    {
        str = bstr;
        msg = "\nElementID: " + str;
        AfxMessageBox(msg); 
    }
    //set content of active element
    BSTR bstrId;
    CString strId("mytest");
    bstrId = strId.AllocSysString(); hr = pActiveElement->put_id(bstrId);
    ::SysFreeString(bstrId);
    if(hr == S_OK)
    {
    AfxMessageBox("put content succeed!"); }
    ==================================================================================
    (1)经过修改之后,当前ActiveElement的id应该变成了 “mytest“ 了吧!!!!!
    (2)接着我就是需要把这个结果保存下来,到某个新文件中去!==================================================================================
    ,请执教,一定给分的
      

  4.   

    将webbrowser设置为设计模式,然后修改,即可保存修改后的结果。
    处理 IDispatch::Invoke
    STDMETHODIMP CYourClientSite::Invoke(DISPID dispid,
     REFIID riid,
     LCID lcid,
     DISPPARAMS ...
     VARIANT *pv,
     ...)
    {
     if(dispid == DISP_AMBIENT_USERMODE)
     {
       V_VT(pv)=VT_BOOL;
       V_BOOL(pv)=FALSE;
       return NOERROR;
     }
     ...
    }
      

  5.   

    如果也要工作于usermode,在状态切换时用
    IOleControl::OnAmbientPropertyChange(DISPID_AMBIENT_USERMODE)通知webbrowser
      

  6.   


    这样做, 打开网页编辑模式然后保存就可以了:
    在.h中:
            IHTMLDocument2* m_pMSHTML;在.cpp中:
            if (m_pMSHTML != NULL ) {
                    VARIANT_BOOL bDisp;
                    VARIANT var;
                    VARIANT_BOOL bRet;                bDisp = VARIANT_FALSE;
                    var.vt = VT_BSTR;
                    var.bstrVal = _bstr_t("e:\\test\\test.htm");
                    bRet = VARIANT_FALSE;                m_pMSHTML->execCommand( _bstr_t("SaveAs"), bDisp, var, &bRet );                return;
            }
      

  7.   

    sorry, 忘记在保存的前后要加上: m_pMSHTML->put_designMode(L"On");

    m_pMSHTML->put_designMode(L"Off");
      

  8.   

    to leicam: 保存下来的文件中, id 仍然是以前的!(没有变成mytest)给个例子如何?先xiexie了
      

  9.   

    ?我这里改是可以保存的。
    要先到designmode,再修改,再保存。如果先修改再到designmode,就恢复了。
      

  10.   

    pdoc2->put_designMode(L"On"); //enter designmode
    ..//modify pdoc2...
    IPersistFile::Save(...);
    pdoc2->put_designMode(L"Inherit");//back to normal mode
      

  11.   

    void CMyHtmlView::OnDesignMode() 
    {
    // TODO: Add your command handler code here
    HRESULT hr;
    IHTMLDocument2* piDoc = 0;
    IWebBrowser2* piWeb = 0;
    IDispatch* piDisp = 0;
    IUnknown* piUnk = m_wndBrowser.GetControlUnknown();
    hr = piUnk->QueryInterface(IID_IWebBrowser2, (void**)&piWeb);
    if(FAILED(hr))
    {
    AfxMessageBox("QueryInterface for IWebBrowser2");
    return;
    }
    hr = piWeb->get_Document(&piDisp);
    if(SUCCEEDED(hr))
    {
    hr = piDisp->QueryInterface(IID_IHTMLDocument2, (void**)&piDoc);
    if(SUCCEEDED(hr))
    {
    if(m_MyClientSite.m_bDocDesignMode)
    piDoc->put_designMode(L"Inherit");
    else
    piDoc->put_designMode(L"On");
    m_MyClientSite.m_bDocDesignMode = !m_MyClientSite.m_bDocDesignMode;
    piDoc->Release();
    }
    else
    AfxMessageBox("QueryInterface doc");
    piDisp->Release();
    }
    else
    AfxMessageBox("get_Document");
    }void CMyHtmlView::OnSaveToFile() 
    {
    // TODO: Add your command handler code here
    CFileDialog dlg(FALSE, NULL, NULL,
    OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
    NULL, NULL);
    if(dlg.DoModal()==IDCANCEL)
    return;
    _bstr_t path=dlg.GetPathName(); HRESULT hr;
    IWebBrowser2* piWeb = 0;
    IDispatch* piDisp = 0;
    IPersistFile* piPF = 0;
    IUnknown* piUnk = m_wndBrowser.GetControlUnknown();
    hr = piUnk->QueryInterface(IID_IWebBrowser2, (void**)&piWeb);
    if(FAILED(hr))
    {
    AfxMessageBox("QueryInterface for IWebBrowser2");
    return;
    }
    hr = piWeb->get_Document(&piDisp);
    if(SUCCEEDED(hr))
    {
    hr = piDisp->QueryInterface(IID_IPersistFile, (void**)&piPF);
    if(SUCCEEDED(hr))
    {
    piPF->Save(path, TRUE);
    piPF->Release();
    }
    piDisp->Release();
    }
    }void CMyHtmlView::OnModifyBody() 
    {
    // TODO: Add your command handler code here
    HRESULT hr;
    IHTMLDocument2* piDoc = 0;
    IWebBrowser2* piWeb = 0;
    IDispatch* piDisp = 0;
    IHTMLElement* piElem = 0;
    IHTMLBodyElement* piBody = 0;
    IUnknown* piUnk = m_wndBrowser.GetControlUnknown();
    hr = piUnk->QueryInterface(IID_IWebBrowser2, (void**)&piWeb);
    if(FAILED(hr))
    {
    AfxMessageBox("QueryInterface for IWebBrowser2");
    return;
    }
    hr = piWeb->get_Document(&piDisp);
    if(SUCCEEDED(hr))
    {
    hr = piDisp->QueryInterface(IID_IHTMLDocument2, (void**)&piDoc);
    if(SUCCEEDED(hr))
    {
    hr = piDoc->get_body(&piElem);
    if(SUCCEEDED(hr))
    {
    hr = piElem->QueryInterface(IID_IHTMLBodyElement, (void**)&piBody);
    if(SUCCEEDED(hr))
    {
    VARIANT v; piBody->get_bgColor(&v);
    _bstr_t bstr=v;
    // AfxMessageBox((char*)bstr); v.vt=VT_BSTR;
    v.bstrVal = ::SysAllocString(OLESTR("#00FF00"));
    piBody->put_bgColor(v);
    ::SysFreeString(v.bstrVal);
    piBody->Release();
    }
    else
    AfxMessageBox("QueryInterface body");
    piElem->Release();
    }
    else
    AfxMessageBox("get_body");
    piDoc->Release();
    }
    else
    AfxMessageBox("QueryInterface doc");
    piDisp->Release();
    }
    else
    AfxMessageBox("get_Document");
    }其中m_MyClientSite.m_bDocDesignMode是当前状态。