|
|
 |
 |
|
|
 |
 |
|
Join the Discussion
|
"Post your views, comments, questions and doubts to this article."
Discuss!
|
|
 |
 |
|
|
 |
 |
 |
This is the fourth article in the series about storing more than just executable code inside a Delphi application.
Introductory article described resources as binary data that a resource compiler or developer ads to an application's executable file.
A resource can be either standard or defined. The data in a standard resource describes an icon, cursor, menu, dialog box, bitmap, enhanced metafile, font, accelerator table, message-table entry, string-table entry, or version. An application-defined resource, also called a custom resource, contains any data required by a specific application (another EXE, GIF image, MP3 song, etc.).
RESource only DLL
Dynamic link libraries contain sharable code or resources, they provide the ability for multiple applications to share a single copy of a routine (or resource) they have in common.
Creating a resource DLL
In order to build a DLL with resources as its only content, you need to create and build an empty DLL project, which contains a resource link reference to the .res file that contains your resources.
Here are the steps to follow:
- Create a RC file "describing" resources you are to place in a DLL. Here's an example ("adpdllresources" is the name of the RC ASCII file) - one ICON and one GIF image are added to the RC file:
|
adpdllresources.rc
aboutlogo RCDATA aboutlogo.gif
factory ICON FACTORY.ICO
|
- Compile the RC file into a RES file with the BRCC32 resource compiler.
- Create an empty DLL project. Save it as "adpResources.dpr" - when compiled the DLL will be named "adpResources.dll". Here's the full source to the DLL project (only one file, and yes only 4 lines):
library adpResources;
{$R adpdllresources.RES}
begin
end.
|
- Compile your DLL (make sure the "adpdllresources.RES" is in the same folder as the DLL)
Once the DLL with resources is created, you can use it inside your Delphi applications. Note that since the resources are inside a DLL, any application can use 'em (no need to be developed with Delphi).
Using your resource only DLL
To use you resource only dynamic link library, simply load the DLL and the resources you wish to use.
Here are the steps to follow:
- Create a new Delphi project. By default, Delphi ads one empty form to the project. Save the project.
- Copy the DLL with resources ("adpResources.DLL") into the folder where your new application was saved
- Load the resource you are after...
Here's how to load the "factory" icon and draw it on a Form1's Canvas (when Button1:TButton was clicked).
procedure TForm1.Button1Click(Sender: TObject);
const
resICON = 'factory';
var
h : THandle;
Icon : HIcon;
begin
h := LoadLibrary('adpResources.DLL');
try
if h <> 0 then
begin
Icon := LoadIcon(h, resICON);
DrawIcon(Canvas.Handle, 10, 10, Icon);
end
else
begin
ShowMessage('Load Resource DLL FAILED!');
end;
finally
FreeLibrary(h);
end;
end;
|
If you have added GIF support to Delphi, you can grab the GIF image stored in resource DLL and draw it also:
procedure TForm1.Button2Click(Sender: TObject);
const
resGIF = 'aboutlogo';
var
h : THandle;
gif : TGifImage;
r:TRect;
begin
h := LoadLibrary('adpResources.DLL');
try
if h <> 0 then
begin
gif := TGIFImage.Create;
try
gif.LoadFromResourceName(h,resGIF);
gif.Paint(Canvas,
Form1.ClientRect,
[goDirectDraw]);
finally
gif.Free;
end;
end
else
begin
ShowMessage('Load Resource DLL FAILED!');
end;
finally
FreeLibrary(h);
end;
end;
|
And that's all. Download the sample project ... and try for yourself - it's easy and fun.
|