About Delphi Programming|
From Zarko Gajic, your Guide to Delphi Programming
|Web Site inside a Delphi EXE|
How to store Web-style content inside a Delphi application.
This is the third article in the series about storing more than just executable code inside a Delphi application.
In the introductory article we saw how Delphi uses standard Windows resource files like icons, bitmaps and cursors. As explained, resource files that store such kind of data can be created with Delphi's Image Editor. The second article explained how to use sound files, video clips, animations and more generally any kind of binary files in a Delphi executable - we placed a mp3 file inside a Delphi exe. As stated, we use Borland Resource Compiler (BRCC32.exe) to create a resource file that gets linked with an executable file.
Many of you have asked how your application can be a container for any Web-style content, including HTML resources and pictures that are part of your project.
This articles will show you how HTML and associated files (pictures) can easily be included within a Delphi application. As a result, you simply have to distribute an EXE file that includes HTML pages, as it would do with icons and cursors.
Creating a HTML page
For the start we have to assemble a (simple) html page. Use your favorite HTML editor and create one page with one associated picture. I'll name mine aboutindex.htm.
Note that when you add a picture tag inside a htm page it looks something like:
<img src="/library/graphics/adp.gif" ...>
We have to alter the IMG tags so that the SRC attribute equals the name we are to give to a picture in a resource:
<img src="ABOUTDP" ...>
My HTML code looks like:
<HTML><HEAD><TITLE>HTML inside a Delphi exe</TITLE></HEAD><BODY>|
This is a HTML Delphi resource test:<br>
<img src="ABOUTDP" width=106 height=58 border=0 alt="">
Creating and compiling a resource file
Remember that to create a new resource script file, you have to:
1. Create a new text file in your projects directory.
2. Rename it to AHTMLDelphi.rc.
3. Write down the following two lines of text in the AHTMLDelphi.rc file.
DELPHIINDEX HTML "c:\Delphi\projects\aboutindex.htm"|
ABOUTDP GIF "c:\library\graphics\adp.gif"
Note: the resource type "HTML" is RT_HTML, predefined as the resource type "23". This is the default resource type for the RES protocol.
In this way we have prepared one HTML page and one GIF picture to be included in the binary code of our EXE module.
The next step is to compile the .rc file. To compile the AHTMLDelphi.rc file to a .res file execute this command at the command prompt (in the projects directory):
The final step is to add the following compiler directive to a unit in your project. RES file must be included in the program's build by adding a line like this:
Displaying inside a Web browser
When you have the application's exe (let's call it: myhtmldelphi.exe) the HTML resource contained within can be accessed via the RES: protocol. Run Internet Explorer and, in the Address bar, type the following:
This should result in:
That's it. If you have any questions; like how use the HREF tag inside a html resource, or how to display the html page inside a Delphi form; please post them on the Delphi Programming Forum!
Zarko Gajic, BSCS
About.com Guide to Delphi Programming
free newsletter: http://delphi.about.com/library/blnewsletter.htm