设为首页 加入收藏

TOP

获得WebBrowser控件中的HTML源码
2014-11-23 20:16:22 来源: 作者: 【 】 浏览:7
Tags:获得 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->QueryInterf

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇在任何地方弹出控件 下一篇变量/对象合法性检查

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: