打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Using resource files with Delphi

Create the resource file script.

What follows is a step by step instruction for creating resource files.

Resource files can contain text, html documents, sounds, images etc.

Open a simple text editor, e.g. Notepad and enter the files you want to include in your application.

Every line consist of a resource name, a resource type and the resource.

In the example below support is the resource name, HTML is the resource type and "support.html" is the resource.

support HTML "support.html"content HTML "content.html"help    HTML "help.html"bg      JPG  "bg.jpg"back    BMP  "back.bmp"forward BMP  "forward.bmp"home    BMP  "home.bmp"next    GIF  "next.gif"

Save this file with the .rc extension, e.g. myresource.rc.

Compile the resource file.

Now compile myresource.rc with the resource compiler BRCC32.exe which you will find in your Delphi5\bin\ directory.

BRCC32.exe is a command line utility that has the .rc extension associated with it.

Double-click myresource.rc in Windows Explorer and it will be compiled to myresource.res.

Include the resource file in your project.

Enter a statement like {$R .\resources\myresource.res} in your main unit file.

This will look then something like:

implementation{$R *.DFM}{$R .\resources\myresource.res}

Assuming that you keep the resource for a project in a subdirectory called resources , (like I always do for clarity).

Loading the resource file

Next you need a procedure to load a named resource into a memorystream

procedure LoadResourceFile(aFile:string; ms:TMemoryStream);varHResInfo: HRSRC;HGlobal: THandle;Buffer, GoodType : pchar;I: integer;Ext:string;beginext:=uppercase(extractfileext(aFile));ext:=copy(ext,2,length(ext));if ext='HTM' then ext:='HTML';Goodtype:=pchar(ext);aFile:=changefileext(afile,'');HResInfo := FindResource(HInstance, pchar(aFile), GoodType);HGlobal := LoadResource(HInstance, HResInfo);if HGlobal = 0 thenraise EResNotFound.Create('Can''t load resource: '+aFile);Buffer := LockResource(HGlobal);ms.clear;ms.WriteBuffer(Buffer[0], SizeOfResource(HInstance, HResInfo));ms.Seek(0,0);UnlockResource(HGlobal);FreeResource(HGlobal);end;

The TmemoryStream must be created before you call the routine. If you use a lot of resources in your program you can create the variable ms in the FormCreate event and destroy it in the FormDestroy event.

aFile is the name of a resource file to be loaded, without path information.

Personally I always use the method of giving the resource name the same name as the resource file without the extension. This allows for a generic routine like above.

Using the resource file

Finally we want to use the resource file that is now in the TMemoryStream variable ms.

In Delphi many components have a LoadFromStream method that you can use to load the resource.

Suppose we want to load the resource file support.html into a Tstrings.

procedure LoadStringResource;beginList:=TStringList.create;LoadResourceFile('support.html',ms);Memo1.Lines.LoadFromStream(ms);end;

In the above Memo1 is a TMemo.

Alternative method

I received an alternative method from Adrian Meyer which is much shorter:

procedure LoadStringResource2;vartmpStream: TResourceStream;begintmpStream := TResourceStream.Create( HInstance, 'support', 'HTML' );trymemo1.Lines.LoadFromStream( tmpStream );finallytmpStream.Free;end;end;

Conclusion

Compiling resources into your application is easy. I used it in my SbookBuilder program to include all the html docs and images that together make up the user interface. I could have included these files as external files, but this way I am sure that they are always there when the program needs them.

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Web安全:文件上传漏洞
绝地程序编辑器 此软件支持多文档打开, 种 语言语法高亮度显示:Delphi,SQL,Basic,C++, Perl, Editor /阅读器 182万源代码下载
C# 资源文件(二)
Qt中rcc工具简介
delphi 获得网页返回 HTML 文字
如何将文件拷贝到剪贴板
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服