Delphi,VirtualStringTree - 类(对象)而不是记录 [英] Delphi, VirtualStringTree - classes (objects) instead of records
本文介绍了Delphi,VirtualStringTree - 类(对象)而不是记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要为VirtualStringTree节点使用类而不是记录。
I need to use a class instead of record for VirtualStringTree node.
我应该声明它是标准的(但在这种情况下 - p>
Should I declare it standard (but in this case - tricky) way like that:
PNode = ^TNode;
TNode = record
obj: TMyObject;
end;
//..
var
fNd: PNode;
begin
fNd:= vstTree.getNodeData(vstTree.AddChild(nil));
fNd.obj:= TMyObject.Create;
//..
或者直接使用 TMyObject
?如果是这样 - 怎么?
如何分配(构造)对象并释放它?
or should I use directly TMyObject
? If so - how?!
How about assigning (constructing) the object and freeing it?
提前感谢
m。
Thanks in advance m.
推荐答案
-
设置用于保存对象的数据
Setup datasize for holding object
vstTree.NodeDataSize := SizeOf(TMyObject);
获取datasize持有者并绑定到您的对象
Get the datasize holder and bind to your object
vstTree.getNodeData(passed in interested node)^ := your object
或
vstTree.getNodeData(vstTree.AddChild(nil))^ := TMyObject.Create;
或
使用vstTree.InsertNode方法
or
use vstTree.InsertNode method
释放绑定对象连接OnFreeNode事件
To free the binding object hookup the OnFreeNode event
vstTree.OnFreeNode := FreeNodeMethod;
与
procedure TFoo.FreeNodeMethod(Sender: TBaseVirtualTree; Node: PVirtualNode);
var
P: ^TMyObject;
begin
P := Sender.getNodeData(Node);
if P <> nil then
begin
P^.Free;
P^ := nil; //for your safety or you can omit this line
end;
end;
这篇关于Delphi,VirtualStringTree - 类(对象)而不是记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文