The Study

home |  about

Programming

Introduction

Delphi

Java

Projects

Graphics

Introduction

POV-Ray

Terragen

Music

Introduction

Work in Progress

Oddments

Library

Miscellaneous

HTML in Resource Files

Contents

Introduction

I suspect this is a solution in search of a problem, but it struck me as rather cool, so if anyone can find a use for it, please feel free. And let me know about it!

The idea is very simple: store HTML pages in a resource file, and access them from within a Delphi program, using the Web Browser component.

The implementation is also surprisingly simple. We need to be able to do the following:

Back to contents

Storing HTML in a RES file

Simple. Though it has one small but annoying twist (which I'll come to in a minute).

To start with the most basic example. Suppose you have an HTML page called "index.html". Create a blank .RC file, and enter the following line:

INDEX RT_HTML DISCARDABLE "index.html"

The "INDEX" identifier at the start of the line will be used to refer to this page from within Delphi. "RT_HTML" is the resource type. "DISCARDABLE" is a bit of information for Windows which we don't need to be concerned with (just make sure it is included), and "index.html" is, obviously, the name of the web-page.

Save this as, for example, HTML.RC (well, you can save it as anything you like, but the rest of the examples will assume it is called HTML.RC).

To include other files in the resource, do exactly the same. For example, to include an image called "title.gif", use the following:

TITLE_GIF RT_HTML DISCARDABLE "title.gif"

And now the annoying twist. Within the HTML for the page, you have to refer to any external file (such as the "title.gif" in our example) using the identifier, not the file name, in order for it to found when it the page is viewed from within the program. Conversely, if you want to test the page before compiling it, it won't be able to find the images or other files if you use the identifier.

If anyone can suggest a cunning way round this, I'd be very grateful!

Back to contents

Compiling the resource

Now we need to compile the RC file into a resource file. The easiest way to do this is from the DOS command-prompt, using Delphi's BRCC32 resource compiler (which is normally installed with Delphi, and should be in your path. If it is not, you will have to find it and copy it to somewhere that will be found DOS).

Use the following command:

brcc32 html.rc

You should now have an html.res file.

We'll come back to the resource file later, and talk about adding images and other files. First, let's see how to display the HTML page from Delphi.

Back to contents

Creating the viewer program

Create a new project. Drop a Web Browser component onto the form (you should find this component on the Internet tab of the component palette. If you do not, you are running an older version of Delphi, and I am afraid you are on your own -- I may come back to this subject in another article, and show you how to include a Web Browser component in earlier versions of Delphi. Alternatively you could try using the THTML component that came with Delphi 2.).

In the unit code, find the {$ *.RES} line (this will come almost immediately after the implementation statement. On the line below, add the following:

{$ html.res}

This tells Delphi to include our resource file in the program.

Now, create a FormCreate procedure (double-click on the form, or the FormCreate event property in the Object Inspector).

Enter the following code into the procedure (this is where all the magic happens!):

procedure TForm1.FormCreate(Sender: TObject);
var
  ResURLStr: string;
  ModuleName: array[0..255] of Char;
begin
  GetModuleFileName(hInstance, ModuleName, 255);
  ResURLStr:='res://' + StrPas(ModuleName) + '/RT_HTML/INDEX';
  WebBrowser1.Navigate(ResURLStr);
end;

This creates a string (ResURLStr) which refers to the "index.html" page in the resource file, using the ".res". URL type. Notice that we use "INDEX" to refer to the HTML page, this being the identifier that we assigned to it the original RC file.

Now, run the program. The HTML page should appear in the browser.

For a slightly more elaborate example, you can download my test program (including all the source code and the HTML files and images used in the program):

Back to contents