在 delphi 中创建一个 TStringGrid 类,其中与单元格关联的对象数组被指定为更具体的类型

Ton*_*lff 1 delphi inheritance tstringgrid

在 Delphi 中,如何创建从 TStringGrid 类派生的类,以便与网格单元关联的 TObject 数组具有更具体的类型,例如用于指定单元格颜色的 TColor?

Rem*_*eau 6

type
  TStringColorGrid = class(TStringGrid)
  private
    function GetColor(ACol, ARow: Integer): TColor;
    procedure SetColor(ACol, ARow: Integer; AValue: TColor);
  public
    property Colors[ACol, ARow: Integer]: TColor read GetColor write SetColor;
  end;

function TStringColorGrid.GetColor(ACol, ARow: Integer): TColor;
begin
  Result := TColor(inherited Objects[ACol, ARow]);
end;

procedure TStringColorGrid.SetColor(ACol, ARow: Integer; AValue: TColor);
begin
  inherited Objects[ACol, ARow] := TObject(AValue);
end;
Run Code Online (Sandbox Code Playgroud)

  • @fpiette它对于*最大*指针大小的类型可以很好地工作,它们可以更小。例如,在 32 位系统上,Int64 将无法工作。此外,必须特别注意引用计数类型,例如字符串和接口。但这只是如何实现自定义属性以使用与 TObject 不同的类型的一个示例。 (5认同)