分享
 
 
 

Loading HTML content from a Stream

王朝vc·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

曾经找了很多地方,居然在msdn找到了,看来不得不佩服msdn,我是想在delphi下实现把html流放到WebBrowser控件,看了别人的解释,还是不清楚,只好自己来找。没有翻译过来,是为了原汁原味给大家。

The IPersistStreamInit interface, and its associated methods, can be used to load HTML content from a stream using the WebBrowser control and Microsoft® Visual C++®.

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 IWebBrowser2::Navigate2 method of the IWebBrowser2 interface enables you to navigate the browser to a URL. In the following sample code, the IWebBrowser2::Navigate2 method is used to navigate to the about:blank page. Navigating to this empty page ensures that MSHTML is loaded and that the HTML elements are available through the Dynamic HTML (DHTML) Object Model.

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 DWebBrowserEvents2::DocumentComplete event of the WebBrowser control. This event may be fired once for each frame in the page, and once when the top frame of the document is loaded. You can determine if the DWebBrowserEvents2::DocumentComplete event is for the top frame by comparing the IDispatch interface pointer passed by this event with that of the WebBrowser control.

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 IWebBrowser2::get_Document property on the WebBrowser control retrieves the document object that represents the DHTML Object Model for the top frame. MSHTML implements the IPersistStreamInit interface to provide the ability to load and save HTML using a stream, through the document object. The IDispatch interface for the document object can be queried for the IPersistStreamInit interface pointer using QueryInterface with an identifier of IID_IPersistStreamInit, as shown in the following code example.

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 IPersist interfaces is supported. In earlier versions, only one call to Load per instance of MSHTML is supported.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有