Virtual Tree View: Delphi 3rd Party Open Source Component

01
of 03

About Virtual TreeView

Virtual Tree View - Sample In Action

Virtual Tree View

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 third 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

How To Install Virtual TreeView

Virtual TreeView - Install in IDE

Virtual TreeView 

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 third 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.

Once installed, you will find three 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

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 dotry
Start := GetTickCount;
case (Sender as TButton).Tag of
0: // add to rootbegin
Count := StrToInt(Edit1.Text);
RootNodeCount := RootNodeCount + Count;
end;
1: // add as childif Assigned(FocusedNode) thenbegin
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;
beginwith Sender dobegin
Data := GetNodeData(Node);
Data.Caption := Format('Level %d, Index %d', [GetNodeLevel(Node), Node.Index]);
end;
end;
Format
mla apa chicago
Your Citation
Gajic, Zarko. "Virtual Tree View: Delphi 3rd Party Open Source Component." ThoughtCo, Aug. 25, 2020, thoughtco.com/virtual-tree-view-1058355. Gajic, Zarko. (2020, August 25). Virtual Tree View: Delphi 3rd Party Open Source Component. Retrieved from https://www.thoughtco.com/virtual-tree-view-1058355 Gajic, Zarko. "Virtual Tree View: Delphi 3rd Party Open Source Component." ThoughtCo. https://www.thoughtco.com/virtual-tree-view-1058355 (accessed March 19, 2024).