`

获得WebBrowser控件中的HTML源码

阅读更多
这里介绍如何用程序的方法获得WebBrowser控件中的HTML的源代码,并可以通过修改源代码内容来修改页面内容(注意:不是显示一个新的页面)。

首先要加入WebBrowser控件,加入控件的方面我就不说了。获得源代码方法有两种:

一、方法1(严格说,这个方法只不过是调用WebBrowser自己的菜单命令"查看源文件而已",并非我们所希望的)
关键代码:

#include "mshtmcid.h"
void CHtmlView::OnMethod1()
{
CWnd* pWnd = NULL;

CWnd* pWndShell = m_browser.GetWindow(GW_CHILD); // get the webbrowser window pointer

if (pWndShell)
{
pWnd = pWndShell->GetWindow(GW_CHILD); //get the child window pointer
}

if (pWnd != NULL)
{
WPARAM wParam = MAKEWPARAM(IDM_VIEWSOURCE, 1); //convert to unsigned 32 bit value and pass it to wparam
pWnd->SendMessage(WM_COMMAND, wParam, (LPARAM)this->m_hWnd); //cool send a message to retreive the source.
}
}

二、方法2

原理在于取得IPersistStreamInit接口指针,然后把网页写到IStream流中去。
关键代码:

#include "mshtml.h"
//在SourceView中填写HtmlView中网页的源程序
void CMainFrame::OnMethod2()
{
IHTMLDocument2 *pHTMLDocument=NULL;
IPersistStreamInit *pPSI=NULL;

IStream *pStream=NULL;
HGLOBAL hHTMLText;

if (!(pHTMLDocument = (IHTMLDocument2*)m_pHtmlView->m_browser.GetDocument()))
return;

if (FAILED(pHTMLDocument->QueryInterface(&pPSI)))
{
// pHTMLDocument->Release();
return;
}

hHTMLText = GlobalAlloc(GMEM_FIXED, MAX_SIZE);
CreateStreamOnHGlobal(hHTMLText, TRUE, &pStream);
pPSI->Save(pStream, FALSE);

// m_pSourceView->SetWindowText((char*)hHTMLText);

long nEditLength = m_pSourceView->GetEditCtrl().GetWindowTextLength();
m_pSourceView->GetEditCtrl().SetSel(0, nEditLength);
m_pSourceView->GetEditCtrl().ReplaceSel("");
char *pText = (char*)hHTMLText;
long lHtmlLength = strlen(pText);
CString str("");
long n = 0;
for (long i=0; i < lHtmlLength; i++)
{
if (*pText != 0x0d && *pText != 0x0a)
{
str += *pText;
pText++;
}
else
{
pText++;
if (*pText == 0x0a)
pText++;
str += "\r\n";
nEditLength = m_pSourceView->GetEditCtrl().GetWindowTextLength();
m_pSourceView->GetEditCtrl().SetSel(nEditLength, nEditLength);
m_pSourceView->GetEditCtrl().ReplaceSel(str);
str.Empty();
}
}

pStream->Release();
pPSI->Release();
// pHTMLDocument->Release();
}

三、修改HTML源代码以改变网页的显示

这部分比较有意思,可以当作是一个小的HTML编辑器,看看预演效果。特别的不是显示一个新文件,而是修改原来的HTML文件。
关键代码:

//根据SourceView里的HTML文本改变HtmlView里的显示
void CMainFrame::OnChangehtml()
{
IHTMLDocument2 *pHTMLDocument=NULL;
IPersistStreamInit *pPSI=NULL;

IStream *pStream=NULL;
HGLOBAL hHTMLText;

if (!(pHTMLDocument = (IHTMLDocument2*)m_pHtmlView->m_browser.GetDocument()))
return;

if (FAILED(pHTMLDocument->QueryInterface(&pPSI)))
{
// pHTMLDocument->Release();
return;
}

pHTMLDocument->clear();
pPSI->InitNew();

LPCTSTR strText = m_pSourceView->LockBuffer();
DWORD dwLength = strlen(strText);
hHTMLText = GlobalAlloc(GMEM_FIXED, dwLength);
memset(hHTMLText, 0, dwLength);
memcpy(hHTMLText, strText, dwLength);
m_pSourceView->UnlockBuffer();
CreateStreamOnHGlobal(hHTMLText, TRUE, &pStream);
ULARGE_INTEGER libNewSize;
libNewSize.QuadPart = dwLength;
pStream->SetSize(libNewSize);这一步必须要,否则显示时会有多余字符出现
pPSI->Load(pStream);

pStream->Release();
pPSI->Release();
// pHTMLDocument->Release();
}

有时侯不能显示出网页而显示的是源码文本,比如微软网站的首页就是这种情况。把源码中的这句话 < META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso8859-1" />去掉就可以了。原因不明。如果您知道原因请告诉我。
from http://www.evget.com/
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics