gpt4 book ai didi

delphi - 如何发布整数列表?

转载 作者:行者123 更新时间:2023-12-03 15:49:11 25 4
gpt4 key购买 nike

我想制作一个包含整数列表作为其序列化属性之一的组件。我知道我无法声明 TList<integer>作为已发布属性,因为它不是从 TPersistent 派生的。我读过,如果您重写 DefineProperties,则可以定义“假”已发布属性,但我不太确定它是如何工作的,特别是在创建一个列表而不是单个值的假属性时。

有人能指出我正确的方向吗?

最佳答案

这是一个使用 DefineBinaryProperty 的最小示例(用 D2007 编写,无泛型):

type
TTestComponent = class(TComponent)
private
FList: TList;

function GetValueCount: Integer;
function GetValues(Index: Integer): Integer;
procedure ReadValueList(Stream: TStream);
procedure SetValues(Index: Integer; Value: Integer);
procedure WriteValueList(Stream: TStream);
protected
procedure DefineProperties(Filer: TFiler); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;

function AddValue(Value: Integer): Integer;
procedure ClearValues;
procedure DeleteValue(Index: Integer);

property ValueCount: Integer read GetValueCount;
property Values[Index: Integer]: Integer read GetValues write SetValues;
end;

{ TTestComponent }

function TTestComponent.GetValueCount: Integer;
begin
Result := FList.Count;
end;

function TTestComponent.GetValues(Index: Integer): Integer;
begin
Result := Integer(FList[Index]);
end;

procedure TTestComponent.ReadValueList(Stream: TStream);
var
Count, I, Value: Integer;
begin
ClearValues;
Stream.Read(Count, SizeOf(Count));
for I := 0 to Count - 1 do
begin
Stream.Read(Value, SizeOf(Value));
AddValue(Value);
end;
end;

procedure TTestComponent.SetValues(Index: Integer; Value: Integer);
begin
FList[Index] := Pointer(Value);
end;

procedure TTestComponent.WriteValueList(Stream: TStream);
var
Count, I, Value: Integer;
begin
Count := ValueCount;
Stream.Write(Count, SizeOf(Count));
for I := 0 to Count - 1 do
begin
Value := Values[I];
Stream.Write(Value, SizeOf(Value));
end;
end;

procedure TTestComponent.DefineProperties(Filer: TFiler);
begin
inherited DefineProperties(Filer);
Filer.DefineBinaryProperty('ValueList', ReadValueList, WriteValueList, ValueCount > 0);
end;

constructor TTestComponent.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FList := TList.Create;
// add some values for testing
AddValue(0);
AddValue(1);
AddValue(2);
end;

destructor TTestComponent.Destroy;
begin
FList.Free;
inherited Destroy;
end;

function TTestComponent.AddValue(Value: Integer): Integer;
begin
Result := FList.Add(Pointer(Value));
end;

procedure TTestComponent.ClearValues;
begin
FList.Clear;
end;

procedure TTestComponent.DeleteValue(Index: Integer);
begin
FList.Delete(Index);
end;

.dfm 看起来像这样:

  object TestComponent1: TTestComponent
Left = 96
Top = 56
ValueList = {03000000000000000100000002000000}
end

关于delphi - 如何发布整数列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2879686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com