这是为后代设置背景图像TGraphicControl
的示例,以及使用它的示例(它TBitmap
在表单的 中OnCreate
使用a ,TJpegImage
在中使用 aButton1Click
来演示两者)。它只需要一个新的空白 VCL 表单应用程序,其中一个TButton
位于其上即可启动。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Jpeg, StdCtrls;
type
TCard = class(TGraphicControl)
private
FBackGround: TBitmap;
procedure SetBackground(Value: TBitmap); overload;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Paint; override;
published
property BackGround: TBitmap read FBackGround write SetBackground;
end;
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
FCard: TCard;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TCard }
constructor TCard.Create(AOwner: TComponent);
begin
inherited;
FBackGround := TBitmap.Create;
end;
destructor TCard.Destroy;
begin
FBackground.Free;
inherited;
end;
procedure TCard.Paint;
begin
inherited;
Self.Canvas.StretchDraw(Self.ClientRect, FBackGround);
end;
procedure TCard.SetBackground(Value: TBitmap);
begin
FBackGround.Assign(Value);
//Self.SetBounds(Left, Top, FBackGround.Width, FBackGround.Height);
Invalidate;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Image: TJPEGImage;
Bmp: TBitmap;
begin
Image := TJPEGImage.Create;
Bmp := TBitmap.Create;
try
Image.LoadFromFile(PathToSomeJPEGFile);
Bmp.Assign(Image);
FCard.BackGround := Bmp;
finally
Bmp.Free;
Image.Free;
end;
end;
procedure TForm4.FormCreate(Sender: TObject);
var
Bmp: TBitmap;
begin
FCard := TCard.Create(Self);
FCard.Parent := Self;
Bmp := TBitmap.Create;
try
// Load a standard image from the backgrounds folder (D2007).
Bmp.LoadFromFile('C:\Program Files (x86)\Common Files\CodeGear Shared\Images\BackGrnd\GREENBAR.BMP');
FCard.BackGround := Bmp;
finally
Bmp.Free;
end;
end;
end.