Browser Control Teaching: Use flow load and save HTML content

zhaozj2021-02-08  239

Browser Control Teaching: Use flow load and save HTML content

Part of this article Translated from MSDN Article loading HTML Content from A Street

IPERSIST * interface, and its subsequent method, can be used for Microsoft® Visual C ® and WebBrowser controls using streaming and saving HTML content

This article discusses the steps required to load HTML content, divided into the following parts:

Validity to the About: Blank DHTML Object Model Using QueryInterface to get the IPERSIST * Interface Using IPERSIST * Interface Load and Save HTML Content Load and Save HTML Element Data Known Issues Related Topics

Position to About: Blank

IWebBrowser2 :: NaviGate2 method for iWebbrowser2 enables you to make your browser to a URL. In the sample code below, the iWebBrowser2 :: NaviGate2 method is used to locate the About: Blank page. Positioning to this empty page ensures that the MSHTML is loaded and the HTML element in the dynamic HTML (DHTML) object model is valid.

This example demonstrates how to make your browser to an empty page. The m_pbrowser variable contains the IWebBrowser2 interface pointer obtained from the WebBrowser control.

M_PBrowser-> NaviGate2 (_T ("About: blank"), NULL, NULL, NULL, NULL;

Validity of DHTML Object Model

The DHTML object model is used to access and operate the HTML page and are not available until the page is loaded. Your application determines if a page is loaded by handling the DwebBrowserevents2 :: DocumentComplete event of the WebBrowser control. This event may be triggered by each frame in the page, and trigger once when the top-level document is loaded. You can judge whether the DwebBBRowserevents2 :: DocumentComplete event is a top-level frame by comparing the IDSPATCH interface pointer and the webbrowser control passing the incident.

This webbrowser dwebbrowserevents2 :: DocumentComplete event sample processing code demonstrates whether the event is a top-level frame, (if yes), this means that the HTML page is loaded. This example also demonstrates how to be from a memory block - this occasion is one A string containing the HTML content that needs to be displayed - creation stream.

Void MyObject :: DocumentComplete (LPDispatch PDISP, VARIANT * URL)

{

HRESULT HR;

IUNKNOWN * PUNKBROWSER = NULL;

IUNKNOWN * PUNKDISP = NULL;

IStream * pstream = NULL;

Hglobal hhtmltext;

Static tchar szhtmltext [] = "

stream test

This HTML content has been loaded from the stream. ";

// Is this DocumentComplete event whether is the top frame window?

// Check COM ID: Compare the iUnknown interface pointer.

HR = m_pbrowser-> queryinterface (IID_IUNKNOWN, (Void **) & punkbrowser;

En (ac))

{

HR = pdisp-> queryinterface (IID_IUNKNOWN, (Void **) & punkdisp);

En (ac))

{

IF (punkbrowser == punkdis)

{// This is the DocumentComplete event of the top frame window - page load is complete!

// Establish a stream containing HTML content

// In addition, this stream can be passed (not created)

SIZE_T = Cchlength;

// Todo: Securely determines the length of SZHTMLText, the unit is TCHAR.

HHTMLTEXT = Globalalloc (GPTR, Cchlength 1);

IF (HHTMLTEXT)

{

SIZE_T CCHMAX = 256;

Stringcchcopy (Tchar *) HHTMLText, CCHMAX 1, SZHTMLTEXT);

// Todo: Add the error handling code here.

HR = CreatestReamonhglobal (HHTMLText, True, & Pstream);

En (ac))

{

// Call the auxiliary function to make the web browser load stream.

LoadWebBrowserfromstream (m_pbrowser, pstream);

PStream-> Release ();

}

GlobalFree (HHTMLText);

}

}

Punkdisp-> Release ();

}

PunkBrowser-> Release ();

}

}

Use QueryInterface to get interfaces such as IPERSIS *

The IWebBrowser2 :: get_document property of the webbrowser control returns a document object that represents the DHTML object model of the top layer frame. MSHTML provides functions that use streams and saved HTML functions with IPersistStreaminit, iPersistFile, such as Frame, IPRAME, and other IPERSISTREAMINIT, IPERSISTFILE. The IDispatch interface of the object can be used to query the corresponding interface pointer by using QueryInterface and IIPERSSTREAMINIT, as described in the following code.

HRESULT loadWebBrowserfromstream (iWebBrowser2 * PwebbBBBBBBBBBBBBBBBBBBBBBBROWSER2 * PSTREAM)

{

HRESULT HR;

Idispatch * phtmldoc = null;

IPersistStreaminit * ppersistStreaminit = NULL;

// Return to the document object.

HR = pwebbrowser-> get_document (& phtmldoc);

En (ac))

{

//> Query IPersistStreaminit interface

HR = phtmldoc-> queryinterface (IID_IPERSISTREAMINIT, (void **) & pPersistStreaminit;

En (ac))

{

// Initialization document.

HR = pPERSISTREAMINIT-> initnew ();

En (ac))

{

// Load stream content

HR = pPersistStreaminit-> load (pstream);

}

PPERSISTREAMINIT-> Release ();

}

}

}

Use the IPersist * interface to load and save HTML content

The IPersistStreaminit interface has an initNew and LOAD methods for initialization and loading HTML content, as well as SAVE methods for saving. The initnew method is initialized to a known state, and the LOAD method is loaded from the stream into the HTML content, and the Save method saves the HTML content to the stream. The IPersistFile interface has a LOAD and Save method for loading and saving HTML content from disk files.

In the previous example code, the HTML document is initialized, and the HTML content is loaded from the stream.

Note that from Microsoft Internet Explorer 5, more than one call IPersist * interface

The LOAD method is feasible. In the earliest version, each MSHTML instance only supports once

LOAD call.

Load and save HTML element data

If the HTML element supports the use of IPERSISTORAGE, IPERSISTREAMINIT, or IPERSISTMEMORY, you can also load and save information by similar code.

For information on the information of the ActiveX control in the web page, please refer to my article: Access the property (9CBS Document Center) of the ActiveX Control in the Document with the HTML Document Object Model (9CBS Document Center) to get the control interface, then query whether the ActiveX control support IPERSIST * interface.

Note: These interfaces that use VB6.0 may not support these interfaces, which requires IPERSISTPROPERTYBAG or attribute set load and saving information. See Microsoft Knowledge Base Article Q272490 BUG: Visual Basic Components Error 0x800A02E0 "Unable to save unmelted classes"

Question

Microsoft Knowledge Base Article

Q271868 In Microsoft Internet Explorer (Programming) 5.5, the frame object does not support IPersistStream, IPersistFile, IPersistMemory interface.

Q323569 BUG: PERSISTREAMINIT :: Load () Shows HTML content for text

Q264868 BUG: Internet Explorer does not detect changes 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 Internet Explorer XML parser to load / save XML data Q196340: Get the webbrowser object model for HTML framework

related topic

Microsoft Visual Studio The Component Object Model Specification

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

New Post(0)