首先编辑 rc 文件如下(假定图片文件放在程序目录下的 img 文件夹下):
1 BITMAP img\bmpFile1.bmp
2 BITMAP img\bmpFile2.bmp
或者:
1,BITMAP,img\bmpFile1.bmp
2,BITMAP,img\bmpFile2.bmp
这次我们是给每个资源做了编号, 没有取标识名称; 然后在窗体上添加两个 Button, 代码如下:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure Button2Click(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} var bit: TBitmap; procedure TForm1.FormCreate(Sender: TObject); begin bit := TBitmap.Create; end; //对只有编号的图片资源需要用 LoadFromResourceID 调用 procedure TForm1.Button1Click(Sender: TObject); begin bit := TBitmap.Create; bit.LoadFromResourceID(HInstance, 1); Self.Brush.Bitmap := bit; Repaint; end; procedure TForm1.Button2Click(Sender: TObject); begin bit := TBitmap.Create; bit.LoadFromResourceID(HInstance, 2); Self.Brush.Bitmap := bit; Repaint; end; procedure TForm1.FormDestroy(Sender: TObject); begin bit.Free; end; end.
//效果图: