|
Loading HTML content from a Stream(3) 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
|