How Delphi Uses Resource Files

Format
mla apa chicago
Your Citation
Gajic, Zarko. "How Delphi Uses Resource Files." ThoughtCo, Mar. 30, 2017, thoughtco.com/how-delphi-uses-resource-files-4077232. Gajic, Zarko. (2017, March 30). How Delphi Uses Resource Files. Retrieved from https://www.thoughtco.com/how-delphi-uses-resource-files-4077232 Gajic, Zarko. "How Delphi Uses Resource Files." ThoughtCo. https://www.thoughtco.com/how-delphi-uses-resource-files-4077232 (accessed September 26, 2017).
Bitmap Hand and Light Switch
Jeffrey Coolidge/Stone/Getty Images

From bitmaps to icons to cursors to 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.

Location of 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 of all, we need to create a resource file. The default extension for resource files is .RES. Resource files can be created with Delphi's Image Editor.

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 a resource is as follows:

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

Cursors in Resources

Screen.Cursors[] is an array of cursors supplied by Delphi. By using resource files, we can add custom cursors 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, then 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. A 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.

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.