首先編輯 rc 文件如下(假定圖片文件放在程序目錄下的 img 文件夾下):
bmp1 BITMAP imgbmpFile1.bmp
bmp2 BITMAP imgbmpFile2.bmp
或者:
bmp1,BITMAP,imgbmpFile1.bmp
bmp2,BITMAP,imgbmpFile2.bmp
然後在窗體上添加一個 Image、兩個 Button, 代碼如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
Image1: TImage;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Image1.AutoSize := True;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Image1.Picture.Bitmap.Handle := LoadBitmap(HInstance, 'bmp1');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
image1.Picture.Bitmap.LoadFromResourceName(HInstance, 'bmp2');
end;
end.
效果圖: