我在编写自定义控件时需要建立一个“对象属性”并且要同时指定该对象属性的部分属性的默认值。代码如下:
unit ImageBox;interfaceuses
SysUtils, Classes, Controls,Forms,ExtCtrls,Graphics,StdCtrls,Dialogs;
type TBorderPen = class(TPersistent)
private
FColor: TColor;
FWidth: Integer;
procedure SetColor(Value: TColor);
procedure SetWidth(Value: Integer);
public published
property Color:TColor read FColor write SetColor default clBlue;
property Width:Integer read FWidth write SetWidth default 3;
end;type
TImageBox = class(TScrollBox)
private
{ Private declarations }
FBorder:TShape;
FBorderPen: TBorderPen;
Fa1:boolean;
protected
{ Protected declarations }
procedure SetSelectedStyle(Value:TBorderPen);
public
{ Public declarations }
constructor Create(AOwner: TComponent);override;
destructor Destroy(); override; published
{ Published declarations }
property SelectedStyle: TBorderPen read FBorderPen write SetSelectedStyle;
property abc: boolean read fa1 write fa1 default true;
end;procedure Register;implementationprocedure Register;
begin
RegisterComponents('我的组件', [TImageBox]);
end;procedure TBorderPen.SetColor(Value:TColor);
begin
if (Color <> Value) then
begin
FColor := Value;
end;
end;procedure TBorderPen.SetWidth(Value:Integer);
begin
if (Width <> Value) then
begin
FWidth := Value;
end;
end;
constructor TImageBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FBorderPen := TBorderPen.Create();
FBorder := TShape.Create(self);
try
//FBorder.Parent := self;
//FBorder.Show();
except
end;
end;destructor TImageBox.Destroy;
begin
FBorder.Destroy();
FBorderPen.Destroy();
inherited Destroy;
end;procedure TImageBox.SetSelectedStyle(Value: TBorderPen);
begin
if (Assigned(Value)) then
begin
FBorderPen := Value;
FBorder.Pen.Color := Value.Color;
FBorder.Pen.Width := Value.Width;
end;
end;end.
可是,无法修改属性值,并且默认属性值也无法设置。
我的环境是Delphi 7.0
unit ImageBox;interfaceuses
SysUtils, Classes, Controls,Forms,ExtCtrls,Graphics,StdCtrls,Dialogs;
type TBorderPen = class(TPersistent)
private
FColor: TColor;
FWidth: Integer;
procedure SetColor(Value: TColor);
procedure SetWidth(Value: Integer);
public published
property Color:TColor read FColor write SetColor default clBlue;
property Width:Integer read FWidth write SetWidth default 3;
end;type
TImageBox = class(TScrollBox)
private
{ Private declarations }
FBorder:TShape;
FBorderPen: TBorderPen;
Fa1:boolean;
protected
{ Protected declarations }
procedure SetSelectedStyle(Value:TBorderPen);
public
{ Public declarations }
constructor Create(AOwner: TComponent);override;
destructor Destroy(); override; published
{ Published declarations }
property SelectedStyle: TBorderPen read FBorderPen write SetSelectedStyle;
property abc: boolean read fa1 write fa1 default true;
end;procedure Register;implementationprocedure Register;
begin
RegisterComponents('我的组件', [TImageBox]);
end;procedure TBorderPen.SetColor(Value:TColor);
begin
if (Color <> Value) then
begin
FColor := Value;
end;
end;procedure TBorderPen.SetWidth(Value:Integer);
begin
if (Width <> Value) then
begin
FWidth := Value;
end;
end;
constructor TImageBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FBorderPen := TBorderPen.Create();
FBorder := TShape.Create(self);
try
//FBorder.Parent := self;
//FBorder.Show();
except
end;
end;destructor TImageBox.Destroy;
begin
FBorder.Destroy();
FBorderPen.Destroy();
inherited Destroy;
end;procedure TImageBox.SetSelectedStyle(Value: TBorderPen);
begin
if (Assigned(Value)) then
begin
FBorderPen := Value;
FBorder.Pen.Color := Value.Color;
FBorder.Pen.Width := Value.Width;
end;
end;end.
可是,无法修改属性值,并且默认属性值也无法设置。
我的环境是Delphi 7.0
//这里的“default”是留给属性编辑器(Object Inspector)的~~
//代码中并不会执行~~constructor TImageBox.Create(AOwner: TComponent);
....
FColor := clBlue;
....
//所有还需要在构造方法中初始化一偏~~
1。default 标识不会影响到程序的任何行为,
他只是一个存储标志(storage specifiers)。
2。在dfm文件中保存控件的各个属性参数时会比较值是否与default指定的值相等,如果相等则不保存到文件中,如果相等则不保存,这个自己可以做一个试验就知道了,主要是为了减少文件大小。
3。初始化property时必须在create中初始化。
4。没有标明default的property 默认为 nodefault.
private
FColor: TColor;
FWidth: Integer;
procedure SetColor(Value: TColor);
procedure SetWidth(Value: Integer);
public
constructor Create;
published
property Color:TColor read FColor write SetColor default clBlue;
//上面这个default是没有用的,下面同
property Width:Integer read FWidth write SetWidth default 3;
end;
{ TBorderPen }constructor TBorderPen.Create;
begin
inherited Create;
//初始值
FColor := clBlue;
FWidth := 3;
end;procedure TBorderPen.SetColor(Value: TColor);
begin
FColor := value;
end;procedure TBorderPen.SetWidth(Value: Integer);
begin
FWidth := Value;
end;
procedure TImageBox.SetSelectedStyle(Value: TBorderPen);
begin
if (Assigned(Value)) then
begin
FBorderPen := Value;
FBorder.Pen.Color := Value.Color;
FBorder.Pen.Width := Value.Width;
end;
end;
如下:
procedure TImageBox.SetSelectedStyle(Value: TBorderPen);
begin
if (Assigned(Value)) then
begin
FBorderPen.Assign(Value);
end;
end;
procedure TBorderPen.Assign(Source: TPersistent);
begin
if (Source is TBorderPen) then
begin
FColor := TBorderPen(Source).Color;
FWidth := TBorderPen(Source).Width;
inherited Assign(Source);
end;
end;这样,不但没起到应有的作用,反而在运行时会提示:TBorderPen不能转换到TBorderPen。
难道真的自己设计属性编辑器吗。delphi7属性编辑器设计同delphi5获6的方法一样嘛。
begin
if (Assigned(Value)) then
begin
fborderpen.free; //原来的对象要释放。
FBorderPen := Value; //value在函数外创建后不能销毁。
FBorder.Pen.Color := Value.Color;
FBorder.Pen.Width := Value.Width;
end;
end;
TIntegerProperty.SetValue(Value: string);
begin
SetIntValue(Value); //将string转换成Integer并赋给该属性
end;
这个属性编辑器在DesignEditors.pas中已经有了。对于你的TBorderPen类型的属性,Delphi完全不知道该如何转换(没有该类型的属性编辑器),所以你得自己写一个
PS:你在运行时为该属性指定一个值,应该是可以成功的。