当前位置:首页 > 网络编程 > 软件语言 > DELPHI > 如何在Delphi中使用资源文件

如何在Delphi中使用资源文件

点击次数:40 次 发布日期:2008-11-09 08:38:28 作者:源代码网
源代码网推荐
广告载入中
作者: 潘汉杰
源代码网推荐
源代码网推荐 资源也是数据,它相当于我们熟悉的只读数据。在应用程序的可执行代码中,它是单独存储的,当其被调用时才载入程序,在程序执行完后又退出。Delphi中的资源有很多类型,适用于不同的地方,大致有以下几类:
源代码网推荐
源代码网推荐 ●图标资源:是一种小型位图,用户常常用不同的图标代替不同的应用程序。
源代码网推荐
源代码网推荐 ●光标资源:也是小型的位图,不过它适用的颜色不多。Delphi已经给光标指定了一个光标图案集,就是我们操作中常用到的Cursor属性,同时用户也可自定义光标图案。
源代码网推荐
源代码网推荐 ●位图资源:Delphi只是将位图资源存入资源文件中,在使用时从资源文件中调出。
源代码网推荐
源代码网推荐 ●字符串资源:将字串符文件存储于资源文件中。
源代码网推荐
源代码网推荐 本文通过实例程序来说明资源在Delphi中的用法,所有示例程序均在Delphi 4中调试通过。
源代码网推荐
源代码网推荐 1、生成资源文件
源代码网推荐 在Delphi中提供了一个图形编辑器(Image Editor),通过该编辑器可以编辑生产Bitmap、Icon和Cursor三种资源文件,也可以直接绘制ICO、CUR和BMP文件。该编辑器不能进行文字处理,具体文字处理在后面介绍。编辑图形类资源文件时,首先启动Image Editor,选择:File->New->Resource File ;在弹出的窗口中用鼠键右键单击“Contents”,再弹出的菜单中单击:New ;选择需要编辑的资源文件的类型(Bitmap、Icon或Cursor)。


源代码网推荐
源代码网推荐 2、装载与使用资源文件
源代码网推荐 资源文件编辑生成后(文件名后缀为.res),要使用这些资源文件,首先要通过添加代在表单文件的implementation关键字中加入:
源代码网推荐
源代码网推荐 {$R *.DFM}
源代码网推荐 {$R 资源文件名.RES}
源代码网推荐
源代码网推荐 定义了资源文件并且在单元文件中包括了资源文件名,需要调用Windows的API函数调用资源文件里的内容,如:LoadIcon,LoadString,LoadBitmap,LoadResource等。
源代码网推荐
源代码网推荐 例如:下面的语句装入了一个名为mybmp.bmp的文件:
源代码网推荐
源代码网推荐 Bmp.Handle := LoadBitmap(Hinstance , "mybmp.bmp");
源代码网推荐
源代码网推荐 下面示例程序说明了图标、光标和位图资源的使用方法,在资源文件TEST.RES中定义了两个光标(cur1和cur2)、两个位图(bmp1和bmp2)以及两个图标(in1和in2),在程序中对这些资源都进行了调用。并利用定时器使位图和图标的显示有类似动画的感觉。当鼠标移动到Ladel1上时,光标会变成你定义的第一个光标形状;当单击Button1后,再把光标移到Ladel1上时,光标会变成你定义的第二个光标形状。
源代码网推荐
源代码网推荐 unit testtes;
源代码网推荐 interface
源代码网推荐 uses
源代码网推荐 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
源代码网推荐 StdCtrls, ExtCtrls;
源代码网推荐 type
源代码网推荐 TForm1 = class(TForm)
软件开发网 www.mscto.com

源代码网推荐 Label1: TLabel;
源代码网推荐 Button1: TButton;
源代码网推荐 Image1: TImage;
源代码网推荐 Timer1: TTimer;
源代码网推荐 procedure FormCreate(Sender: TObject);
源代码网推荐 procedure Button1Click(Sender: TObject);
源代码网推荐 procedure Timer1Timer(Sender: TObject);
源代码网推荐 private
源代码网推荐 bmp12 : Integer;
源代码网推荐 { Private declarations }
源代码网推荐 public
源代码网推荐 { Public declarations }
源代码网推荐 end;
源代码网推荐 var
源代码网推荐 Form1: TForm1;
源代码网推荐 implementation
源代码网推荐 {$R *.DFM}
源代码网推荐 {$R TEST.RES}
源代码网推荐 const
源代码网推荐 crMycur1 = 1;
源代码网推荐 crMycur2 = 2;
源代码网推荐 procedure TForm1.FormCreate(Sender: TObject);
源代码网推荐 var
源代码网推荐 bmp : TBitmap;
源代码网推荐 ico : TIcon;
源代码网推荐 begin
源代码网推荐 Screen.Cursors[crMycur1] := LoadCursor(Hinstance,"CUR1");
源代码网推荐 Screen.Cursors[crMycur2] := LoadCursor(Hinstance,"CUR2");
源代码网推荐 Label1.Cursor := crMycur1;
源代码网推荐 bmp := TBitmap.Create ;
源代码网推荐 bmp.Handle := LoadBitmap(Hinstance,"BMP1");
源代码网推荐 Image1.Width := bmp.Width 10;
源代码网推荐 Image1.Height := bmp.Height 10;
源代码网推荐 Image1.Canvas.Draw(4,8,bmp);
源代码网推荐 bmp12 := 1;
源代码网推荐 ico := TIcon.Create ;
源代码网推荐 ico.Handle := LoadIcon(Hinstance,"IN1");
源代码网推荐 Icon := ico;
源代码网推荐 end;
源代码网推荐 procedure TForm1.Button1Click(Sender: TObject);

源代码网推荐 begin
源代码网推荐 Label1.Cursor := crMycur2;
源代码网推荐 end;
源代码网推荐 procedure TForm1.Timer1Timer(Sender: TObject);
源代码网推荐 var
源代码网推荐 bmp:TBitmap;
源代码网推荐 ico : TIcon;
源代码网推荐 begin
源代码网推荐 bmp := TBitmap.Create ;
源代码网推荐 ico := TIcon.Create ;
源代码网推荐 if bmp12=1 then bmp12 := 2 else bmp12 :=1;
源代码网推荐 bmp.Handle := LoadBitmap(Hinstance,PChar("BMP" IntToStr(bmp12)));
源代码网推荐 ico.Handle := LoadIcon(Hinstance,PChar("IN" IntToStr(bmp12)));
源代码网推荐 Image1.Width := bmp.Width 10;
源代码网推荐 Image1.Height := bmp.Height 10;
源代码网推荐 Image1.Canvas.Draw(4,6,bmp);
源代码网推荐 Icon := ico;
源代码网推荐 end;
源代码网推荐 end.
源代码网推荐
源代码网推荐 3、字符串资源的定义与使用
源代码网推荐 字符串的存储在应用程序中是独立的,应用程序只有在使用资源时载入,使用完之后清除,从而节省内存,同时字符串也可以用于翻译,一些汉化软件都利用了字符串。编辑的字符串放在一个文本文件中,可以使用Delphi中的:File->New->Text,编辑字符串文件,字符串文件的格式如下:
源代码网推荐
源代码网推荐 stringtable
源代码网推荐 begin
源代码网推荐 1,"book"
源代码网推荐 2,"apple"
源代码网推荐 3,"desk"
源代码网推荐 4,"pen"
源代码网推荐 5,"computer"
源代码网推荐 end
源代码网推荐
源代码网推荐 编辑完字符串文件后,选择Save as,注意要将文件类型改为资源编译文件(.RC),这还不是资源文件,它还必须经过编译才能成为资源文件(.RES)。编译命令为Dos提示符下的BRCC32,其路径为:D:Program FilesBorlandDelphi4Binrcc32.exe ;例如上面的字符串资源编译文件名为:StrRes.rc,在DOS提示符下输入:brcc32 mydirStrRes.rc ;则编译后会生成一个名为:StrRes.res的资源文件,使用该文件即可访问字符串资源。具体使用见下例:


源代码网推荐 unit teststr;
源代码网推荐 interface
源代码网推荐 uses
源代码网推荐 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
源代码网推荐 type
源代码网推荐 TForm1 = class(TForm)
源代码网推荐 Button1: TButton;
源代码网推荐 Label1: TLabel;
源代码网推荐 procedure Button1Click(Sender: TObject);
源代码网推荐 procedure FormCreate(Sender: TObject);
源代码网推荐 private
源代码网推荐 count : integer;
源代码网推荐 { Private declarations }
源代码网推荐 public
源代码网推荐 { Public declarations }
源代码网推荐 end;
源代码网推荐 var
源代码网推荐 Form1: TForm1;
源代码网推荐 implementation
源代码网推荐 {$R *.DFM}
源代码网推荐 {$R StrRes.RES}
源代码网推荐 const
源代码网推荐 wordcount = 5;
源代码网推荐 procedure TForm1.Button1Click(Sender: TObject);
源代码网推荐 var
源代码网推荐 strword : string;
源代码网推荐 begin
源代码网推荐 if count>wordcount then count := 1;
源代码网推荐 strword := LoadStr(count);
源代码网推荐 label1.Caption := strword;
源代码网推荐 count := count 1;
源代码网推荐 end;
源代码网推荐 procedure TForm1.FormCreate(Sender: TObject);
源代码网推荐 begin
源代码网推荐 label1.Caption := LoadStr(1);
源代码网推荐 count := 2;
源代码网推荐 end;
源代码网推荐 end.
源代码网推荐
源代码网推荐 程序中常量wordcount用来记录字符串资源文件中字符串的数量,变量count用来记录显示的字符串编号。程序运行后单击Button1按钮,则将循环显示字符串资源文件中的每一个字符串。


源代码网推荐

源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华