动态修改网页之后,如何把IHTMLDocument2中内容保存到文件? 动态修改网页之后,如何把IHTMLDocument2中内容保存到文件? ---------------(现在无法把修改结果保存下来) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 保存到本地的话,就是用fopen等一系列函数好了。你不会想把你修改的页面保存到服务器上吧?如果你能做到,麻烦告诉我下。 LZ 我的意思是你怎么 IHTMLDocument2 的内容? LZ 我的意思是你怎么获取 IHTMLDocument2 的内容? 通过CWebBrowser2控件的GetDocument函数 IHTMLElement 的get_innerhtml 得到整个body的htm代码,然后建立个文件把得到的代码写进去,就行了 思路可行,不过不是保存<body>对应的代码而是<html>对应的代码 可是如果这个网页很大的话,他的html对应的代码会很多,赋值给CString参数? 不行啊 CHtmView::GetSource()可以试一下,看了其源码,是把IHTMLDocument2序列化到流 看了下CHtmlEditView相关的源码,就是搞动态编辑网页,然后保存的,肯定可以源码本机路径:C:\Program Files\Microsoft Visual Studio 8\VC\atlmfc\src\mfc\viewhtml.cpp[code]BOOL CHtmlEditDoc::OnSaveDocument(LPCTSTR lpszFileName){ BOOL bRet = FALSE; CHtmlEditView *pView = GetView(); if (pView != NULL) { CFile file; if (file.Open(lpszFileName, CFile::modeCreate|CFile::modeWrite)) { CArchive ar(&file, CArchive::store); CArchiveStream as(&ar); CComPtr<IHTMLDocument2> spHTMLDocument; CComQIPtr<IPersistStreamInit> spPSI; pView->GetDHtmlDocument(&spHTMLDocument); if (spHTMLDocument) { spPSI = spHTMLDocument; if (spPSI) { if (S_OK == spPSI->Save((IStream*)&as, TRUE)) { SetModifiedFlag(FALSE); bRet = TRUE; } } } } } return bRet;}[/code] BOOL CHtmlEditDoc::OnSaveDocument(LPCTSTR lpszFileName){ BOOL bRet = FALSE; CHtmlEditView *pView = GetView(); if (pView != NULL) { CFile file; if (file.Open(lpszFileName, CFile::modeCreate|CFile::modeWrite)) { CArchive ar(&file, CArchive::store); CArchiveStream as(&ar); CComPtr<IHTMLDocument2> spHTMLDocument; CComQIPtr<IPersistStreamInit> spPSI; pView->GetDHtmlDocument(&spHTMLDocument); if (spHTMLDocument) { spPSI = spHTMLDocument; if (spPSI) { if (S_OK == spPSI->Save((IStream*)&as, TRUE)) { SetModifiedFlag(FALSE); bRet = TRUE; } } } } } return bRet;} 下面是我的代码片段,中间有去掉对你没用的东西。这个可以保存你修改过的源码文件,但是不包括doctype对象的源码。CComQIPtr<IHTMLDocument3> pDoc3; if ( S_OK != pDisp2->QueryInterface(IID_IHTMLDocument3,(LPVOID*)&pDoc3) ) { AfxMessageBox(TEXT("get modify source failed")); return; } //save modified html file //HTML object CComQIPtr<IHTMLElement> pRootElem; pDoc3->get_documentElement(&pRootElem); _bstr_t bstrHtml; pRootElem->get_outerHTML(bstrHtml.GetAddress()); wstring sNewHtml( bstrHtml ); //write to file strDir = strDir.substr(0, strDir.rfind(TEXT("."))); strDir += TEXT(".html"); absPath = CString(arrTmpPath); absPath += TEXT("DevHelper\\"); absPath += strDir.c_str(); HANDLE hFile = CreateFile(absPath,GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ,NULL,CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,NULL); if ( INVALID_HANDLE_VALUE == hFile ) { AfxMessageBox(TEXT("Open Save Failed")); return; } pDoc2->get_URL(bstrHtml.GetAddress()); CStringA comment; comment.Format(("<!-- saved by DevHelper from url=%s -->\n"),CStringA(bstrHtml.GetBSTR())); DWORD dwBytesWritten = 0; BOOL bRet; if ( FALSE == WriteFile(hFile,comment.GetBuffer(),comment.GetLength(),&dwBytesWritten,NULL) ) { AfxMessageBox(TEXT("Write temp file head failed")); } bRet = WriteFile(hFile,pTmp,bufLenExp,&dwBytesWritten,NULL); if ( FALSE == bRet ) { AfxMessageBox(TEXT("Write temp file failed")); return; } CloseHandle(hFile); 我的开发环境是VC++ 6.0 你的代码中很多对象6.0里都没有,如IHTMLDocument3。不知道在6.0里怎么写 如何检测两列波是否相交与分开? VC中对数据库的delete的操作 高手帮我看下! 一个基础问题 Sendrequest函数的第二个参数该如何来写? 面试时问我windows重要的dll有哪几个,谁来说说看 我要写个画立方体的的算法,怎么写? 求助:谁知道如何编程实现对音量控制里的,如 wave,midi,line in等,控制它们的静音与不静音? MFC 的 DockPane 比 vs2012 的效率差很多。 有比“%i”的取值范围更大的么? IOCP非常疑惑的问题:GetQueuedCompletionStatus到底是如何知道是发送还是接收的? 这种效果如何实现
你不会想把你修改的页面保存到服务器上吧?如果你能做到,麻烦告诉我下。
源码本机路径:C:\Program Files\Microsoft Visual Studio 8\VC\atlmfc\src\mfc\viewhtml.cpp
[code]
BOOL CHtmlEditDoc::OnSaveDocument(LPCTSTR lpszFileName)
{
BOOL bRet = FALSE;
CHtmlEditView *pView = GetView();
if (pView != NULL)
{
CFile file;
if (file.Open(lpszFileName, CFile::modeCreate|CFile::modeWrite))
{
CArchive ar(&file, CArchive::store);
CArchiveStream as(&ar);
CComPtr<IHTMLDocument2> spHTMLDocument;
CComQIPtr<IPersistStreamInit> spPSI;
pView->GetDHtmlDocument(&spHTMLDocument);
if (spHTMLDocument)
{
spPSI = spHTMLDocument;
if (spPSI)
{
if (S_OK == spPSI->Save((IStream*)&as, TRUE))
{
SetModifiedFlag(FALSE);
bRet = TRUE;
}
}
}
}
}
return bRet;
}
[/code]
BOOL CHtmlEditDoc::OnSaveDocument(LPCTSTR lpszFileName)
{
BOOL bRet = FALSE;
CHtmlEditView *pView = GetView();
if (pView != NULL)
{
CFile file;
if (file.Open(lpszFileName, CFile::modeCreate|CFile::modeWrite))
{
CArchive ar(&file, CArchive::store);
CArchiveStream as(&ar);
CComPtr<IHTMLDocument2> spHTMLDocument;
CComQIPtr<IPersistStreamInit> spPSI;
pView->GetDHtmlDocument(&spHTMLDocument);
if (spHTMLDocument)
{
spPSI = spHTMLDocument;
if (spPSI)
{
if (S_OK == spPSI->Save((IStream*)&as, TRUE))
{
SetModifiedFlag(FALSE);
bRet = TRUE;
}
}
}
}
}
return bRet;
}
if ( S_OK != pDisp2->QueryInterface(IID_IHTMLDocument3,(LPVOID*)&pDoc3) )
{
AfxMessageBox(TEXT("get modify source failed"));
return;
}
//save modified html file
//HTML object
CComQIPtr<IHTMLElement> pRootElem;
pDoc3->get_documentElement(&pRootElem);
_bstr_t bstrHtml;
pRootElem->get_outerHTML(bstrHtml.GetAddress());
wstring sNewHtml( bstrHtml );
//write to file
strDir = strDir.substr(0, strDir.rfind(TEXT(".")));
strDir += TEXT(".html");
absPath = CString(arrTmpPath);
absPath += TEXT("DevHelper\\");
absPath += strDir.c_str();
HANDLE hFile = CreateFile(absPath,GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ,NULL,CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,NULL);
if ( INVALID_HANDLE_VALUE == hFile )
{
AfxMessageBox(TEXT("Open Save Failed"));
return;
}
pDoc2->get_URL(bstrHtml.GetAddress());
CStringA comment;
comment.Format(("<!-- saved by DevHelper from url=%s -->\n"),CStringA(bstrHtml.GetBSTR()));
DWORD dwBytesWritten = 0;
BOOL bRet;
if ( FALSE == WriteFile(hFile,comment.GetBuffer(),comment.GetLength(),&dwBytesWritten,NULL) )
{
AfxMessageBox(TEXT("Write temp file head failed"));
} bRet = WriteFile(hFile,pTmp,bufLenExp,&dwBytesWritten,NULL);
if ( FALSE == bRet )
{
AfxMessageBox(TEXT("Write temp file failed"));
return;
}
CloseHandle(hFile);