Browser control teaching: loading and saving HTML content using streams
Part of this article is translated from the msdn Article loading HTML content from a stream
The ipersist * interface and its ancillary methods can be used by Microsoft Visual C ++ and webbrowser controls to load and save HTML content using streams.
This article describes the steps required to load HTML content, which are divided into the following parts:
- Go to about: blank
- DHTML Object Model Validity
- Use QueryInterface to obtain ipersist * interface
- Use the ipersist * interface to load and save HTML content
- Load and save HTML Element Data
- Known issues
- Reference
- Related Topics
Go to about: blank
The iwebbrowser2: navigate2 method of the iwebbrowser2 interface allows the browser to navigate to a URL. In the following sample code,Iwebbrowser2: navigate2Method is used to locateAbout: blankPage. Locate this empty page to ensure that mshtml is loaded and the HTML element in the dynamic HTML (DHTML) object model is valid.
This example shows how to locate an empty page in the browser.M_pbrowserVariable include fromWebbrowserTheIwebbrowser2Interface pointer.
m_pBrowser->Navigate2( _T("about:blank"), NULL, NULL, NULL, NULL );
DHTML Object Model Validity
The DHTML Object model is used to access and manipulate HTML page content and is unavailable before page loading. Your application processesWebbrowserControl dwebbrowserevents2: documentcomplete event to determine whether a page is loaded. This event may be triggered by every framework on the page and triggered again when the top-level document is loaded. You can use the idispatch interface pointer andWebbrowserControl to determineDwebbrowserevents2: documentcompleteWhether the event is a top-level framework.
ThisWebbrowser Dwebbrowserevents2: documentcompleteThe event sample processing code demonstrates how to determine whether the event is a top-level framework. (If yes,) This indicates that the HTML page is loaded completely. this example also demonstrates how to create a stream from a memory block, which is a string containing the HTML content to be displayed.
Void myobject: documentcomplete (lpdispatch Pdisp, variant * URL) {hresult hr; iunknown * punkbrowser = NULL; iunknown * punkdisp = NULL; istream * pstream = NULL; hglobal hhtmltext; static tchar szhtmltext [] = "<HTML> Use QueryInterface to obtain ipersis * and other interfaces
WebbrowserThe iwebbrowser2: get_document property of the control returns the document object that represents the DHTML Object Model of the top-level framework. Mshtml passedDocument ObjectAnd other HTML element objects, such as frame and IFRAME.Ipersiststreaminit,IpersistfileAnd other interfaces to load and save HTML.ObjectOfIdispatchInterfaces can be used by using QueryInterface andIid_ipersiststreaminitInterface identifier to query the corresponding interface pointer, as described in the following code example.
Hresult loadwebbrowserfromstream (iwebbrowser2 * pwebbrowser, istream * pstream) {hresult hr; idispatch * phtmldoc = NULL; ipersiststreaminit * ppersiststreaminit = NULL; // return the Document Object. hR = pwebbrowser-> get_document (& phtmldoc); If (succeeded (HR) {//> query ipersiststreaminit interface hR = phtmldoc-> QueryInterface (iid_ipersiststreaminit, (void **) & ppersiststreaminit); If (succeeded (HR) {// initialization document. hR = ppersiststreaminit-> initnew (); If (succeeded (HR) {// load stream content hR = ppersiststreaminit-> load (pstream );} ppersiststreaminit-> release ();}}}
Use the ipersist * interface to load and save HTML content
IpersiststreaminitThe interface hasInitnewAndLoadMethod and the Save Method Used for saving.InitnewMethod initialization flow to a known state,LoadMethod To load HTML content from a stream,SaveMethod To save the HTML content to the stream.
IpersistfileThe interface hasLoadAndSaveMethod.
In the previous sample code, the HTML document is initialized and the HTML content is loaded from the stream.
Note:From Microsoft Internet Explorer 5, more than one call to the ipersist * interface
LoadThe method is feasible. In earlier versions, each mshtml instance only supports one
LoadCall. Load and save HTML Element Data
If HTML elements supportIpersiststorage,Ipersiststreaminit, OrIpersistmemoryYou can also load and save information through similar code.
For how to load and save ActiveX Control Information on the webpage, refer to my article: access the properties of ActiveX control in the document through the HTML Document Object Model (csdn document center) to obtain the control interface, and then query whether the ActiveX control supports the ipersist * interface.
Note: controls written in VB6.0 may not support these interfaces. In this case, you need to use ipersistpropertybag or attribute set to load and save information. See Microsoft Knowledge Base Article q272490 BUG: Visual Basic component error 0x800a02e0 "unable to save uninitialized classes"
Known issues
Microsoft Knowledge Base Article
Q271868 in Microsoft Internet Explorer (programming) 5.5, framework objects are not supportedIpersiststream,Ipersistfile,IpersistmemoryInterface.
Q323569 BUG: persiststreaminit: load () display HTML content as text
Q264868 BUG: Internet Explorer does not detect changes in content types from text/html to text/XML.
Reference
The following articles provide information about the Component Object Model (COM.
- Inside Ole, 2nd edition, by kraig brockschmidt (Microsoft Press)
- Understanding ActiveX and Ole, By David Chappell (Microsoft Press)
- Inside com, By Dale Rogerson (Microsoft Press)
Microsoft Knowledge Base Article
- Q223337 information: use the Internet Explorer XML parser to load/save XML data
- Q196340: how to obtain the webbrowser object model of the HTML framework
Related Topics
- Microsoft Visual Studio
- The Component Object Model Specification