delphi控件-VirtualStringTreeView使用-转
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;