编写以下代码是为了将HTML字符串(作为Stream(加载到TWebBrowser
:
procedure TfrmTestDialog.LoadDocumentFromStream(const Stream: TStream);
var
PersistStreamInit: IPersistStreamInit;
StreamAdapter: IStream;
begin
if not Assigned(dWebBrowser.Document) then Exit;
// Get IPersistStreamInit interface on document object
if dWebBrowser.Document.QueryInterface(IPersistStreamInit,
PersistStreamInit) = S_OK then
begin
// Clear document
if PersistStreamInit.InitNew = S_OK then
begin
// Get IStream interface on stream
StreamAdapter:= TStreamAdapter.Create(Stream);
// Load data from Stream into WebBrowser
PersistStreamInit.Load(StreamAdapter);
end;
end;
end;
但我需要在 TChromium 浏览器中实现这一点。
任何人请帮我...
您可以使用 TStringStream 将流转换为字符串:
function StreamToString(Stream: TStream): String;
begin
with TStringStream.Create('') do
try
CopyFrom(Stream, Stream.Size - Stream.Position);
Result := DataString;
finally
Free;
end;
end;
该函数可在此处获得: http://embarcadero.newsgroups.archived.at/public.delphi.vcl.components.using/200907/0907292775.html
然后调用 TChromium.LoadString 来加载生成的字符串,如您在 MiniBrowser 演示中看到的那样: https://github.com/salvadordf/CEF4Delphi/blob/7798f97872e4ca6f5246e3fdda04feeb990f88c7/demos/Delphi_VCL/MiniBrowser/uMiniBrowser.pas#L725
MyChromiumcomponent.LoadString(MyString);