Author: Tomas Rutkauskas
How to load a stream containing HTML code into a TWebBrowser
Answer:
Solve 1:
Imagine, you want to load a html document into the browser, that is not available
as a file on your hard disk, but linked as a resource into your application.
You can use TResourceStream to make the data accessible for Delphi. But how to get
the data into your WebBrowser? HTML documents implement the IPersistentStreamInit -
interface, what means they support standard methods to accept data from any kind of
stream or write data to any kind of stream.
1 uses ActiveX;
2 3 {Loads the contents of the "Stream" into the "WebBrowser"4 "Stream" should contain HTML code}5 6 procedure LoadStream(WebBrowser: TWebBrowser; Stream: TStream);
7 var8 PersistStreamInit: IPersistStreamInit;
9 StreamAdapter: IStream;
10 MemoryStream: TMemoryStream;
11 begin12 {Load empty HTML document into Webbrowser to make "Document" a valid HTML 13 document}14 WebBrowser.Navigate('about:blank');
15 {wait until finished loading}16 repeat17 Application.ProcessMessages;
18 Sleep(0);
19 until20 WebBrowser.ReadyState = READYSTATE_COMPLETE;
21 {Get IPersistStreamInit - Interface}22 if WebBrowser.Document.QueryInterface(IPersistStreamInit, PersistStreamInit) =
23 S_OK then24 begin25 {Clear document}26 if PersistStreamInit.InitNew = S_OK then27 begin28 {Make local copy of the contents of Stream if you want to use Stream directly,29 you have to consider, that StreamAdapter will destroy it automatically}30 MemoryStream := TMemoryStream.Create;
31 try32 MemoryStream.CopyFrom(Stream, 0);
33 MemoryStream.Position := 0;
34 except35 MemoryStream.Free;
36 raise;
37 end;
38 {Use Stream-Adapter to get IStream Interface to our stream}39 StreamAdapter := TStreamAdapter.Create(MemoryStream, soOwned);
40 {Load data from Stream into WebBrowser}41 PersistStreamInit.Load(StreamAdapter);
42 end;
43 end;
44 end;
45 46 {Let's test. You could also create a TResourceStream or TFileStream etc. here.}47 48 procedure TForm1.Button2Click(Sender: TObject);
49 var50 S: TStringStream;
51 begin52 {To use this code, replace [ ] brackets with <> ones in the following two lines !}53 S := TStringStream.Create('[html][h1]Stream Test[/h1][p]This HTML content ' +
54 'is being loaded from a stream.[/html]');
55 try56 LoadStream(WebBrowser1, S);
57 finally58 S.Free;
59 end;
60 end;
Solve 2:
61 ///////Begin Source62 uses ActiveX;
63 64 function ShowHtml(mWebBrowser: TWebBrowser; mStrings: TStrings): Boolean;
65 var66 vMemoryStream: TMemoryStream;
67 begin68 Result := False;
69 ifnot (Assigned(mStrings) and Assigned(mWebBrowser)) then70 Exit;
71 mWebBrowser.Navigate('about:blank');
72 ifnot Assigned(mWebBrowser.Document) then73 Exit;
74 vMemoryStream := TMemoryStream.Create;
75 try76 mStrings.SaveToStream(vMemoryStream);
77 try78 vMemoryStream.Position := 0;
79 Application.ProcessMessages; // :)80 (mWebBrowser.Document as IPersistStreamInit).Load(
81 TStreamAdapter.Create(vMemoryStream));
82 except83 Exit;
84 end;
85 finally86 vMemoryStream.Free;
87 end;
88 Result := True;
89 end; { ShowHtml }90 ///////End Source91 92 ///////Begin Demo93 94 procedure TForm1.Button1Click(Sender: TObject);
95 begin96 ShowHtml(WebBrowser1, Memo1.Lines);
97 end;
98 99 procedure TForm1.FormCreate(Sender: TObject);
100 begin101 Memo1.Text :=
102 ''#13#10 +
103 'Hello Worlds!'#13#10 +
104 ''#13#10;
105 end;
106 ///////End Demo
Solve 3:
107 procedure AssignDocument(Browser: TWebBrowser; Text: string);
108 var109 Document: OleVariant;
110 {$IFDEF PERSIST_STREAM}111 InStream: TStream;
112 Persist: IPersistStreamInit; {Declared in ActiveX}113 {$ENDIF}114 begin115 {$IFDEF WRITE_FILE}116 Document := LocalServerPath('temp.html');
117 WriteTextFile(Document, Text); {utility function}118 Browser.Navigate2(Document);
119 {$ENDIF}120 {$IFDEF PERSIST_STREAM}121 Document := 'about:blank';
122 Browser.Navigate2(Document);
123 InStream := TStringStream.Create(Text);
124 try125 Persist := (Browser.Document as IPersistStreamInit);
126 Persist.Load(TStreamAdapter.Create(InStream));
127 finally128 InStream.Free;
129 end;
130 {$ENDIF}131 {$IFDEF DISPATCH_DOC}132 Document := 'about:blank';
133 Browser.Navigate2(Document);
134 Document := Browser.Document as IDispatch;
135 Document.Open;
136 try137 Document.write(Text);
138 finally139 Document.Close;
140 end;
141 {$ENDIF}142 end;