Link to home
Start Free TrialLog in
Avatar of treqw
treqw

asked on

TWebBrowser LOAD FROM MEMO

TWebBrowser LOAD FROM MEMO

How to load WebBrowser1 HTML from Memo1.Text (HTML TEXT) ?

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Brian Mulder
Brian Mulder
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
uses ActiveX;

{Loads the contents of the "Stream" into the "WebBrowser"
"Stream" should contain HTML code}
procedure LoadStream(WebBrowser: TWebBrowser; Stream: TStream);
var
  PersistStreamInit: IPersistStreamInit;
  StreamAdapter: IStream;
  MemoryStream: TMemoryStream;
begin
  {Load empty HTML document into Webbrowser to make "Document" a valid HTML document}
  WebBrowser.Navigate('about:blank');
  {wait until finished loading}
  repeat
    Application.ProcessMessages;
    Sleep(0);
  until
    WebBrowser.ReadyState = READYSTATE_COMPLETE;
  {Get IPersistStreamInit - Interface}
  if WebBrowser.Document.QueryInterface(IPersistStreamInit, PersistStreamInit) = S_OK then
  begin
    {Clear document}
    if PersistStreamInit.InitNew = S_OK then
    begin
      {Make local copy of the contents of Stream if you want to use Stream directly, you have to
      consider, that StreamAdapter will destroy it automatically}
      MemoryStream:= TMemoryStream.Create;
      try
        MemoryStream.CopyFrom(Stream, 0);        
        MemoryStream.Position:= 0;
      except
        MemoryStream.Free;
        raise;
      end;
      {Use Stream-Adapter to get IStream Interface to our stream}
      StreamAdapter:= TStreamAdapter.Create(MemoryStream, soOwned);
      {Load data from Stream into WebBrowser}
      PersistStreamInit.Load(StreamAdapter);
    end;
  end;
end;


{Let's test. You could also create a TResourceStream or TFileStream etc. here.}
procedure TForm1.Button2Click(Sender: TObject);
var
  S: TStringStream;
begin
  {To use this code, replace [ ] brackets with <> ones in the following two lines !}
  S:= TStringStream.Create(Memo1.Text)
  try
    LoadStream(WebBrowser1, S);
  finally
    S.Free;
  end;
end;

Best Regards

Cesario
Avatar of delpro
delpro

go to http://www.pbear.com/ 
and download the HTML Display Components

This is how easy it is.

   HTMLViewer1.LoadFromString(memo1.Text);
nice :)
another way would be to install EmbededWB from http://www.euromind.com/iedelphi/
wow, thanks for the grade