---
title: "delphi中使用资源文件"
author: "mmdev"
date: "2010-12-01T10:10:00Z"
tags: []
slug: "delphi"
draft: false
lastmod: "2013-01-29T10:23:59Z"
id: 1777228
url: "https://mmdev.iteye.com/blog/1777228"
---

delphi中资源文件为扩展名\*.res的文件,利用delphi自带工具brcc32.exe就可以生成相应的资源文件  
delphi生成资源文件步骤：  
1.编写rc脚本文件

资源文件的源文件是普通的文本文件，扩展名是\*.rc。比如说test.rc.  
编写一个扩展名\*.rc的文件,格式为资源名称--资源类型--文件名称,例如

appdll dll Sqlite3.dll //加入主程序dll  
mybmp bitmap sea.bmp //加入位图  
myicon icon sea.ico //加入图标

2.编译rc文件为res资源文件

注意：要把资源文件test.rc和Sqlite3.dll、sea.bmp、sea.ico放在同一个文件夹中  
利用delphi自带工具brcc32.exe即可将rc文件编译成res资源文件,例如  
在dos窗口输入brcc32 myresource.rc。（目录一定要正确）编译成功后,就会生成一个test.res的资源文件。  
也可以把命令写到批处理文件,这样执行更方便

3.在delphi单元中加入资源文件  
在单元文件{$R \*.DFM}后,加入{$Rtest.res},编译程序后,资源文件就包含在exe文件中

4.加载资源  
程序要加载资源,需要调用WindowsAPI函数,如LoadBitmap,LoadIcon等  
//加载位图  
procedure TForm1.btnShowBitmapClick(Sender: TObject);  
var  
ht:hBitmap;  
begin  
ht:=LoadBitmap(hinstance,'mybmp');  
Image1.Picture.Bitmap.Handle:=ht;  
end;

//加载图标  
procedure TForm1.btnShowIconClick(Sender: TObject);  
var  
ht:hIcon;  
begin  
ht:=LoadIcon(hinstance,'myicon');  
Image1.Picture.Icon.Handle:=ht;  
end;