如何用 CppWebBrowser 直接打开放在 TMemoryStream 中的html格式的文件?
如何用 CppWebBrowser 直接打开放在 TMemoryStream 中的html格式的文件?
[解决办法]
#include <mshtml.h>
void __fastcall SetHtmlContent(IHTMLDocument2 *spDoc, TMemoryStream *ms)
{
if(spDoc == NULL)
return;
IStream *pStream;
HGLOBAL hHTMLText;
IPersistStreamInit *psi;
hHTMLText = GlobalAlloc(GPTR, ms-> Size + 1);
if(0 == hHTMLText)
{
ShowMessage( "内存分配发生错误! ");
return;
}
CopyMemory(hHTMLText, ms-> Memory, ms-> Size);
OleCheck(CreateStreamOnHGlobal(hHTMLText, true, &pStream));
try
{
OleCheck(spDoc-> QueryInterface(IID_IPersistStreamInit, (void **)&psi));
try
{
OleCheck(psi-> Load(pStream));
}
__finally
{
psi-> Release();
}
}
__finally
{
pStream-> Release();
}
}
// 使用例子:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
CppWebBrowser1-> Navigate(WideString( "about:blank "));
IHTMLDocument2 *spDoc(NULL);
if(!CppWebBrowser1-> Busy && CppWebBrowser1-> Document != NULL)
CppWebBrowser1-> Document-> QueryInterface(
::IID_IHTMLDocument2, reinterpret_cast <void **> (&spDoc));
if(spDoc == NULL)
return;
TMemoryStream *ms = new TMemoryStream;
ms-> LoadFromFile( "C:\\ccrun\\123.htm ");
SetHtmlContent(spDoc, ms);
spDoc-> Release();
delete ms;
}
上面例子中123.htm的内容为:
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> New Page 1 </title>
</head>
<body>
<h2> 妖哥万岁! </h2>
</body>
</html>