首先編輯 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.
效果圖: