动态修改网页之后,如何把IHTMLDocument2中内容保存到文件?

linjr 2003-04-22 11:49:36
加精
动态修改网页之后,如何把IHTMLDocument2中内容保存到文件?

---------------(现在无法把修改结果保存下来)

...全文
357 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
ndy_w 2003-05-15
  • 打赏
  • 举报
回复
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是当前状态。
ndy_w 2003-05-15
  • 打赏
  • 举报
回复
pdoc2->put_designMode(L"On"); //enter designmode
..//modify pdoc2...
IPersistFile::Save(...);
pdoc2->put_designMode(L"Inherit");//back to normal mode
ndy_w 2003-05-15
  • 打赏
  • 举报
回复
?我这里改是可以保存的。
要先到designmode,再修改,再保存。如果先修改再到designmode,就恢复了。
linjr 2003-05-15
  • 打赏
  • 举报
回复
to leicam:

保存下来的文件中, id 仍然是以前的!(没有变成mytest)

给个例子如何?

先xiexie了
leicam 2003-05-13
  • 打赏
  • 举报
回复
sorry, 忘记在保存的前后要加上:

m_pMSHTML->put_designMode(L"On");

m_pMSHTML->put_designMode(L"Off");
leicam 2003-05-13
  • 打赏
  • 举报
回复

这样做, 打开网页编辑模式然后保存就可以了:
在.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;
}
ndy_w 2003-04-29
  • 打赏
  • 举报
回复
如果也要工作于usermode,在状态切换时用
IOleControl::OnAmbientPropertyChange(DISPID_AMBIENT_USERMODE)通知webbrowser
ndy_w 2003-04-29
  • 打赏
  • 举报
回复
V_BOOL(pv)=VARIANT_FALSE
ndy_w 2003-04-29
  • 打赏
  • 举报
回复
将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;
}
...
}
guzh 2003-04-29
  • 打赏
  • 举报
回复
学习
buaafang 2003-04-29
  • 打赏
  • 举报
回复
关注
linjr 2003-04-28
  • 打赏
  • 举报
回复
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)接着我就是需要把这个结果保存下来,到某个新文件中去!

==================================================================================
,请执教,一定给分的
loopyifly 2003-04-22
  • 打赏
  • 举报
回复
...
ndy_w 2003-04-22
  • 打赏
  • 举报
回复
hr=piDoc->QueryInterface(IID_IPersistFile, (void**)piPF);
if(SUCCEEDED(hr))
{
piPF->Save(OLESTR("1.htm"), TRUE);
piPF->Release();
}
ndy_w 2003-04-22
  • 打赏
  • 举报
回复
QueryInterface for IPersistFile
IPersistFile->Save();

3,055

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC HTML/XML
社区管理员
  • HTML/XML社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧