Title: From resources to TWebBrowser
Question: Ever wanted to fast do your own exe containing HTML pages.
This way of doing, lets you easy manage HTML files included into your EXE in a TWebBrowser. I think it's cool!
Answer:
First of all you need to include those two units:
uses mshtml, activex;
Next, you must insert a TWebBrowser (called "WB" in this article) into your form (called frmMain in this article).
You must add 2 public procedures, called "InternalPage" and "ResourcePage", to your form. After that, the declaration should look like this:
...
Type
TfrmMain = class(TForm)
wb: TWebBrowser;
...
public
procedure InternalPage(const HTMLString:string);
procedure ResourcePage(const Name:string);
...
end;
The implementation of that procedures is this:
procedure tfrmmain.InternalPage(const HTMLString:string);
var
pagesource : OleVariant;
HTMLDocument : IHTMLDocument2;
begin
if not(Assigned(WB.Document)) then
WB.Navigate('about:blank', EmptyParam, EmptyParam, EmptyParam, EmptyParam);
HTMLDocument := WB.Document as IHTMLDocument2;
pagesource := VarArrayCreate([0, 0], varVariant);
pagesource[0] := HTMLString;
HTMLDocument.Write(PSafeArray(TVarData(pagesource).VArray));
HTMLDocument.Close;
end;
procedure TfrmMain.ResourcePage(const Name:string);
var
RS : TResourceStream;
SL : TStringList;
begin
try
RS:=TResourceStream.create(HInstance, uppercase(trim(Name)), RT_RCDATA);
try
SL:=TStringList.create;
try
SL.LoadFromStream(RS);
InternalPage(SL.Text);
finally
SL.Destroy;
end;
finally
RS.Destroy;
end;
except
on e:exception do ;
end;
end;
The next move is to manage a little the BeforeNavigate2 event of our TWebBrowser. You only need to make this:
procedure TfrmMain.wbBeforeNavigate2(Sender: TObject;
const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData,
Headers: OleVariant; var Cancel: WordBool);
var
pagename:string;
begin
if lowercase(trim(url))='about:blank' then exit;
if pos('internal://',lowercase(URL))=1 Then
begin
cancel:=true;
pagename:=copy(URL,(pos('://',URL)+3),maxint);
if length(pagename)0 then
if pagename[length(pagename)]='/' then
delete(pagename,length(pagename),1);
ResourcePage(pagename);
end;
end;
Now, add, for example tro pages as RT_RCDATA into project's resources (Project\Resources menu into Delphi IDE, then right click on the toolwindow, and select New\User Data), called for example "FIRSTHTMLPAGE" and "SECONDHTMLPAGE".
On the Create event of your form you need to load the first page:
procedure TfrmMain.FormCreate(Sender: TObject);
begin
ResourcePage('FIRSTHTMLPAGE');
end;
That's all.
By the way: you'll need to refer all links in your page to "internal://" + name of the resource containing the page for it to work.
Hope it'll be usefull for you all.