Get html source code in the webbrowser control

xiaoxiao2021-03-06  43

Author: Unknown Updated: legume Source: Unknown Last update: 2004-06-11 Submission-mail: tg * ddvip.com Here's how to get WebBrowser control in the HTML source code of a program with the method, and can modify the contents of source code To modify the page content (Note: Not a new page). First join the webbrowser control, I don't say it. There are two kinds of source code methods: 1. Method 1 (strictly said that this method is just to call WebBrowser's own menu command "View Source File", not what we hope) 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 rate the source.}} 2, method 2 is the principle of obtaining the IPersistStreaminit interface pointer, and then write the webpage into the IPREAM stream.

The key code: #include "mshtml.h" // fill in the void SourceView in HtmlView the source pages CMainFrame :: OnMethod2 () {IHTMLDocument2 * pHTMLDocument = NULL; IPersistStreamInit * pPSI = NULL; IStream * pStream = NULL; HGLOBAL hHTMLText ; if return ((pHTMLDocument = (IHTMLDocument2 *) m_pHtmlView-> m_browser.GetDocument ())!); 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 (long i = 0; i GetEditCtrl () GetWindowTextLength ();.. m_pSourceView-> GetEditCtrl () SetSel (nEditLength, nEditLength);. m_pSourceView-> GetEditCtrl () ReplaceSel (str); str.Empty ();}} pStream-> Release ( ); ppsi-> release (); // phtmldocument-> release ();} III, modify the HTML source code to change the display of the webpage, which is more interesting, can be used as a small HTML editor, look at the preview effect . Special not to display a new file, but modify the original HTML file.

转载请注明原文地址:https://www.9cbs.com/read-72995.html

New Post(0)