Here are the answers to both questions. How to
load a WAVE resource, and ho to load a JPEG
resource.
Joe Hecht
-----------------------------------------------
The following demonstrates creating a resource file containing
a JPEG image, and loading the JPEG file from the resource file.
The resulting JPEG image is displayed in a Image component.
1) Create a text file with the extension of ".rc". The text file should
be
named something different than the project name or any unit name
in your application to avoid any confusion for the compiler. The
text file should contain the following line:
MYJPEG JPEG C:DownLoadMY.JPG
Where:
"MYJPEG" is the name you wish to name the resource
"JPEG" is the user defined resource type.
"C:DownLoadMY.JPG" is the path and filename of the JPEG file.
For our example we will name the file "foo.rc" .
Now run the BRCC32.exe (Borland Resource CommandLine Compiler) program
found
in the Delphi/C++ Builders bin directory giving the full path to the rc
file:
C:DelphiPathBINBRCC32.EXE C:ProjectPathFOO.RC
You should now have a compiled resource file named the same as the ".rc"
file you compiled with the extension of ".res".
The following demonstrates using the embedded JPEG in your application.
{Link the res file}
{$R FOO.RES}
uses Jpeg;
procedure LoadJPEGFromRes(TheJPEG : string;
ThePicture : TPicture);
var
ResHandle : THandle;
MemHandle : THandle;
MemStream : TMemoryStream;
ResPtr : PByte;
ResSize : Longint;
JPEGImage : TJPEGImage;
begin
ResHandle := FindResource(hInstance, PChar(TheJPEG), 'JPEG');
MemHandle := LoadResource(hInstance, ResHandle);
ResPtr := LockResource(MemHandle);
MemStream := TMemoryStream.Create;
JPEGImage := TJPEGImage.Create;
ResSize := SizeOfResource(hInstance, ResHandle);
MemStream.SetSize(ResSize);
MemStream.Write(ResPtr^, ResSize);
FreeResource(MemHandle);
MemStream.Seek(0, 0);
JPEGImage.LoadFromStream(MemStream);
ThePicture.Assign(JPEGImage);
JPEGImage.Free;
MemStream.Free;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
LoadJPEGFromRes('MYJPEG', Image1.Picture);
end;
-----------------------------------------------
1) Use the sndPlaySound() function to directly
play a wave file.
2) Read the wave file into memory, then use the
sndPlaySound() to play the wave file
3) Use sndPlaySound to directly play a wave
file thats embedded in a resource file attached
to your application.
4) Read a wave file thats embedded in a resource
file attached to your application into memory,
then use the sndPlaySound() to play the wave file.
To build the project you will need to:
1) Create a wave file called 'hello.wav'
in the project's directory.
2) Create a text file called 'snddata.rc'
in the project's directory.
3) Add the following line to the file 'snddata.rc':
HELLO WAVE hello.wav
4) At a dos prompt, go to your project directory
and compile the .rc file using the Borland Resource
compiler (brcc32.exe) by typing the path to brcc32.exe
and giving 'snddata.rc' as a parameter.
Example:
binbrcc32 snddata.rc
This will create the file 'snddata.res' that
Delphi will link with your application's .exe
file.
Final Note: Keep on Jamm'n!
*)
unit PlaySnd1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
PlaySndFromFile: TButton;
PlaySndFromMemory: TButton;
PlaySndbyLoadRes: TButton;
PlaySndFromRes: TButton;
procedure PlaySndFromFileClick(Sender: TObject);
procedure PlaySndFromMemoryClick(Sender: TObject);
procedure PlaySndFromResClick(Sender: TObject);
procedure PlaySndbyLoadResClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
{$R snddata.res}
uses MMSystem;
procedure TForm1.PlaySndFromFileClick(Sender: TObject);
begin
sndPlaySound('hello.wav',
SND_FILENAME or SND_SYNC);
end;
procedure TForm1.PlaySndFromMemoryClick(Sender: TObject);
var
f: file;
p: pointer;
fs: integer;
begin
AssignFile(f, 'hello.wav');
Reset(f,1);
fs := FileSize(f);
GetMem(p, fs);
BlockRead(f, p^, fs);
CloseFile(f);
sndPlaySound(p,
SND_MEMORY or SND_SYNC);
FreeMem(p, fs);
end;
procedure TForm1.PlaySndFromResClick(Sender: TObject);
begin
PlaySound('HELLO',
hInstance,
SND_RESOURCE or SND_SYNC);
end;
procedure TForm1.PlaySndbyLoadResClick(Sender: TObject);
var
h: THandle;
p: pointer;
begin
h := FindResource(hInstance,
'HELLO',
'WAVE');
h := LoadResource(hInstance, h);
p := LockResource(h);
sndPlaySound(p,
SND_MEMORY or SND_SYNC);
UnLockResource(h);
FreeResource(h);
end;
end. |
|