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

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

回答

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;

  • @fpiette it will work fine for types *up to* the size of a pointer, they can be smaller. On a 32bit system, Int64 won't work, for instance. Also, extra care has to be taken for reference-counted types, like strings and interfaces. But this is just one example of how to implement a custom property to work with a different type than TObject.

以上是在delphi中创建一个TStringGrid类,其中与单元格关联的对象数组被指定为更具体的类型的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>