Articles   Members Online: 3
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
Load a stream containing HTML code into a TWebBrowser Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
25-Aug-02
Category
ActiveX
Language
Delphi 5.x
Views
201
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			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   var
8     PersistStreamInit: IPersistStreamInit;
9     StreamAdapter: IStream;
10    MemoryStream: TMemoryStream;
11  begin
12    {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    repeat
17      Application.ProcessMessages;
18      Sleep(0);
19    until
20      WebBrowser.ReadyState = READYSTATE_COMPLETE;
21    {Get IPersistStreamInit - Interface}
22    if WebBrowser.Document.QueryInterface(IPersistStreamInit, PersistStreamInit) = 
23  S_OK then
24    begin
25      {Clear document}
26      if PersistStreamInit.InitNew = S_OK then
27      begin
28        {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        try
32          MemoryStream.CopyFrom(Stream, 0);
33          MemoryStream.Position := 0;
34        except
35          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  var
50    S: TStringStream;
51  begin
52    {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    try
56      LoadStream(WebBrowser1, S);
57    finally
58      S.Free;
59    end;
60  end;



Solve 2:

61  ///////Begin Source
62  uses ActiveX;
63  
64  function ShowHtml(mWebBrowser: TWebBrowser; mStrings: TStrings): Boolean;
65  var
66    vMemoryStream: TMemoryStream;
67  begin
68    Result := False;
69    if not (Assigned(mStrings) and Assigned(mWebBrowser)) then
70      Exit;
71    mWebBrowser.Navigate('about:blank');
72    if not Assigned(mWebBrowser.Document) then
73      Exit;
74    vMemoryStream := TMemoryStream.Create;
75    try
76      mStrings.SaveToStream(vMemoryStream);
77      try
78        vMemoryStream.Position := 0;
79        Application.ProcessMessages; // :)
80        (mWebBrowser.Document as IPersistStreamInit).Load(
81          TStreamAdapter.Create(vMemoryStream));
82      except
83        Exit;
84      end;
85    finally
86      vMemoryStream.Free;
87    end;
88    Result := True;
89  end; { ShowHtml }
90  ///////End Source
91  
92  ///////Begin Demo
93  
94  procedure TForm1.Button1Click(Sender: TObject);
95  begin
96    ShowHtml(WebBrowser1, Memo1.Lines);
97  end;
98  
99  procedure TForm1.FormCreate(Sender: TObject);
100 begin
101   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 var
109   Document: OleVariant;
110 {$IFDEF PERSIST_STREAM}
111   InStream: TStream;
112   Persist: IPersistStreamInit; {Declared in ActiveX}
113 {$ENDIF}
114 begin
115 {$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   try
125     Persist := (Browser.Document as IPersistStreamInit);
126     Persist.Load(TStreamAdapter.Create(InStream));
127   finally
128     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   try
137     Document.write(Text);
138   finally
139     Document.Close;
140   end;
141 {$ENDIF}
142 end;



			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC