曾经找了很多地方,居然在msdn找到了,看来不得不佩服msdn,我是想在delphi下实现把html流放到WebBrowser控件,看了别人的解释,还是不清楚,只好自己来找。没有翻译过来,是为了原汁原味给大家。
The
This article discusses the steps required to load HTML content from a stream and is divided into the following sections.
- Navigating to about:blank
- Availability of the DHTML Object Model
- Using QueryInterface to Obtain the IPersistStreamInit Interface
- Using the IPersistStreamInit Interface to Load HTML Content
- Offline Reading
- Related Topics
Navigating to about:blank
The
This example demonstrates how to navigate the WebBrowser control to an empty page. The variable m_pBrowser contains the IWebBrowser2 interface pointer obtained from the WebBrowser control.
m_pBrowser->Navigate2( _T("about:blank"), NULL, NULL, NULL, NULL );
Availability of the DHTML Object Model
The DHTML Object Model is used to access and manipulate the contents of an HTML page and is not available until the page is loaded. Your application determines that a page is loaded by handling the
This sample handler code for the WebBrowser DWebBrowserEvents2::DocumentComplete event demonstrates how to determine if this event is for the top frame, which indicates that the HTML page has loaded. This sample also demonstrates how to create a stream from a block of memory—in this case a string that contains the HTML content to be displayed.
Hide Example
void myObject::DocumentComplete(LPDISPATCH pDisp, VARIANT* URL)
{
HRESULT hr;
IUnknown* pUnkBrowser = NULL;
IUnknown* pUnkDisp = NULL;
IStream* pStream = NULL;
HGLOBAL hHTMLText;
static TCHAR szHTMLText[] = "<html><h1>Stream Test</h1><p>This HTML content is/
being loaded from a stream.</html>";
// Is this the DocumentComplete event for the top frame window?
// Check COM identity: compare IUnknown interface pointers.
hr = m_pBrowser->QueryInterface( IID_IUnknown, (void**)&pUnkBrowser );
if ( SUCCEEDED(hr) )
{
hr = pDisp->QueryInterface( IID_IUnknown, (void**)&pUnkDisp );
if ( SUCCEEDED(hr) )
{
if ( pUnkBrowser == pUnkDisp )
{ // This is the DocumentComplete event for the top frame - page is loaded!
// Create a stream containing the HTML.
// Alternatively, this stream may have been passed to us.
size_t = cchLength;
// TODO: safely determine the length of szHTMLText in TCHAR.
hHTMLText = GlobalAlloc( GPTR, cchLength+1 );
if ( hHTMLText )
{
size_t cchMax = 256;
StringCchCopy((TCHAR*)hHTMLText, cchMax + 1, szHTMLText);
// TODO: add error handling code here.
hr = CreateStreamOnHGlobal( hHTMLText, TRUE, &pStream );
if ( SUCCEEDED(hr) )
{
// Call the helper function to load the Web Browser from the stream.
LoadWebBrowserFromStream( m_pBrowser, pStream );
pStream->Release();
}
GlobalFree( hHTMLText );
}
}
pUnkDisp->Release();
}
pUnkBrowser->Release();
}
}
Using QueryInterface to Obtain the IPersistStreamInit Interface
The
Hide Example
HRESULT LoadWebBrowserFromStream(IWebBrowser* pWebBrowser, IStream* pStream)
{
HRESULT hr;
IDispatch* pHtmlDoc = NULL;
IPersistStreamInit* pPersistStreamInit = NULL;
// Retrieve the document object.
hr = pWebBrowser->get_Document( &pHtmlDoc );
if ( SUCCEEDED(hr) )
{
// Query for IPersistStreamInit.
hr = pHtmlDoc->QueryInterface( IID_IPersistStreamInit, (void**)&pPersistStreamInit );
if ( SUCCEEDED(hr) )
{
// Initialize the document.
hr = pPersistStreamInit->InitNew();
if ( SUCCEEDED(hr) )
{
// Load the contents of the stream.
hr = pPersistStreamInit->Load( pStream );
}
pPersistStreamInit->Release();
}
}
}
Using the IPersistStreamInit Interface to Load HTML Content
The IPersistStreamInit interface has InitNew and Load methods that are used to initialize and load an HTML document from a stream. The InitNew method initializes the stream to a known state and the Load method loads the HTML content from the stream.
In the previous sample code, the HTML document is initialized and the HTML content is loaded from the stream.
Note In Microsoft Internet Explorer 5, more than one call to the Load method of the







账号登录