请问怎么做包含字符串的Res文件?怎么包含Jpg文件?请各位大侠帮忙!!(30分)

  • 主题发起人 主题发起人 ggwen
  • 开始时间 开始时间
G

ggwen

Unregistered / Unconfirmed
GUEST, unregistred user!
请问怎么做包含字符串的Res文件?怎么包含Jpg文件?请各位大侠帮忙!!
 
::放置任意的文件到Delphi的EXE文件里面::
通常在Delphi的应用程序中,我们会调用到很多的资源,例如图片,动画(AVI),声音,甚至于别的执行文件。当然,把这些资源分布到不同的目录不失为一个好办法,但是有没有可能把这些资源编译成标准的windows资源从而链接到一个执行文件里面呢?

我们可以自己做一个RC文件,例如 sample.rc ,RC文件其实就是一个资源文件的描述文本,通过“记事本”程序创建就行了。然后可以输入一些我们要定义的资源,例如:

MEN BITMAP c:/bitmap/men.bitmap
ARJ EXEFILE c:/arj.exe
MOV AVI c:/mov.avi
然后用BRCC32把这个RC文件编译成sample.res(真正的资源文件)。

在Delphi的工程文件中使用 $R 编译指令让Delphi包括资源到EXE文件里面。

{$R sample.res}
这样我们就可以在这个单一的执行文件中调用资源了。举例如下:

EXEFILE:

procedure ExtractRes(ResType, ResName, ResNewName : String);
var
Res : TResourceStream;
begin
Res := TResourceStream.Create(Hinstance, Resname, Pchar(ResType)); Res.SavetoFile(ResNewName);
Res.Free;
end;
AVI:

procedure LoadAVI;
begin
{Avi1是一个TAnimate类}
Avi1.ResName:='AVI';
Avi1.Active:=True;
end;

 
STRINGTABLE
BEGIN
11,"开始"
22,"退出"
END


然后用Brcc32编译,最后再程序中
{$R FileName.res}
使用:
showmessage(LoadStr(11));
showmessage(LoadStr(22));
 
resurce builder
 
How To Include JPEG's In Your Executable (Delphi 3)

From: Marko Peric
I'm a Delphi beginner who's exploring this wonderful programming environment,
and I just thought I'd like to share some really neat stuff I worked out.
Delphi3 comes with the jpeg unit, and that's great, but did you ever wonder
how to include jpeg's in your executable and then use them in your application?
Well, follow this simple 5 step plan and you can't go wrong!


STEP ONE:
Create a resource script file (*.RC) with a simple text editor like Notepad and add the following line:
--------------------------------------------------------------------------------

1 RCDATA "MyPic.jpg"

--------------------------------------------------------------------------------
The first entry is simply the index of the resource. The second entry specifies that we are dealing
with a user-defined resource. The third and final entry is the name of the jpeg file.

STEP TWO:
Use Borland's Resource Compiler, BRCC32.EXE, to compile it into a .RES file. At the MS-DOS command line:
--------------------------------------------------------------------------------

BRCC32 MyPic.RC

--------------------------------------------------------------------------------
This will create a resource file called MyPic.RES.
STEP THREE:
Add a compiler directive to the source code of your program. It should immediately
follow the form directive, as shown here:
--------------------------------------------------------------------------------

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

--------------------------------------------------------------------------------

STEP FOUR:
Add the following code to your project (I've created a procedure for it):
--------------------------------------------------------------------------------

procedure LoadJPEGfromEXE;

var
MyJPG : TJPEGImage; // JPEG object
ResStream : TResourceStream; // Resource Stream object

begin
try
MyJPG := TJPEGImage.Create;
ResStream := TResourceStream.CreateFromID(HInstance, 1, RT_RCDATA);
MyJPG.LoadFromStream(ResStream); // What!? Yes, that easy!
Canvas.Draw(12,12,MyJPG); // draw it to see if it really worked!
finally
MyJPG.Free;
ResStream.Free;
end;
end; // procedure

--------------------------------------------------------------------------------
See the second parameter of the CreateFromID procedure of the TResourceStream component?
It's simply the resource index. You can include more than one jpeg in your executable just
by adding a line for each jpeg (with a different index) in the resource script (.RC) file.
STEP FIVE:
Call the procedure, run the program, and voila! Now go eat some nachos.
 
多人接受答案了。
 
后退
顶部