动态修改网页之后,如何把IHTMLDocument2中内容保存到文件? 动态修改网页之后,如何把IHTMLDocument2中内容保存到文件?---------------(现在无法把修改结果保存下来) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 QueryInterface for IPersistFileIPersistFile->Save(); hr=piDoc->QueryInterface(IID_IPersistFile, (void**)piPF);if(SUCCEEDED(hr)){ piPF->Save(OLESTR("1.htm"), TRUE); piPF->Release();} 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)接着我就是需要把这个结果保存下来,到某个新文件中去!==================================================================================,请执教,一定给分的 将webbrowser设置为设计模式,然后修改,即可保存修改后的结果。处理 IDispatch::InvokeSTDMETHODIMP 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; } ...} 如果也要工作于usermode,在状态切换时用IOleControl::OnAmbientPropertyChange(DISPID_AMBIENT_USERMODE)通知webbrowser 这样做, 打开网页编辑模式然后保存就可以了:在.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; } sorry, 忘记在保存的前后要加上: m_pMSHTML->put_designMode(L"On");和 m_pMSHTML->put_designMode(L"Off"); to leicam: 保存下来的文件中, id 仍然是以前的!(没有变成mytest)给个例子如何?先xiexie了 ?我这里改是可以保存的。要先到designmode,再修改,再保存。如果先修改再到designmode,就恢复了。 pdoc2->put_designMode(L"On"); //enter designmode..//modify pdoc2...IPersistFile::Save(...);pdoc2->put_designMode(L"Inherit");//back to normal mode 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是当前状态。 iocp中GetQueuedCompletionStatus的问题? 谁会使用 Inno Setup 关于相对路径 那里可以找到 Microsoft SAPI 5.1 TTS 免费的中文女声的发音包 多线程编程的几个问题,各位方家请指教 调用DLL的保护措施 采用VC++编程,请问如何捕获程序的异常错误(任何异常)及进行相关处理? 十万火急... 我两个线程,各自单独用AD0连接数据库,有必要做线程同步吗? 一个MFC的问题 vc下进行usb读写问题:vid pid相同的usb设备,在读写的时候如何进行区分 关于参数的传递 怎么得到Edit窗口的hdc
IPersistFile->Save();
if(SUCCEEDED(hr))
{
piPF->Save(OLESTR("1.htm"), TRUE);
piPF->Release();
}
不过,这样子只能把修改之前的 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)接着我就是需要把这个结果保存下来,到某个新文件中去!==================================================================================
,请执教,一定给分的
处理 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;
}
...
}
IOleControl::OnAmbientPropertyChange(DISPID_AMBIENT_USERMODE)通知webbrowser
这样做, 打开网页编辑模式然后保存就可以了:
在.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;
}
和
m_pMSHTML->put_designMode(L"Off");
要先到designmode,再修改,再保存。如果先修改再到designmode,就恢复了。
..//modify pdoc2...
IPersistFile::Save(...);
pdoc2->put_designMode(L"Inherit");//back to normal mode
{
// 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是当前状态。