读书人

tobject类型转换的有关问题

发布时间: 2012-02-12 17:16:34 作者: rapoo

tobject类型转换的问题

这是一个二维数值 c
c : array of array of TColor;

把c放到 TObjectList 里
BmpobList.add(TObject(c));


我现在要取出来该怎么取

[解决办法]
期待高手
[解决办法]
进来学习
[解决办法]
c ^ : array of array of TColor;
还要设定数组下标
把c搞成指针数组应该容易些吧
[解决办法]

type
TMyArray = array of array of TColor;

var
FB: TMyArray; // to hold the addr

procedure TForm3.Button1Click(Sender: TObject);
{
var
FB: TMyArray; // 不能为私有变量,否则会自动把这个搞没了,或者可以定义成指针,然后New一个出来
}
begin
SetLength(FB, 5);
SetLength(FB[0], 5);
FB[0][0] := clRed;
Flist.Add(TObject(FB));
end;

procedure TForm3.Button2Click(Sender: TObject);
var
FAnotherB: TMyArray;
begin
if Flist.Count > 0 then
begin
FAnotherB := TMyArray(Flist[0]);
Color := FAnotherB[0][0];
end;
end;

constructor TForm3.Create(AOwner: TComponent);
begin
inherited;

Flist := TObjectList.Create;
Flist.OwnsObjects := False;
end;

destructor TForm3.Destroy;
begin
Flist.Free;

inherited;
end;

[解决办法]
发布指针版本,

Delphi(Pascal) code
type    TMyArray   =   array   of   array   of   TColor;    PMyArray   =   ^TMyArray;procedure   TForm3.Button1Click(Sender:   TObject);var    FB:   PMyArray;begin  New(FB);  SetLength(FB^,   5);  SetLength(FB^[0],   5);  FB^[0][0]   :=   clRed;  Flist.Add(TObject(FB));end;procedure   TForm3.Button2Click(Sender:   TObject);var    FAnotherB:   PMyArray;begin    if   Flist.Count   >   0   then    begin        FAnotherB   :=   PMyArray(Flist[0]);        Self.Color   :=   FAnotherB^[0][0];    end;end;constructor   TForm3.Create(AOwner:   TComponent);begin    inherited;    Flist   :=   TObjectList.Create;    Flist.OwnsObjects   :=   False;end;destructor   TForm3.Destroy;var  FObj: Pointer;begin  for FObj in Flist do    Dispose(PMyArray(FObj));    Flist.Free;    inherited;end;
[解决办法]
Delphi(Pascal) code
unit Unit2;interfaceuses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  Dialogs, StdCtrls,  Contnrs;type  PColorGrid = ^TColorGrid;  TColorGrid = array of array of TColor;const  conColsOfGrid = 5;  conRowsOfGrid = 4;  conColorGrid :array [0..conRowsOfGrid -1] of                array [0..conColsOfGrid -1] of                TColor = (  (clBlack ,  clMaroon,  clGreen ,  clOlive,  clNavy),                            (clPurple,  clTeal  ,  clGray  ,  clSilver,  clRed),                            (clLime,  clYellow,  clBlue,  clFuchsia,  clAqua ),                            (clLtGray,  clDkGray,  clWhite,clSkyBlue,clMedGray)                        );var  GColorGridList : TObjectList = Nil;type  TfrmTestColorGrid = class(TForm)    btnAddColor: TButton;    Button1: TButton;    procedure FormDestroy(Sender: TObject);    procedure btnAddColorClick(Sender: TObject);    procedure Button1Click(Sender: TObject);  private    { Private declarations }  public    { Public declarations }  end;var  frmTestColorGrid: TfrmTestColorGrid;implementation{$R *.dfm}procedure TfrmTestColorGrid.btnAddColorClick(Sender: TObject);var  tmpColorGrid : PColorGrid;  I,J: Integer;begin    if Not Assigned(GColorGridList) then      begin        GColorGridList := TObjectList.Create;        GColorGridList.OwnsObjects := False;//特别需要注意这一个设置,                                            //当前所添加进去的并不是TObject                                            //如果不对此设置,默认为True,                                            //进行删除的时候会以TObject类去调用Free方法。        if Not Assigned(GColorGridList) then          Exit;//内存不足或其它      end;    if GColorGridList.Count = 0 then      begin        New(tmpColorGrid);        SetLength(tmpColorGrid^,conRowsOfGrid);        for I := 0 to conRowsOfGrid - 1 do          begin            SetLength(tmpColorGrid^[I],conColsOfGrid);          end;        if GColorGridList.Add(TObject(tmpColorGrid)) = -1 then          Exit;//添加失败      end;  if GColorGridList.Count = 0 then    Exit;  try    tmpColorGrid := PColorGrid(GColorGridList.Items[0]);    for I := 0 to Length(tmpColorGrid^) - 1 do      begin        for J := 0 to Length(tmpColorGrid^[I]) - 1 do          begin            tmpColorGrid^[I][J] := conColorGrid[I][J]; //初始化          end;      end;  except    //未知错误  end;end;procedure TfrmTestColorGrid.Button1Click(Sender: TObject);var  tmpColorGrid : PColorGrid;  I,J : Integer;begin  if Not Assigned(GColorGridList) then    Exit;  if GColorGridList.Count = 0 then    Exit;  try    tmpColorGrid := PColorGrid(GColorGridList.Items[0]);    for I := 0 to Length(tmpColorGrid^) - 1 do      begin        for J := 0 to Length(tmpColorGrid^[I]) - 1 do          begin            Color :=  tmpColorGrid^[I][J]; //取出颜色值,并设置为Form的前景色            ShowMessage('我看到效果了');          end;      end;  except    //未知错误  end;end;procedure TfrmTestColorGrid.FormDestroy(Sender: TObject);var  I : Integer;  tmpColorGrid : PColorGrid;  J: Integer;begin  if Assigned(GColorGridList) then    begin //清理内存      for I := GColorGridList.Count - 1 to 0 do        begin          tmpColorGrid := PColorGrid(GColorGridList.Items[I]);          GColorGridList.Items[I] := Nil;          GColorGridList.Delete(I);          for J := 0 to Length(tmpColorGrid^) - 1 do            begin              SetLength(tmpColorGrid^[J],0);//逐行清理            end;          SetLength(tmpColorGrid^,0);//清理整个Grid          Dispose(tmpColorGrid);     //删除Grid        end;      GColorGridList.Free;           //删除全局列表    end;end;end. 

读书人网 >.NET

热点推荐