Including (Linking/Embeding) Resources to Executables
With the Borland's Resource Compiler we have created the AboutDelphi.res resource file. The next step is to add the following compiler directive to a unit in your project, immediately after the form directive (below the implementation key word). {$R *.DFM} {$R AboutDelphi.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.
After the .RES file is linked to the executable file, the application can load its resources at run time as needed. To actually use the resource, you'll have to make a few Windows API calls.
In order to follow the article you'll need a new Delphi project with a blank form (the default new project). Of course add the {$R AboutDelphi.RES} directive to the main form's unit. It's finally time to see how to use resources in a Delphi application. As mentioned above, in order to use resources stored inside an exe file we have to deal with API. However, several methods can be found in the Delphi help files that are "resource" enabled.
For example take a look at the LoadFromResourceName method of a TBitmap object. This method extracts the specified bitmap resource and assigns it TBitmap object. This is *exactly* what LoadBitmap API call does. As always Delphi has improved an API function call to suit your needs better.
Playing Animations from Resources
To show the animation inside the cool.avi (remember it was defined in the .rc file) we'll use the TAnimate component (Win32 palette) - drop it on to the main form. Let the name of the Animate component be the default one: Animate1. We'll use the OnCreate event of a form to display the animation: procedure TForm1.FormCreate(Sender: TObject) ;
begin
with Animate1 do begin
ResName := 'cool';
ResHandle := hInstance;
Active := TRUE;
end;
end;
That simple! As we can see, in order to play an animation from a resource we have to use the use the ResHandle, ResName or ResID properties of TAnimate component. After setting ResHandle, we set the ResName property to specify which resource is the AVI clip that should be displayed by the animation control. Asigning True to the Active property simply starts the animation.
Playing WAVs
Since we have placed two WAVE files in our executable, we will now see how to grab a song inside the exe and play it. Drop a button (Button1) on a form and assign the following code to the OnClick event handler: uses mmsystem;
...
procedure TForm1.Button1Click(Sender: TObject) ;
var
hFind, hRes: THandle;
Song: PChar;
begin
hFind := FindResource(HInstance, 'MailBeep', 'WAVE') ;
if hFind <> 0 then begin
hRes:=LoadResource(HInstance, hFind) ;
if hRes <> 0 then begin
Song:=LockResource(hRes) ;
if Assigned(Song) then SndPlaySound(Song, snd_ASync or snd_Memory) ;
UnlockResource(hRes) ;
end;
FreeResource(hFind) ;
end;
end;
This approach uses several API calls to load a WAVE type resource named MailBeep and play it. Note: you cal use Delphi to play system predefined sounds.
Playing MP3s
The only MP3 file in our resource has the name Intro. Since this resource is of a RCDATA type we'll use another technique to get and play the mp3 song. Just in case you don't know that Delphi can play MP3 songs read the "Build your own WinAmp" article. Yes, that's right, the TMediaPlayer can play the mp3 file.Now, add the TMediaPlayer component to a form (name: MediaPlayer1) and add a TButton (Button2). Let the OnClick event look like:
procedure TForm1.Button2Click(Sender: TObject) ;
var
rStream: TResourceStream;
fStream: TFileStream;
fname: string;
begin
{this part extracts the mp3 from exe}
fname := ExtractFileDir(Paramstr(0))+'Intro.mp3';
rStream := TResourceStream.Create(hInstance, 'Intro', RT_RCDATA) ;
try
fStream := TFileStream.Create(fname, fmCreate) ;
try
fStream.CopyFrom(rStream, 0) ;
finally
fStream.Free;
end;
finally
rStream.Free;
end;
{this part plays the mp3}
MediaPlayer1.Close;
MediaPlayer1.FileName:=fname;
MediaPlayer1.Open;
end;
This code, with the help of TResourceStream, extracts the mp3 song from the exe and saves it to the applications working directory. The name of the mp3 file is intro.mp3. Then simply assign that file to the FileName property of a MediaPlayer and play the song.
One minor *problem* is that the application creates a mp3 song on a user machine. You could add a code that deletes that file before the application is terminated.
