TVirtualStringTreeView使用摘要篇(转)

本文介绍了一种在VirtualStringTree中自定义结点结构的方法,包括初始化、遍历根节点及所有节点的过程。此外,还展示了如何通过不同方式增加根节点和子节点,以及实现必要的回调函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://peirenlei.iteye.com/blog/487207

自定义结点结构:

PTagCustomListItem = ^TTagCustomListItem ;  
TTagCustomListItem = record  
  Name:string;  
  Id:Integer;  
end; 

*初始化:

VST.NodeDataSize := SizeOf(TTagCustomListItem);  
 VST.RootNodeCount := 2;  

*遍历根节点:

var  
  PVN:PVirtualNode;  
  _pNodeData:PTagCustomListItem;  
begin  
   PVN := VST.GetFirstChild(nil);  
   while Assigned(PVN) do  
   begin  
     _pNodeData := VST.GetNodeData(PVN);  
     if Assigned(_pNodeData) then  
       ShowMessage(_pNodeData.Name);  
     PVN := VST.GetNextSibling(PVN);  
   end;  
end;  

*遍历所有节点:

VST.IterateSubtree(nil,VSTIterateProc,nil,[]);  
  
  
procedure TForm1.VSTIterateProc(Sender: TBaseVirtualTree;  
  Node: PVirtualNode; Data: Pointer; var Abort: Boolean);  
var  
  _pNodeData:PTagCustomListItem;  
begin  
  _pNodeData := Sender.GetNodeData(Node);  
  if Assigned(_pNodeData) then  
    ShowMessage(_pNodeData.Name);  
end;  

*增加根节点:

_count := VST.RootNodeCount ;  
VST.RootNodeCount := _count + 1;  

**增加子节点:

var  
  _count: Cardinal;  
begin  
   // add as child  
   _count := VST.ChildCount[VST.FocusedNode];  
   VST.ChildCount[VST.FocusedNode] := _count + 1 ;  
   VST.Expanded[VST.FocusedNode] := True;  
   VST.InvalidateToBottom(VST.FocusedNode);  
end;  

*另一种添加节点方法:

procedure TForm1.FormCreate(Sender: TObject);  
var  
Data:PTagCustomListItem;  
RootNode:PVirtualNode;  
begin  
//清除所有Node  
VirtualStringTree1.Clear;  
//指定VitrualStringTree有幾個Node  
//VirtualStringTree1.RootNodeCount := 2;  
//將所定義的結構大小指定給VitualStringTree  
VirtualStringTree1.NodeDataSize := SizeOf(TTagCustomListItem);  
  
//添加节点  
RootNode:= VirtualStringTree1.AddChild(nil);  
Data:=VirtualStringTree1.GetNodeData(RootNode);  
Data^.Name:='根结点';  
  
RootNode:= VirtualStringTree1.AddChild(nil);  
Data:=VirtualStringTree1.GetNodeData(RootNode);  
Data^.Name:='根结点aaa';   
  
end;  

*必须的回调函数:

procedure TForm1.VSTGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;  
  Column: TColumnIndex; TextType: TVSTTextType; var CellText: WideString);  
var  
  _pNodeData:PTagCustomListItem;  
begin  
  _pNodeData := Sender.GetNodeData(Node);  
  
  if Assigned(_pNodeData) then  
    CellText := _pNodeData.Name;  
end;  
  
procedure TForm1.VSTInitNode(Sender: TBaseVirtualTree; ParentNode,  
  Node: PVirtualNode; var InitialStates: TVirtualNodeInitStates);  
var  
  _pNodeData:PTagCustomListItem;  
begin  
  _pNodeData := Sender.GetNodeData(Node);  
    
  if Assigned(_pNodeData) then  
    _pNodeData.Name := Format('Node Level:%d, Index:%d ',[Sender.GetNodeLevel(Node),Node.Index]);  
end; 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值