如何使用子属性编写属性? [英] How do I code a property with sub-properties?
本文介绍了如何使用子属性编写属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
编辑:我的意思是当我在对象检查器中查看Font有一点加号,我可以点击设置字体名称times new roman,字体大小10等。Sorrry如果我使用错误的术语,那就是我通过子属性。 >
解决方案
所有您需要做的是创建一个新的已发布属性,指向已发布属性的类型。
检查此代码
键入
TCustomType = class(TPersistent)//此类型有3个已发布的财产
private
FColor:TColor;
FHeight:整数;
宽度:整数;
public
procedure Assign(Source:TPersistent);覆盖
发布
属性颜色:TColor读取FColor写入FColor;
属性高度:整数读取FHeight写入FHeight;
属性宽度:整数读宽度写FWidth;
结束
TMyControl = class(TWinControl)
private
FMyProp:TCustomType;
FColor1:TColor;
FColor2:TColor;
程序SetMyProp(AValue:TCustomType);
public
构造函数Create(AOwner:TComponent);覆盖
析构函数覆盖
发布
属性MyProp:TCustomType读取FMyProp写SetMyProp; //现在这个属性有3个子属性(这个术语不存在于delphi中)
属性Color1:TColor读取FColor1写入FColor1;
属性Color2:TColor读取FColor2写入FColor2;
结束
procedure TCustomType.Assign(Source:TPersistent);
var
Src:TCustomType;
begin
如果Source是TCustomType然后
begin
Src:= TCustomType(Source);
FColor:= Src.Color;
身高:= Src.Height;
FWidth:= Src.Width;
end else
继承;
结束
构造函数TMyControl.Create(AOwner:TComponent);
开始
继承;
FMyProp:= TCustomType.Create;
结束
析构函数TMyControl.Destroy;
begin
FMyProp.Free;
继承;
结束
程序TMyControl.SetMyProp(AValue:TCustomType);
begin
FMyProp.Assign(AValue);
结束
For instance, like Font. Can anyone give a very simple example? Maybe just a property with two sub-properties
Edit: I mean that when I look at Font in the object inspector it has a little plus sign which I can click to set font name "times new roman", font size "10", etc. Sorrry if I use the wrong terms, that is what I menat by "sub-properties".
解决方案
All you have to do is create a new published property that points to an type that has published properties.
check this code
type
TCustomType = class(TPersistent) //this type has 3 published properties
private
FColor : TColor;
FHeight: Integer;
FWidth : Integer;
public
procedure Assign(Source: TPersistent); override;
published
property Color: TColor read FColor write FColor;
property Height: Integer read FHeight write FHeight;
property Width : Integer read FWidth write FWidth;
end;
TMyControl = class(TWinControl)
private
FMyProp : TCustomType;
FColor1 : TColor;
FColor2 : TColor;
procedure SetMyProp(AValue: TCustomType);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property MyProp : TCustomType read FMyProp write SetMyProp; //now this property has 3 "sub-properties" (this term does not exist in delphi)
property Color1 : TColor read FColor1 write FColor1;
property Color2 : TColor read FColor2 write FColor2;
end;
procedure TCustomType.Assign(Source: TPersistent);
var
Src: TCustomType;
begin
if Source is TCustomType then
begin
Src := TCustomType(Source);
FColor := Src.Color;
Height := Src.Height;
FWidth := Src.Width;
end else
inherited;
end;
constructor TMyControl.Create(AOwner: TComponent);
begin
inherited;
FMyProp := TCustomType.Create;
end;
destructor TMyControl.Destroy;
begin
FMyProp.Free;
inherited;
end;
procedure TMyControl.SetMyProp(AValue: TCustomType);
begin
FMyProp.Assign(AValue);
end;
这篇关于如何使用子属性编写属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文