Source: http://www0.ccidnet.com/tech/web/2001/02/08/58_1642.html
IE in the exploration customize the browser, a good assistant (under) (Author: Green Apple studio compilation, 2001 at 14 o'clock on February 8)
Access Document Objects Now BHO references Internet Explorer's WebBrowser controls and has been connected to the browser to receive events it produce. After the web page is fully downloaded and initialized correctly, it is now possible to access it through the DHTML document object model. WebBrowser's Document property returns a pointer to the IDispatch interface to the document object: ccomptr pdisp; hResult hr = m_spwebbrowser2-> get_document (& pdisp); get_document () method provides just a pointer to the interface. We need to determine that it is really an HTML document object behind the iDispatch pointer. If you are using Visual Basic, the following is equivalent code: Dim doc As Object Set doc = WebBrowser1.Document If TypeName (doc) = "HTMLDocument" Then 'Get the document content and display Else' Disable the display dialog End If now we need Determine the essence of the iDispatch pointer returned by GET_Document (). Internet Explorer is not only an HTML browser, but also handles any ActiveX document; that is, any document supported as an application for the ActiveX document service. In this way, it is indeed an HTML page that cannot be guaranteed. There is a solution to view the URL and check the extension of the URL. But how do you handle Active Server Pages (ASP) or a URL that is contained in the HTML page? What should I use if you use a custom protocol like About or RES? We decided to take another way, it is the same as the above Visual Basic code. This idea is that if the IDispatch pointer does point to an HTML document, access to the IHTMLDocument2 interface can be successfully returned. IHTMLDocument2 is an interface that integrates the functionality of the DHTML object model as the HTML page implementation. The following code fragment shows how to make such a determination: CComPtr pDisp; HRESULT hr = m_spWebBrowser2-> get_Document (& pDisp); CComQIPtr spHTML; spHTML = pDisp; if (spHTML) {// obtain the content of the document And display it} else {// Prohibited code window control} If the access IHTMLDocument2 interface fails, the SPHTML pointer is NULL. Otherwise, we can access the method and properties of the DHTML object model normally. The current problem is how to get the source code of the displayed page. Fortunately, basic DHTML knowledge is enough to do this.
Since all of the HTML pages are included in the
tag, the DHTML object model requires you first get pointers to the Body object: ccomptr
m_pbody; hr = sphtml-> get_body (& m_pbody); 奇特, DHTML The object model does not let you know the marks before , such as the original content of . These content have been processed and saved to a series of properties, but you still can't get the original content of the original HTML file. However, now Body can tell us enough. We need to read the contents of the OuterHTML property to a BSTR variable to get HTML code included between and body>. BSTR BSTRHTMLTEXT; HR = M_PBODY-> GET_OUTERHTML (& BSTRHTMLTEXT); now, the job display text in the code window is the creation window, convert the string from Unicode to ANSI, and set the edit box as shown in Figure 3.
The following is a complete work of all code: HRESULT CViewSource :: GetDocumentContent () {USES_CONVERSION; // get document object WebBrowser CComPtr pDisp; HRESULT hr = m_spWebBrowser2-> get_Document (& pDisp); if (FAILED (hr)) return HR; // Verify that we get a pointer to the htmldocument2 interface // We query the IHTMLDocument2 interface (through the delegation pointer) ccomqiptr Sphtml; Sphtml = PDISP; // Get the source code IF (SPHTML) {/ / Get a Body object hr = SPHTML-> get_body (& m_PBODY); if (Failed (HR)) Return HR; // Get html text BSTR BSTRHTMLTEXT; hr = m_pbody-> get_outerhtml (& BSTRHTMLTEXT); if (Failed (HR)) Return hr; // converts from Unicode text to ANSI LPTSTR psz = new TCHAR [SysStringLen (bstrHTMLText)]; lstrcpy (psz, OLE2T (bstrHTMLText)); // allowed to modify the text HWND hwnd = m_dlgCode.GetDlgItem (IDC_TEXT); EnableWindow ( HWnd, true); hwnd = m_dlgcode.getdlgitem (idc_apply); EnableWindow (hwnd, true); // Setting the text M_dlgcode.SetdlgitemText (IDC_Text, PSZ) of the code window; delete [] psz;} else // document is not an HTML page {m_dlgCode.SetDlgItemText (IDC_TEXT, ""); HWND hwnd = m_dlgCode.GetDlgItem (IDC_TEXT); EnableWindow (hwnd, false); hwnd = m_dlgCode.GetDlgItem (IDC_APPLY); EnableWindow (hwnd, false); } Return S_OK;} Since we run this code to respond to DocumentComplete notifications, each new page will be quickly processed. The DHTML object model allows you to modify the structure of the page, but after you press the F5 key or the refresh button to refresh the view, all modifications will be lost immediately. By processing the DownloadComplete event you can refresh the code window at the same time. (Note that the DownloadComplete event is arrived first than the DocumentComplete event. A simple Boolean member, for example, M_BDocumentCompleted can be used to distinguish between these two situations. The management code window is used to display the code window of the HTML source code for the current page is another basic element of the ATL, a dialog window that can be found in the Miscellaneous page of the ATL object wizard.