简体   繁体   中英

How can I load an HTML string into a Firemonkey TWebBrowser?

I'm trying to use the Firemonkey TWebBrowser to load some HTML that is generated at runtime. The HTML is a Delphi string.

I've looked at some code from: http://delphi.about.com/cs/adptips2004/a/bltip0104_4.htm

procedure WBLoadHTML(WebBrowser: TWebBrowser; HTMLCode: string) ;
var
   sl: TStringList;
   ms: TMemoryStream;
begin
   WebBrowser.Navigate('about:blank') ;
   while WebBrowser.ReadyState < READYSTATE_INTERACTIVE do
     Application.ProcessMessages;

   if Assigned(WebBrowser.Document) then begin
     sl := TStringList.Create;
     try
       ms := TMemoryStream.Create;
       try
         sl.Text := HTMLCode;
         sl.SaveToStream(ms) ;
         ms.Seek(0, 0) ;
         (WebBrowser.Document as IPersistStreamInit).Load(TStreamAdapter.Create(ms)) ;
       finally
         ms.Free;
       end;
     finally
       sl.Free;
     end;
   end;
end;

procedure TForm1.FormCreate(Sender: TObject) ;
var
  sHTML : string;
begin
  sHTML := '<a href="http://delphi.about.com">GOTO</a>' +
           '<b>About Delphi Programming</b>';
  WBLoadHTML(WebBrowser1,sHTML) ;
end;

but this appears to be designed for a VCL application which is incompatible with Firemonkey TWebBrowser .

How can I achieve the same thing as this code example but using the Firemonkey control?

使用WebBrowser LoadFromStrings方法。

You could implement this functionality in a DLL...detail and downloadable example here .

Another option is to consider this open-source TWebBrowserEx project . It actually will use the platform's normal web browser functionality. However, in the case of Windows it means that in your FMX project you will be using the VCL for the web browser support.. .which may or may not have unintended effects.

This class provide WebBrowser for All-Platform FireMonkey Applications.

Platform => Component

  • Windows => IWebBrowser(IE)
  • OS X => WebView(Safari)
  • iOS => WebView
  • Android => WebView

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