Obtain the HTML source code in the WebBrowser control.

Source: Internet
Author: User

 

 

This section describes how to use the program method to obtainThe source code of HTML in the WebBrowser control, and the page content can be modified by modifying the source code content (Note: it is not to display a new page ).

 

First, add the WebBrowser control. There are two methods to obtain source code:

 

I. Methods1 (strictly speaking, this method is just to call WebBrowser's menu command "view source files", which is not what we want)

Key code:

 

# 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.

}

}

 

Ii. Method2

 

The principle is to get the IPersistStreamInit interface pointer and then write the webpage to the IStream stream.

Key code:

 

# Include "mshtml. h"

// Fill in the source code of the web page in HtmlView in SourceView

Void CMainFrame: OnMethod2 ()

{

IHTMLDocument2 * pHTMLDocument = NULL;

IPersistStreamInit * pPSI = NULL;

 

IStream * pStream = NULL;

HGLOBALHHTMLText;

 

If(! (PHTMLDocument = (IHTMLDocument2 *) m_pHtmlView-> m_browser.GetDocument ()))

Return;

If(FAILED (pHTMLDocument-> QueryInterf

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.