TWebBrowser中能不能LoadFromStream?

Jimy 2000-01-28 03:38:00
问了几个问题都得到满意的回答,心里真是高兴,
真是问对地方了...
这个问题,不知谁能帮忙?
1.我从磁盘或Internet上取得文件
2.把文件变成TMemoryStream,然后判断它是不是HTML格式
3.如果是HTML,就用TWebBrowser来显示,这时我已经忘掉
文件是从磁盘来的还是Internet来的,如果我记住来源把URL
给TWebBrowser的话,如果文件在Internet上,那么又要再
DL一次,(起先是用Winsock拿的,所以没有Cache),如果
把TMemoryStream写成临时文件交给TWebBrowser的话,如果
万一我都是从磁盘拿的,又都在磁盘写临时文件,那又显得极为
浪费,最好的方法是TWebBrowser能从内存读取数据,
不知道能不能实现,或者有更好的办法?
期待回答
...全文
231 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
JGTM2000 2000-08-31
  • 打赏
  • 举报
回复
意思对了,复杂复杂了……

var
OleStream: IStream;
DelphiStream: TStream;

DelphiStream := TStringStream.Create('<h1>I Love Delphi!</h1>'); // or any other TStream class instance

OleStream := TOleStream.Create(OleStream);

try

OleStream.CopyFrom(DelphiStream, DelphiStream.Size);

// of course if you have already valid IStream which contains what you want to load, use it directly. e.g URLOpenStream...

with (WebBrowser.Document as IPersistStreamInit) do
begin
OleCheck(InitNew);
OleCheck(Load(OleStream)); // OleStream is a IStream interface pointer
end;

finally

OleStream.Free;
DelphiStream.Free;

end;

最关键的一点,sundaynews说了,仅用流不能保留文档参考位置信息,所以这种情况下要用到Moniker了。即不用IPersistStreamInit接口,而用IPersistMoniker来加载IMoniker实现的对象的内容(详见URL Moniker文档)。
alin 2000-08-30
  • 打赏
  • 举报
回复
//写HTML
procedure SetHtml(const WebBrowser:TWebBrowser; const Html: string);
var
Stream: IStream;
hHTMLText: HGLOBAL;
psi: IPersistStreamInit;
begin
if not Assigned(WebBrowser.Document) then Exit;

hHTMLText := GlobalAlloc(GPTR, Length(Html) + 1);
if 0 = hHTMLText then RaiseLastWin32Error;

CopyMemory(Pointer(hHTMLText),
PChar(Html), Length(Html));

OleCheck(CreateStreamOnHGlobal
(hHTMLText, True, Stream));
try
OleCheck(WebBrowser.Document.
QueryInterface(IPersistStreamInit, psi));
try
OleCheck(psi.InitNew);
OleCheck(psi.Load(Stream));
finally
psi := nil;
end;
finally
Stream := nil;
end;
end;

//读HTML
function GetHtml(const WebBrowser:TWebBrowser): string;
const
BufSize = $10000;
var
Size: Int64;
Stream: IStream;
hHTMLText: HGLOBAL;
psi: IPersistStreamInit;
begin
if not Assigned(WebBrowser.Document) then Exit;

OleCheck(WebBrowser.Document.QueryInterface
(IPersistStreamInit, psi));
try
//OleCheck(psi.GetSizeMax(Size));
hHTMLText := GlobalAlloc(GPTR, BufSize);
if 0 = hHTMLText then RaiseLastWin32Error;

OleCheck(CreateStreamOnHGlobal(hHTMLText,
True, Stream));
try
OleCheck(psi.Save(Stream, False));

Size := StrLen(PChar(hHTMLText));
SetLength(Result, Size);
CopyMemory(PChar(Result), Pointer(hHTMLText), Size);
finally
Stream := nil;
end;
finally
psi := nil;
end;
end;

sundaynews 2000-01-29
  • 打赏
  • 举报
回复
没见过这种方法,最好还是用临时文件,否则你HTML文档中的链接和JAVASCRIPT怎么办?而且我见过的软件中的实现方法也是用临时文件方法做的。
Jimy 2000-01-28
  • 打赏
  • 举报
回复
再稍微提示一点点,比如说是用TWebBrowser的方法,
还是用文档对象的方法?如果用文档对象的话,那么TWebBrowser又该
如何操作?
tiger 2000-01-28
  • 打赏
  • 举报
回复
天哪, delphi5的关于TWebBrower的help太简略了.
我只能说, 可以做到, 要看microsoft的Dynamic HTML reference.
我没有这个

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧