Virtual Treeview 5 0 0的安裝以及入門 阿新 • • 發佈:2018-12-21 {------------------------------------------------------------------------------- 在Delphi 7下Virtual Treeview的簡單使用 http://blog.csdn.net/akof1314 -------------------------------------------------------------------------------} unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, VirtualTrees, StdCtrls; type TForm1 = class(TForm) vrtlstrngtrVST: TVirtualStringTree; btnAddRoot: TButton; btnAddChildren: TButton; btnClear: TButton; edtNumber: TEdit; grp1: TGroupBox; procedure FormCreate(Sender: TObject); procedure btnAddRootClick(Sender: TObject); procedure btnAddChildrenClick(Sender: TObject); procedure btnClearClick(Sender: TObject); procedure vrtlstrngtrVSTInitNode(Sender: TBaseVirtualTree; ParentNode, Node: PVirtualNode; var InitialStates: TVirtualNodeInitStates); procedure vrtlstrngtrVSTGetText(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType; var CellText: WideString); procedure vrtlstrngtrVSTFreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} {------------------------------------------------------------------------------- Description: 定義一個記錄型別,根據所需新增內容 -------------------------------------------------------------------------------} type PMyRec = ^TMyRec; TMyRec = record //記錄自己的節點結構型別 Caption: String; end; {------------------------------------------------------------------------------- Description: 建立建立函式 -------------------------------------------------------------------------------} procedure TForm1.FormCreate(Sender: TObject); begin vrtlstrngtrVST.NodeDataSize := SizeOf(TMyRec); //設定節點資料空間 vrtlstrngtrVST.RootNodeCount := 20; //設定初始化節點數 end; {------------------------------------------------------------------------------- Description: 新增節點 -------------------------------------------------------------------------------} procedure TForm1.btnAddRootClick(Sender: TObject); var Count: Cardinal; begin Count := StrToInt(edtNumber.Text); vrtlstrngtrVST.RootNodeCount := vrtlstrngtrVST.RootNodeCount + Count; end; {------------------------------------------------------------------------------- Description: 新增子節點 -------------------------------------------------------------------------------} procedure TForm1.btnAddChildrenClick(Sender: TObject); var Count: Cardinal; begin with vrtlstrngtrVST do begin if Assigned(FocusedNode) then begin Count := StrToInt(edtNumber.Text); ChildCount[FocusedNode] := ChildCount[FocusedNode] + Count; Expanded[FocusedNode] := True; //展開節點 InvalidateToBottom(FocusedNode); //重新整理此節點以下的區域 end; end; end; {------------------------------------------------------------------------------- Description: 清除所有節點 -------------------------------------------------------------------------------} procedure TForm1.btnClearClick(Sender: TObject); begin vrtlstrngtrVST.Clear; end; {------------------------------------------------------------------------------- Description: 每個節點非同步觸發一次 -------------------------------------------------------------------------------} procedure TForm1.vrtlstrngtrVSTInitNode(Sender: TBaseVirtualTree; ParentNode, Node: PVirtualNode; var InitialStates: TVirtualNodeInitStates); var Data: PMyRec; begin with Sender do begin Data := GetNodeData(Node); Data.Caption := Format('等級 %d,索引 %d',[GetNodeLevel(Node),Node.Index]); end; end; {------------------------------------------------------------------------------- Description: 樹節點顯示的字串資料 -------------------------------------------------------------------------------} procedure TForm1.vrtlstrngtrVSTGetText(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType; var CellText: WideString); var Data: PMyRec; begin Data := Sender.GetNodeData(Node); if Assigned(Data) then CellText := Data.Caption; end; {------------------------------------------------------------------------------- Description: 釋放節點資料 -------------------------------------------------------------------------------} procedure TForm1.vrtlstrngtrVSTFreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode); var Data: PMyRec; begin Data := Sender.GetNodeData(Node); Finalize(Data^); end; end.