1. Computing

How to load HTML directly to a WebBrowser

By

Here's how to load "static" HML code from a string into a TWebBrowser:

Usage: Simply drop an instance of TWebBrowser component on a form, HTML code gets loaded in the OnCreate event for a form

~~~~~~~~~~~~~~~~~~~~~~~~~
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;
~~~~~~~~~~~~~~~~~~~~~~~~~

Delphi tips navigator:
» How to draw transparent text on Windows Desktop.
« Enter key or Return key?

©2014 About.com. All rights reserved.