delphi资源文件使用_Delphi如何使用资源文件

delphi资源文件使用

From bitmaps to icons to cursors to string tables, every Windows program uses resources. Resources are those elements of a program that support the program but are not executable code. In this article, we will walk through some examples of the use of bitmaps, icons, and cursors from resources.

从位图到图标,再到游标再到字符串表,每个Windows程序都使用资源。 资源是程序中支持程序但不是可执行代码的那些元素。 在本文中,我们将介绍一些使用资源中的位图,图标和光标的示例。

资源位置 ( Location of Resources )

Placing resources in the .exe file has two main advantages:

将资源放在.exe文件中有两个主要优点

  • The resources can be accessed more quickly because it takes less time to locate a resource in the executable file than it does to load it from a disk file.

    可以更快地访问资源,因为在可执行文件中定位资源所花的时间比从磁盘文件中加载资源所花费的时间少。
  • The program file and resources can be contained in a single unit (the .exe file) without the need for a lot of supporting files.

    程序文件和资源可以包含在单个单元(.exe文件)中,而无需大量支持文件。

图像编辑器 ( The Image Editor )

First of all, we need to create a resource file. The default extension for resource files is .RES. Resource files can be created with Delphi's Image Editor.

首先,我们需要创建一个资源文件。 资源文件的默认扩展名是。 RES 。 可以使用Delphi的图像编辑器创建资源文件。

You can name the resource file anything you want, as long as it has the extension ".RES" and the filename without the extension is not the same as any unit or project filename. This is important, because, by default, each Delphi project that compiles into an application has a resource file with the same name as the project file, but with the extension ".RES". It's best to save the file to the same directory as your project file.

您可以将资源文件命名为任意名称,只要它的扩展名为“ .RES”,且不带扩展名的文件名与任何单位或项目的文件名都不相同。 这很重要,因为默认情况下,每个编译成应用程序的Delphi项目都具有与项目文件同名的资源文件,但扩展名为“ .RES”。 最好将文件保存到项目文件所在的目录中。

在应用程序中包含资源 ( Including Resources in Applications )

In order to access our own resource file, we have to tell Delphi to link our resource file in with our application. This is accomplished by adding a compiler directive to the source code. This directive needs to immediately follow the form directive, like the following:

为了访问我们自己的资源文件,我们必须告诉Delphi将我们的资源文件链接到我们的应用程序。 这是通过在源代码中添加编译器指令来实现的。 该指令需要立即遵循form指令,如下所示:


{$R *.DFM} {$R DPABOUT.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.

不要意外删除{$ R * .DFM}部分,因为这是告诉Delphi链接到表单可视部分的代码行。 当为速度按钮,图像组件或按钮组件选择位图时,Delphi会包含您选择的位图文件作为表单资源的一部分。 Delphi将您的用户界面元素隔离到.DFM文件中。

To actually use the resource, you must make a few Windows API calls. Bitmaps, cursors, and icons stored in RES files can be retrieved by using the API functions LoadBitmap, LoadCursor, and LoadIcon respectively.

要实际使用资源,您必须进行几次Windows API调用。 可以分别使用API​​函数LoadBitmapLoadCursorLoadIcon来检索RES文件中存储的位图,光标和图标。

资源中的图片 ( Pictures in Resources )

The first example shows how to load a bitmap stored as a resource and display it in a TImage component.

第一个示例显示如何加载存储为资源的位图并将其显示在TImage组件中。


procedure TfrMain.btnCanvasPic(Sender: TObject);var bBitmap : TBitmap;begin
bBitmap := TBitmap.Create;try
bBitmap.Handle := LoadBitmap(hInstance, 'ATHENA');
Image1.Width := bBitmap.Width;
Image1.Height := bBitmap.Height;
Image1.Canvas.Draw(0,0,bBitmap);finally
bBitmap.Free;end;end;

Note: If the bitmap that is to be loaded is not in the resource file, the program will still run, it just won't display the bitmap. This situation can be avoided by testing to see if the bBitmap.Handle is zero after a call to LoadBitmap() and taking the appropriate steps. The try/finally part in the previous code doesn't solve this problem, it is just here to make sure that the bBitmap is destroyed and its associated memory is freed.

注意:如果要加载的位图不在资源文件中,则程序仍将运行,只是不会显示位图。 通过在调用LoadBitmap()之后进行测试以查看bBitmap.Handle是否为零并采取适当的步骤,可以避免这种情况。 先前代码中的try / finally部分无法解决此问题,只是在此处确保bBitmap被销毁并释放其关联的内存。

Another way we can use to display a bitmap from a resource is as follows:

我们可以用来显示资源中位图的另一种方法如下:


procedure TfrMain.btnLoadPicClick(Sender: TObject);begin
Image1.Picture.Bitmap.
LoadFromResourceName(hInstance,'EARTH');end;

资源中的游标 ( Cursors in Resources )

Screen.Cursors[] is an array of cursors supplied by Delphi. By using resource files, we can add custom cursors to the Cursors property. Unless we wish to replace any of the defaults, the best strategy is to use cursor numbers starting from 1.

Screen.Cursors []是Delphi提供的游标数组 。 通过使用资源文件,我们可以将自定义光标添加到Cursors属性。 除非我们希望替换任何默认值,否则最佳策略是使用从1开始的光标编号。


procedure TfrMain.btnUseCursorClick(Sender: TObject);const NewCursor = 1;begin
Screen.Cursors[NewCursor] :=
LoadCursor(hInstance,'CURHAND');
Image1.Cursor := NewCursor;end;

资源中的图标 ( Icons in Resources )

If we look at Delphi's Project-Options-Application settings, we can find that Delphi supplies the default icon for a project. This icon represents the application in the Windows Explorer and when the application is minimized. We can easily change this by clicking the 'Load Icon' button.

如果我们查看Delphi的Project-Options-Application设置,我们会发现Delphi为项目提供了默认图标。 此图标代表Windows资源管理器中的应用程序以及应用程序最小化的时间。 我们可以通过单击“加载图标”按钮轻松更改此设置。

If we want, for example, to animate the program's icon when the program is minimized, then the following code will do the job.

例如,如果我们要在最小化程序时对程序的图标进行动画处理,则以下代码将完成此工作。

For the animation, we need a TTimer component on a form. The code loads two icons from resource file into an array of TIcon objects; this array needs to be declared in the public part of the main form. We'll also need NrIco, that is an Integer type variable, declared in the public part. The NrIco is used to keep track of the next icon to show.

对于动画,我们需要窗体上的TTimer组件。 该代码将两个图标从资源文件加载到TIcon对象数组中。 该数组需要在主表单的公共部分声明。 我们还需要在公共部分声明的NrIco ,它是一个Integer类型的变量NrIco用于跟踪要显示的下一个图标。


public
nrIco : Integer;
MinIcon : array[0..1] of TIcon;
...procedure TfrMain.FormCreate(Sender: TObject);begin
MinIcon[0]:=TIcon.Create;
MinIcon[1]:=TIcon.Create;
MinIcon[0].Handle:=LoadIcon(hInstance,'ICOOK');
MinIcon[1].Handle:=LoadIcon(hInstance,'ICOFOLD');
NrIco:=0;
Timer1.Interval:=200;end;
...procedure TfrMain.Timer1Timer(Sender: TObject);beginif IsIconic(Application.Handle) then begin
NrIco:=(NrIco+1) mod 2;
Application.Icon:=MinIcon[NrIco];end;end;
...procedure TfrMain.FormDestroy(Sender: TObject);begin
MinIcon[0].Free;
MinIcon[1].Free;end;

In the Timer1.OnTimer event handler, IsMinimized function is used to see whether we need to animate our main icon or not. A better way of accomplishing this would be to capture the maximize/minimize buttons and than act.

Timer1.OnTimer事件处理程序中,使用IsMinimized函数查看我们是否需要对主图标进行动画处理。 更好的方法是捕获最大化/最小化按钮,然后执行操作。

最后的话 ( Final Words )

We can place anything (well, not everything) in resource files. This article has shown you how to use resources to use/display bitmap, cursor or an icon in your Delphi application.

我们可以在资源文件中放置任何内容(不是全部)。 本文介绍了如何在Delphi应用程序中使用资源来使用/显示位图,光标或图标。

Note: When we save a Delphi project to the disk, Delphi automatically creates one .RES file that has the same name as the project (if nothing else, the main icon of the project is inside). Although we can alter this resource file, this is not advisable.

注意:当我们将一个Delphi项目保存到磁盘时,Delphi会自动创建一个与该项目同名的.RES文件(如果没有其他说明,则该项目的主图标位于其中)。 尽管我们可以更改此资源文件,但这是不可取的。

翻译自: https://www.thoughtco.com/how-delphi-uses-resource-files-4077232

delphi资源文件使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
FlatStyle .........\vcl_flatstyle .........\.............\vcl_flatstyle7 .........\.............\..............\Packages .........\.............\..............\........\FlatStyle_D5.cfg,382,2001-07-01 .........\.............\..............\........\FlatStyle_D5.dcu,4531,2001-07-01 .........\.............\..............\........\FlatStyle_D5.dof,1240,2001-07-01 .........\.............\..............\........\FlatStyle_D5.dpk,2301,2001-07-01 .........\.............\..............\........\FlatStyle_D5.res,1536,2001-07-01 .........\.............\..............\........\FlatStyle_D6.cfg,390,2002-08-29 .........\.............\..............\........\FlatStyle_D6.dcu,5009,2002-08-29 .........\.............\..............\........\FlatStyle_D6.dof,1421,2002-08-29 .........\.............\..............\........\FlatStyle_D6.dpk,2250,2001-07-01 .........\.............\..............\........\FlatStyle_D6.dsk,2462,2001-07-01 .........\.............\..............\........\FlatStyle_D6.res,1536,2001-07-01 .........\.............\..............\........\FlatStyle_D7.cfg,438,2004-10-26 .........\.............\..............\........\FlatStyle_D7.dcu,6183,2004-10-26 .........\.............\..............\........\FlatStyle_D7.dof,2023,2004-10-26 .........\.............\..............\........\FlatStyle_D7.dpk,2250,2001-07-01 .........\.............\..............\........\FlatStyle_D7.dsk,2568,2002-09-09 .........\.............\..............\........\FlatStyle_D7.res,1536,2001-07-01 .........\.............\..............\Source .........\.............\..............\......\DFS.INC,8673,2001-06-29 .........\.............\..............\......\FlatArrow.res,372,2001-06-29 .........\.............\..............\......\FlatGraphics.dcu,1710,2002-09-09 .........\.............\..............\......\FlatGraphics.pas,2168,2001-06-29 .........\.............\..............\......\FlatSound.res,71508,2001-06-29 .........\.............\..............\......\FlatUtilitys.dcu,11988,2002-09-09 .........\.............\..............\......\FlatUtilitys.pas,11687,2001-06-29 .........\.............\..............\......\HSLUtils.dcu,3209,2002-09-09 .........\.............\..............\......\HSLUtils.pas,4174,2001-06-29 .........\.............\..............\......\TFlatAnimationUnit.dcu,15066,2002-09-09 .........\.............\..............\......\TFlatAnimationUnit.pas,12001,2001-06-29 .........\.............\..............\......\TFlatAnimWndUnit.dcu,9884,2002-09-09 .........\.............\..............\......\TFlatAnimWndUnit.pas,6336,2001-06-29 .........\.............\..............\......\TFlatButtonUnit.dcu,28259,2002-09-09 .........\.............\..............\......\TFlatButtonUnit.pas,24710,2001-06-29 .........\.............\..............\......\TFlatCheckBoxUnit.dcu,20650,2002-09-09 .........\.............\..............\......\TFlatCheckBoxUnit.pas,15453,2001-06-29 .........\.............\..............\......\TFlatCheckListBoxUnit.dcu,27040,2002-09-09 .........\.............\..............\......\TFlatCheckListBoxUnit.pas,25105,2001-06-29 .........\.............\..............\......\TFlatColorComboBoxUnit.dcu,27520,2002-09-09 .........\.............\..............\......\TFlatColorComboBoxUnit.pas,19775,2001-06-29 .........\.............\..............\......\TFlatComboBoxUnit.dcu,21667,2002-09-09 .........\.............\..............\......\TFlatComboBoxUnit.pas,13785,2001-06-29 .........\.............\..............\......\TFlatDesignRegister.dcu,6690,2001-07-01 .........\.............\..............\......\TFlatDesignRegister.pas,5137,2001-07-01 .........\.............\..............\......\TFlatEditUnit.dcu,17400,2002-09-09 .........\.............\..............\......\TFlatEditUnit.pas,10760,2001-06-29 .........\.............\..............\......\TFlatGaugeUnit.dcu,10185,2002-09-09 .........\.............\..............\......\TFlatGaugeUnit.pas,7556,2001-07-01 .........\.............\..............\......\TFlatGroupBoxUnit.dcu,14451,2002-09-09 .........\.............\..............\......\TFlatGroupBoxUnit.pas,10964,2001-06-29 .........\.............\..............\......\TFlatHintUnit.dcu,15818,2002-09-09 .........\.............\..............\......\TFlatHintUnit.pas,12646,2001-06-29 .........\.............\..............\......\TFlatListBoxUnit.dcu,25421,2002-09-09 .........\.............\..............\......\TFlatListBoxUnit.pas,22127,2001-06-29 .........\.............\..............\......\TFlatMaskEditUnit.dcu,16638,2002-09-09 .........\.............\..............\......\TFlatMaskEditUnit.pas,9655,2001-06-29 .........\.............\..............\......\TFlatMemoUnit.dcu,14856,2002-09-09 .........\.............\..............\......\TFlatMemoUnit.pas,8473,2001-06-29 .........\.............\..............\......\TFlatPanelUnit.dcu,10308,2002-09-09 .........\.............\..............\......\TFlatPanelUnit.pas,4805,2001-06-29 .........\.............\..............\......\TFlatProgressBarUnit.dcu,12657,2002-09-09 .........\.............\..............\......\TFlatProgressBarUnit.pas,11030,2001-06-29 .........\.............\..............\......\TFlatRadioButtonUnit.dcu,20512,2002-09-09 .........\.............\..............\......\TFlatRadioButtonUnit.pas,15276,2001-06-29 .........\.............\..............\......\TFlatRegister.dcr,11732,2001-06-29 .........\.............\..............\......\TFlatRegister.dcu,3463,2002-09-09 .........\.............\..............\......\TFlatRegister.pas,1324,2001-06-29 .........\.............\..............\......\TFlatScrollbarUnit.dcu,33164,2002-09-09 .........\.............\..............\......\TFlatScrollbarUnit.pas,31957,2001-06-29 .........\.............\..............\......\TFlatScrollbarUnit.res,9996,2001-06-29 .........\.............\..............\......\TFlatSoundUnit.dcu,3106,2002-09-09 .........\.............\..............\......\TFlatSoundUnit.pas,1820,2001-06-29 .........\.............\..............\......\TFlatSpeedButtonUnit.dcu,24834,2002-09-09 .........\.............\..............\......\TFlatSpeedButtonUnit.pas,22668,2001-06-29 .........\.............\..............\......\TFlatSpinButtonUnit.dcu,14577,2002-09-09 .........\.............\..............\......\TFlatSpinButtonUnit.pas,9502,2001-06-29 .........\.............\..............\......\TFlatSpinEditUnit.dcu,27146,2002-09-09 .........\.............\..............\......\TFlatSpinEditUnit.pas,16383,2001-06-29 .........\.............\..............\......\TFlatSplitterUnit.dcu,15292,2002-09-09 .........\.............\..............\......\TFlatSplitterUnit.pas,10697,2001-06-29 .........\.............\..............\......\TFlatTabControlUnit.dcu,21602,2002-09-09 .........\.............\..............\......\TFlatTabControlUnit.pas,19674,2001-06-29 .........\.............\..............\......\TFlatTitlebarUnit.dcu,10441,2002-09-09 .........\.............\..............\......\TFlatTitlebarUnit.pas,6386,2001-06-29

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值