Virtual Tree View - How To Install - Delphi 3rd Party Open Source Component

01
of 03

Virtual TreeView - About

Virtual Tree View - Sample In Action
Virtual Tree View - Sample In Action

Any tree view like component's purpose is to display a hierarchical list of items. A most common one you use and see every day is the one used in Windows Explorer - to display folders (and more) on your file system.

Delphi comes with the TTreeView control - located on the "Win32" section of the tool palette. Defined in the ComCtrls unit, the TTreeView does a decent task of allowing you to present any parent-child relation of any type of objects.

Each node in the TTreeView consists of a label and an optional bitmapped image - and the TTreeNode object describes an individual node in a TTreeView control.

While powerful enough for most of the tasks if your application is based on displaying hierarchical data, like folders and files, XML structure, any anything alike, you would soon realize that you need more power from a tree view like component.

This is where one gem of the 3rd party components world comes to the rescue: the Virtual TreeView component.

Virtual TreeView

The Virtual TreeView, initially being developed by Mike Lischke and now being maintained as an open source project on Google Code is a must-use control if you are up to working with whatever you could call "nodes".

With more than 13 years spend in development, the Virtual TreeView is one of the most polished, flexible and advanced open source components for the Delphi market.

Never mind the Delphi version you are using from Delphi 7 to the latest version (XE3 at the moment) you would be able to use and leverage the power of the TVirtualStringTree and TVirtualDrawTree (the actual names of the controls) in your applications.

Here are just a few "why to use" features of the Virtual TreeView control:

  • very small memory foot print.
  • super fast.
  • virtual - meaning it does not know about the data it manages - only the size. Everything is done via events.
  • supports multi-column views
  • easy customization of a node display with bitmaps and font styles.
  • drag'n drop and clipboard support
  • each node in the tree can have its own check type (even mixed tri-state partial checking).
  • sophisticated tree content serialization.
  • edit tree data using application defined editors.

With this article I'm starting a series on how-to style articles around using the TVirtualStringTree control.

For the start, let's see how to install the Virtual TreeView in Delphi's IDE.

02
of 03

Virtual TreeView - How To Install

Virtual TreeView - Install in IDE
Virtual TreeView - Install in IDE

First, download the main Virtual TreeView package (under "Downloads").

You'll download a ZIP file containing the source code, packages to install the component in Delphi, some demos and some more stuff.

Unzip the content of the archive to some folder where you have other 3rd party components. I'm using "C:\Users\Public\Documents\Delphi3rd\" and for me the location is "C:\Users\Public\Documents\Delphi3rd\VirtualTreeviewV5.1.0"

Here's how to install the Virtual TreeView in Delphi XE3 / RAD Studio XE3

  1. Open the project group "Packages\RAD Studio XE2\RAD Studio XE3.groupproj".
  2. Right click on "VirtualTreesD16.bpl" and click "Install".
  3. Go to "Tools > Options > Environment Options > Delphi Options > Library > Library Path > [...]". Browse to the "Source" folder of Virtual TreeView, press "OK", "Add", "OK", "OK"
  4. Save the project. File - Close all.
Note: if you still using Delphi 7, the package you need to install is named "Packages\Delphi 7\VirtualTrees.bpg" for any later versions it will be ""Packages\Delphi [version]\Delphi [version].groupproj".

Once installed, you will find 3 components on the "Virtual Controls" section of the Tool Palette:

  • TVirtualStringTree - the main control you will use - manages node captions on its own.
  • TVirtualDrawTree - allows the application to draw its own stuff into the tree window.
  • TVTHeaderPopupMenu - provides a convenient way to implement a header popup used to switch visibility of columns.
03
of 03

Virtual TreeView - "Hello World" Example

Virtual TreeView - Hello World Example
Virtual TreeView - Hello World Example
Once the Virtual TreeView package is installed in the Delphi / Rad Studio IDE, let's run the sample project from the downloaded package to see if everything works :)

Load the project located under "\Demos\Minimal\", the project name is "Minimal.dpr".

Run.

See how fast is to add hundreds (even thousands) of nodes as child nodes to a selected one. Finally, here's the (important implementation) source code to this "hello world" example:

implementation

type
 PMyRec = ^TMyRec;
 TMyRec = record
 Caption: WideString;
 end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
 VST.NodeDataSize := SizeOf(TMyRec);
 VST.RootNodeCount := 20;
end;

procedure TMainForm.ClearButtonClick(Sender: TObject);
var
 Start: Cardinal;
begin
 Screen.Cursor := crHourGlass;
 try
 Start := GetTickCount;
 VST.Clear;
 Label1.Caption := Format('Last operation duration: %d ms', [GetTickCount - Start]);
 finally
 Screen.Cursor := crDefault;
 end;
end;

procedure TMainForm.AddButtonClick(Sender: TObject);
var
 Count: Cardinal;
 Start: Cardinal;
begin
 Screen.Cursor := crHourGlass;
 with VST do
 try
 Start := GetTickCount;
 case (Sender as TButton).Tag of
 0: // add to root
 begin
 Count := StrToInt(Edit1.Text);
 RootNodeCount := RootNodeCount + Count;
 end;
 1: // add as child
 if Assigned(FocusedNode) then
 begin
 Count := StrToInt(Edit1.Text);
 ChildCount[FocusedNode] := ChildCount[FocusedNode] + Count;
 Expanded[FocusedNode] := True;
 InvalidateToBottom(FocusedNode);
 end;
 end;
 Label1.Caption := Format('Last operation duration: %d ms', [GetTickCount - Start]);
 finally
 Screen.Cursor := crDefault;
 end;
end;

procedure TMainForm.VSTFreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode);
var
 Data: PMyRec;
begin
 Data := Sender.GetNodeData(Node);
 Finalize(Data^);
end;

procedure TMainForm.VSTGetText(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType; var CellText: string);
var
 Data: PMyRec;
begin
 Data := Sender.GetNodeData(Node);
 if Assigned(Data) then
 CellText := Data.Caption;
end;

procedure TMainForm.VSTInitNode(Sender: TBaseVirtualTree; ParentNode, Node: PVirtualNode; var InitialStates: TVirtualNodeInitStates);
var
 Data: PMyRec;
begin
 with Sender do
 begin
 Data := GetNodeData(Node);
 Data.Caption := Format('Level %d, Index %d', [GetNodeLevel(Node), Node.Index]);
 end;
end;
For the moment I'll not go into details ... this will follow ...
Format
mla apa chicago
Your Citation
Gajic, Zarko. "Virtual Tree View - How To Install - Delphi 3rd Party Open Source Component." ThoughtCo, Jun. 23, 2017, thoughtco.com/virtual-tree-view-1058355. Gajic, Zarko. (2017, June 23). Virtual Tree View - How To Install - Delphi 3rd Party Open Source Component. Retrieved from https://www.thoughtco.com/virtual-tree-view-1058355 Gajic, Zarko. "Virtual Tree View - How To Install - Delphi 3rd Party Open Source Component." ThoughtCo. https://www.thoughtco.com/virtual-tree-view-1058355 (accessed October 20, 2017).