自己开发了一个复合控件,继承自TPanel,Pannel上面有一个TImage.控件暴露出Picture属性,设计时图片可见,运行时却看不到图片,为什么,请进来

  • 主题发起人 主题发起人 jsres
  • 开始时间 开始时间
J

jsres

Unregistered / Unconfirmed
GUEST, unregistred user!
自己开发了一个复合控件,继承自TPanel,Pannel上面有一个TImage.控件暴露出Picture属性,设计时图片可见,运行时却看不到图片,为什么,请进来看看! ( 积分: 100 )<br />自己开发了一个复合控件,继承自TPanel,Pannel上面有一个TImage.暴露出Picture属性,设计时图片可见,运行时却看不到图片,为什么?

设计时我通过Object Inspector给Picuture选定了一个图片,Panel上的Timage能够显示
但是运行时图片就没了,应该怎么做,标准写法怎么写.请指教

unit Unit1;

interface

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

type
TMyPanel = class(TPanel)
private
ImageIcon: TImage;
FPicture: TPicture;
procedure SetPicture(const Value: TPicture);
protected
{ Protected declarations }
public
constructor Create(AOwner: TComponent); override;
published
property Picture: TPicture read FPicture write SetPicture;
end;

procedure Register;

implementation

{$R *.DFM}

procedure Register;
begin
RegisterComponents('Samples', [TMyPanel]);
end;

{ TMyPanel }

constructor TMyPanel.Create(AOwner: TComponent);
begin
inherited;
ImageIcon:=TImage.Create(self);
ImageIcon.Parent:=Self;
ImageIcon.Width:=16;
ImageIcon.Height:16;
end;
procedure TMyPanel.SetPicture(const Value: TPicture);
begin
FPicture.Assign(Value);
ImageIcon.Picture.Assign(FPicture);

end;

end.
 
自己开发了一个复合控件,继承自TPanel,Pannel上面有一个TImage.暴露出Picture属性,设计时图片可见,运行时却看不到图片,为什么?

设计时我通过Object Inspector给Picuture选定了一个图片,Panel上的Timage能够显示
但是运行时图片就没了,应该怎么做,标准写法怎么写.请指教

unit Unit1;

interface

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

type
TMyPanel = class(TPanel)
private
ImageIcon: TImage;
FPicture: TPicture;
procedure SetPicture(const Value: TPicture);
protected
{ Protected declarations }
public
constructor Create(AOwner: TComponent); override;
published
property Picture: TPicture read FPicture write SetPicture;
end;

procedure Register;

implementation

{$R *.DFM}

procedure Register;
begin
RegisterComponents('Samples', [TMyPanel]);
end;

{ TMyPanel }

constructor TMyPanel.Create(AOwner: TComponent);
begin
inherited;
ImageIcon:=TImage.Create(self);
ImageIcon.Parent:=Self;
ImageIcon.Width:=16;
ImageIcon.Height:16;
end;
procedure TMyPanel.SetPicture(const Value: TPicture);
begin
FPicture.Assign(Value);
ImageIcon.Picture.Assign(FPicture);

end;

end.
 
呵呵,你的 TMyPanel.Create(AOwner: TComponent);是一个新的对象,把原来在设计时候的对象实例给“冲”掉了……

建议你改写Create函数:

constructor TMyPanel.Create(AOwner: TComponent,const Value: TPicture);
其中const Value: TPicture就是你想指定的Picture
 
楼上的,我认为你的说法不对,Panel绝对没有被冲掉!
虽然Timage没有显示图片,但是FPicture是真实存在的.
后台在Pannel构件Resize事件里给Timage重新赋值,运行就能显示
procedure TMyPanel.BoxResize(Sender: TObject);
begin
ImageIcon.Picture.Assign(FPicture);
end;
只是我觉得这样用好像不是标准写法.
 
jsres:
这样

unit MyPanel;

interface

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

type
TMyPanel = class(TPanel)
private
ImageIcon: TImage;
FPicture: TPicture;
procedure SetPicture(Value: TPicture);
procedure PictureChanged(Sender: TObject);
protected
{ Protected declarations }
procedure CreateWnd; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Picture: TPicture read FPicture write SetPicture;
end;

procedure Register;

implementation


procedure Register;
begin
RegisterComponents('Samples', [TMyPanel]);
end;

{ TMyPanel }

constructor TMyPanel.Create(AOwner: TComponent);
begin
inherited;
ImageIcon := TImage.Create(self);
FPicture := TPicture.Create;
FPicture.OnChange := PictureChanged;
end;

procedure TMyPanel.CreateWnd;
begin
inherited;
ImageIcon.Parent:=Self;
ImageIcon.Width:=16;
ImageIcon.Height:=16;
end;

destructor TMyPanel.Destroy;
begin
FPicture.Free;
inherited;
end;

procedure TMyPanel.PictureChanged(Sender: TObject);
begin
ImageIcon.Picture.Assign(FPicture);
end;

procedure TMyPanel.SetPicture(Value: TPicture);
begin
FPicture.Assign(Value);
ImageIcon.Picture.Assign(FPicture);
end;

end.
 
constructor TMyPanel.Create(AOwner: TComponent);
begin
inherited;
ImageIcon:=TImage.Create(self);
ImageIcon.Parent:=Self;
ImageIcon.Width:=16;
ImageIcon.Height:=16;
FPicture:=TPicture.Create;//这是要创建的,没有创建根本不实际存在,也没有冲掉之说

end;

procedure TMyPanel.SetPicture(const Value: TPicture);
begin
FPicture.Assign(Value);
ImageIcon.Picture.Assign(FPicture);

// 如果不创建FPicture,则不管它,干脆直接
// ImageIcon.Picture.Assign(Value);

end;

//注意:退出时要释放创建的对象,特别是含图片,很占内存的

控件使用
MyPanel1.Picture:=Image1.Picture;
 
后退
顶部