4

试图将背景图像添加到 TGraphicControl。

TCard(TGraphicControl)

Private
  BitMap1:TBitMap;       {Used to store a card image}
Public
  procedure SetBitmap(image: TBitmap);

……

 procedure TCard.SetBitmap(image: TBitmap);
 begin
   bitmap1 := Tbitmap.create();
   bitmap1.Assign(image);
   canvas.draw(0,0,bitmap1);
 end;

在 Form 1 button click 上,我想将图像添加到 tcard 组件

    procedure TForm1.Button1Click(Sender: TObject);
var
  image : Tbitmap;
  jpg: TJpegImage;
begin
  image := TBitmap.create();
  jpg := Tjpegimage.Create();
  jpg.LoadFromFile(dir+'\pics\backcard.jpg');
  image.Assign(jpg);
  card1.setbitmap(image);
  card1.Repaint;
  image.Destroy;
  jpg.Destroy;
 end;

当我运行它时,什么也没有发生。如何将此图像添加到 TCard 组件的背景中?

4

1 回答 1

5

这是为后代设置背景图像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.
于 2013-09-29T06:47:46.390 回答