当前位置:编程学习 > C/C++ >>

获得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

补充:软件开发 , C语言 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,