最强开源Delphi树形控件VirtualTreeView示例

十多年的发展创造了当今最灵活,最先进的Delphi树视图控件!VirtualTreeview不会读取其管理的数据,除了其大小,甚至节点的标题也是如此。一切都是通过事件或后代(通过重写方法)从应用程序中检索的。Virtual Treeview已经证明了其概念以及在许多商业产品和免费软件项目中的出色可用性。欢迎加入Delphi开发局QQ群:32422310  Delphi控件源码下载网站

unit Main;

// Demonstration project for TVirtualStringTree to generally show how to get started.
// Written by Mike Lischke.

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  VirtualTrees, StdCtrls, ExtCtrls;

type
  TMainForm = class(TForm)
    VST: TVirtualStringTree;
    ClearButton: TButton;
    AddOneButton: TButton;
    Edit1: TEdit;
    Button1: TButton;
    Label1: TLabel;
    CloseButton: TButton;
    procedure FormCreate(Sender: TObject);
    procedure ClearButtonClick(Sender: TObject);
    procedure AddButtonClick(Sender: TObject);
    procedure VSTFreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode);
    procedure VSTInitNode(Sender: TBaseVirtualTree; ParentNode, Node: PVirtualNode;
      var InitialStates: TVirtualNodeInitStates);
    procedure CloseButtonClick(Sender: TObject);
    procedure VSTGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
      Column: TColumnIndex; TextType: TVSTTextType; var CellText: string);
    procedure VSTAddToSelection(Sender: TBaseVirtualTree; Node: PVirtualNode);
    procedure VSTAfterPaint(Sender: TBaseVirtualTree; TargetCanvas: TCanvas);
    procedure VSTAfterItemPaint(Sender: TBaseVirtualTree; TargetCanvas: TCanvas;
      Node: PVirtualNode; ItemRect: TRect);
    procedure VSTChecked(Sender: TBaseVirtualTree; Node: PVirtualNode);
  end;

var
  MainForm: TMainForm;

//----------------------------------------------------------------------------------------------------------------------

implementation

{$R *.DFM}

type
  // This is a very simple record we use to store data in the nodes.
  // 这是一个非常简单的记录类型我们用来在节点中储存数据
  // Since the application is responsible to manage all data including the node's caption
  //因为应用程序负责管理包括节点标题在内的所有数据
  // this record can be considered as minimal requirement in all VT applications.
   //该记录可被视为所有VT应用的最低要求。
  // Extend it to whatever your application needs.
  //根据你的应用需求扩展这个示例
  PMyRec = ^TMyRec;//记录指针
  TMyRec = record
    Caption: WideString; //标题
  end;

//----------------------------------------------------------------------------------------------------------------------

procedure TMainForm.FormCreate(Sender: TObject);

begin
  // Let the tree know how much data space we need.
  //让树形控件知道我们需要多少数据空间。
  VST.NodeDataSize := SizeOf(TMyRec);
  // Set an initial number of nodes.设置一个节点初始数值
  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
  // Add some nodes to the treeview. 添加节点
  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.VSTGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
  Column: TColumnIndex; TextType: TVSTTextType; var CellText: string);

var
  Data: PMyRec;

begin
  // A handler for the OnGetText event is always needed as it provides the tree with the string data to display.
  //始终需要OnGetText事件的处理程序,因为它向树形控件提供要显示的字符串数据。
  Data := Sender.GetNodeData(Node); //获取节点数据
  if Assigned(Data) then
    CellText := Data.Caption; //显示节点数据
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TMainForm.VSTAddToSelection(Sender: TBaseVirtualTree;
  Node: PVirtualNode);
begin
 // VST.Colors.SelectionTextColor:=clred;

end;

procedure TMainForm.VSTAfterItemPaint(Sender: TBaseVirtualTree;
  TargetCanvas: TCanvas; Node: PVirtualNode; ItemRect: TRect);
begin
           VST.Colors.SelectionTextColor:=clred; //选择节点显示红色
end;

procedure TMainForm.VSTAfterPaint(Sender: TBaseVirtualTree;
  TargetCanvas: TCanvas);
begin
      VST.Colors.SelectionTextColor:=clred;  //选择节点显示红色
end;

procedure TMainForm.VSTChecked(Sender: TBaseVirtualTree; Node: PVirtualNode);
begin
   VST.Colors.SelectionTextColor:=clred;  //选择节点显示红色
end;

procedure TMainForm.VSTFreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode);

var
  Data: PMyRec;

begin
  Data := Sender.GetNodeData(Node);
  // Explicitely free the string, the VCL cannot know that there is one but needs to free
  // it nonetheless. For more fields in such a record which must be freed use Finalize(Data^) instead touching
  // every member individually.
//明确地释放字符串,VCL无法知道有一个,但需要释放
//尽管如此。对于此类记录中必须释放的更多字段,请使用Finalize(Data^)代替touch
//每个成员都是单独的。
  Finalize(Data^);
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TMainForm.VSTInitNode(Sender: TBaseVirtualTree; ParentNode, Node: PVirtualNode;
  var InitialStates: TVirtualNodeInitStates);

var
  Data: PMyRec;

begin
  with Sender do
  begin
    Data := GetNodeData(Node);
    // Construct a node caption. This event is triggered once for each node but
    // appears asynchronously, which means when the node is displayed not when it is added.
//构造节点标题。每个节点触发一次此事件,但是
//异步显示,这意味着当节点显示时,而不是在添加节点时。
    Data.Caption := Format('Level %d, Index %d', [GetNodeLevel(Node), Node.Index]);
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TMainForm.CloseButtonClick(Sender: TObject);

begin
  Close;
end;


end.

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值