菜鸟刚学类,不知怎么用,我写的简单代码怎么不好使?(60分)

  • 主题发起人 主题发起人 wanglongb
  • 开始时间 开始时间
W

wanglongb

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;

type

TSprite = class
private

FBits: TBitmap;

public
constructor Create
virtual;

end;

TForm1 = class(TForm)
Image1: TImage;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
wl:tsprite;
implementation

{$R *.dfm}

constructor TSprite.Create;
begin
inherited Create;

FBits := TBitmap.Create;
FBits.Width := 100;
FBits.Height := 100;
fbits.LoadFromFile('bullet.bmp');

end;

procedure TForm1.Button1Click(Sender: TObject);
begin
//如何把新创建的类显示到image控件上
wl:=tsprite.Create;
end;

end.
 
你的新写的类得从tcontrol或twincontrol类继承
才能显示出来。否则为非可视组件 .
 
TSprite = class
private

FBits: TBitmap;

public
constructor Create
virtual;
destructor Destroy;override;
property Bits: TBitmap read FBits;
end;

destructor TSprite.Destroy;
begin
FBits.Free;
inherited;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
//如何把新创建的类显示到image控件上
wl:=tsprite.Create;
image1.Picture.Bitmap.Assign(wl.Bits);
end;
 
当然不行了,你的那各类只是包含了一个位图文件的私有属性而已。由于没有定义访问函数所以外部的类
根本不可能访问到你的类中的私有属性,建议你好好理解类的封装的概念。下面是我改的
类,加了一个可以访问私有属性的函数GetBitmap

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;

type

TSprite = class
private

FBits: TBitmap;

public
constructor Create
virtual;
Function GetBitmap:TBitMap;//<-------------在这里加上访问内部属性的函数
end;

TForm1 = class(TForm)
Image1: TImage;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
wl:tsprite;
implementation

{$R *.dfm}

constructor TSprite.Create;
begin
inherited Create;

FBits := TBitmap.Create;
FBits.Width := 100;
FBits.Height := 100;
FBits.LoadFromFile('bullet.bmp');
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
//如何把新创建的类显示到image控件上
wl:=tsprite.Create;
iMAGE1.Picture.Bitmap:=wl.GetBitmap;//获得图片
end;

function TSprite.GetBitmap: TBitMap;
begin
Result:=FBits;
end;

end.
 
多人接受答案了。
 
后退
顶部