简体   繁体   中英

How to load HTML string into Chromium browser in “Delphi 10 Seattle”

The following code is written for loading the HTML string (as Stream) into 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;

But i need to achieve this in TChromium browser.

Anyone please help me on this...

You can convert the stream to a string using TStringStream:

function StreamToString(Stream: TStream): String;
begin
    with TStringStream.Create('') do
    try
        CopyFrom(Stream, Stream.Size - Stream.Position);
        Result := DataString;
    finally
        Free;
    end;
end;

That function is available here: http://embarcadero.newsgroups.archived.at/public.delphi.vcl.components.using/200907/0907292775.html

Then call TChromium.LoadString to load the resulting string as you can see in the MiniBrowser demo: https://github.com/salvadordf/CEF4Delphi/blob/7798f97872e4ca6f5246e3fdda04feeb990f88c7/demos/Delphi_VCL/MiniBrowser/uMiniBrowser.pas#L725

MyChromiumcomponent.LoadString(MyString);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM