1. Technology
Send to a Friend via Email
Resource Files Made Easy
How Delphi uses standard Windows-format resource files: icons, bitmaps and cursors.
More of this Feature
Part 1: Resource Files Made Easy
Part 2: Inside the EXE (compiling .rc files)
Part 3: Web Site inside a Delphi EXE
Part 4: Resource only DLL
Part 5: StringTable resources
Part 6: Resource Managers
Part 7: Fixing the "Duplicate resource" error
Join the Discussion
"Post your views, comments, questions and doubts to this article."
Discuss!
Related Resources
HTML in exe
WAV, MP3 and AVI in exe
Icons, glyphs

Resources
Bitmaps, icons, cursors, string tables... Every Windows program uses resources. Resources are those elements of a program that support the program but are not executable code. In this article, we will walk through some examples of the use of bitmaps, icons and cursors from resources.

Placing resources in the .exe file has two main advantages:
- The resources can be accessed more quickly because it takes less time to locate a resource in the executable file than it does to load it from a disk file.
- The program file and resources can be contained in a single unit (the .exe file) without the need for a lot of supporting files.

The Image Editor
First off all, we need to create a resource file. Default extension for resource files is .RES. Resource files can be created with Delphi's Image Editor. Read in Help files on working with the Image Editor.

Resource File

You can name the resource file anything you want, as long as it has the extension ".RES" and the filename without the extension is not the same as any unit or project filename. This is important, because by default, each Delphi project that compiles into an application has a resource file with the same name as the project file, but with the extension .RES. It's best to save the file to the same directory as your project file.

Including resources in applications
In order to access our own resource file we have to tell Delphi to link our resource file in with our application. This is accomplished by adding a compiler directive to the source code. This directive needs to immediately follow the form directive, like the following:

{$R *.DFM} 
{$R DPABOUT.RES}

Do not accidentally erase {$R *.DFM} part, as this is the line of code that tells Delphi to link in the form's visual part. When you choose bitmaps for speed buttons, Image components or Button components, Delphi includes the bitmap file you chose as part of the form's resource. Delphi isolates your user interface elements into the .DFM file.

To actually use the resource, you must make a few Windows API calls. Bitmaps, cursors and icons stored in RES files can be retrieved by using the API functions LoadBitmap, LoadCursor and LoadIcon respectively.

Pictures in resources
The first example shows how to load a bitmap stored as a resource and display it in a TImage component.

procedure TfrMain.btnCanvasPic(Sender: TObject);
var bBitmap : TBitmap;
begin
 bBitmap := TBitmap.Create;
 try
  bBitmap.Handle := LoadBitmap(hInstance, 'ATHENA');
  Image1.Width := bBitmap.Width;
  Image1.Height := bBitmap.Height;
  Image1.Canvas.Draw(0,0,bBitmap);
 finally
  bBitmap.Free;
 end;
end;

Note: If the bitmap that is to be loaded is not in the resource file, the program will still run, it just won't display the bitmap. This situation can be avoided by testing to see if the bBitmap.Handle is zero after a call to LoadBitmap() and taking the appropriate steps. The try/finally part in the previous code doesn't solve this problem, it is just here to make sure that the bBitmap is destroyed and its associated memory is freed.

Another way we can use to display a bitmap from resource is as follows:

procedure TfrMain.btnLoadPicClick(Sender: TObject);
begin
 Image1.Picture.Bitmap.
   LoadFromResourceName(hInstance,'EARTH');
end;

Cursors in resources
As discussed in the previous article Screen.Cursors[] is an array of cursors supplied by Delphi. By using resource files, we can add custom cursor to the Cursors property. Unless we wish to replace any of the defaults, the best strategy is to use cursor numbers starting from 1.

procedure TfrMain.btnUseCursorClick(Sender: TObject);
 const NewCursor = 1;
begin
 Screen.Cursors[NewCursor] :=
    LoadCursor(hInstance,'CURHAND');
 Image1.Cursor := NewCursor;
end;

Icons in Resources
If we look at Delphi's Project-Options-Application settings, we can find that Delphi supplies the default icon for a project. This icon represents the application in the Windows Explorer and when the application is minimized. We can easily change this by clicking the 'Load Icon' button.

If we want, for example, to animate the program's icon when the program is minimized than the following code will do the job.
For the animation, we need a TTimer component on a form. The code loads two icons from resource file into an array of TIcon objects; this array needs to be declared in the public part of the main form. We'll also need NrIco, that is an Integer type variable, declared in the public part. The NrIco is used to keep track of the next icon to show.

public
    nrIco : Integer;
  MinIcon : array[0..1] of TIcon;
...
procedure TfrMain.FormCreate(Sender: TObject);
begin
 MinIcon[0]:=TIcon.Create;
 MinIcon[1]:=TIcon.Create;
 MinIcon[0].Handle:=LoadIcon(hInstance,'ICOOK');
 MinIcon[1].Handle:=LoadIcon(hInstance,'ICOFOLD');
 NrIco:=0;
 Timer1.Interval:=200;
end;
...
procedure TfrMain.Timer1Timer(Sender: TObject);
begin
 if IsIconic(Application.Handle) then begin
  NrIco:=(NrIco+1) mod 2;
  Application.Icon:=MinIcon[NrIco];
 end;
end;
...
procedure TfrMain.FormDestroy(Sender: TObject);
begin
 MinIcon[0].Free;
 MinIcon[1].Free;
end;

In the Timer1.OnTimer event handler, IsMinimized function is used to see whether we need to animate our main icon or not. Better way of accomplishing this would be to capture the maximize/minimize buttons and than act.

Final words
We can place anything (well, not everything) in resource files. This article has shown you how to use resources to use/display bitmap, cursor or an icon in your Delphi application. If you are interested in what else can be placed inside one resource file try the links at the bottom of the article.

Note: When we save a Delphi project to the disk, Delphi automatically creates one .RES file that has the same name as the project (if nothing else, the main icon of the project is inside). Although we can alter this resource file, this is not advisable.

Don't forget to download this project and try to play with it.

Resource Project
Related Video
How to Share Files between your Mac and PC
How to Share Media Files With Windows Media Player

©2014 About.com. All rights reserved.