0

我需要向 TVirtualStringTree 插入一些新节点。我使用带有 UserData 参数的 InsertNode 方法。但是树不能显示任何数据。如何获取 GetText/FreeNode 事件的用户数据?

  TMyData = record
    Name: string;
    size: Integer;
  end;
  PMyData = ^TMyData;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.FormCreate(Sender: TObject);
begin
  VirtualStringTree1.NodeDataSize := SizeOf(TMyData);
  Randomize;
end;

procedure TForm2.Button1Click(Sender: TObject);
var
  Data: PMyData;
  Node: PVirtualNode;
begin
  New(Data);
  Data.Name := DateTimeToStr(now);
  Data.size := Random(100);
  Node := VirtualStringTree1.InsertNode(nil, amInsertAfter, Data);
end;

procedure TForm2.VirtualStringTree1FreeNode(Sender: TBaseVirtualTree; Node:
    PVirtualNode);
var
  Data: PMyData;
begin
  Data := Sender.GetNodeData(Node);
  Data.Name := '';
end;

procedure TForm2.VirtualStringTree1GetText(Sender: TBaseVirtualTree; Node:
    PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType; var CellText:
    WideString);
var
  Data: PMyData;
begin
  Data := Sender.GetNodeData(Node);
  case Column of
    0: CellText := Data.Name;
    1: CellText := IntToStr(Data.size);
  end;
end;
4

1 回答 1

6

当您在树中插入用户数据时,您的指针将存储在节点的内部记录中。GetNodeData不返回您传入的指针,它是指向存储指针的内存的指针。因此,您需要取消引用它:

Data := PMyData(Sender.GetNodeData(Node)^);
于 2011-05-18T10:30:05.843 回答