treeview.pas

来自「详细说明:毕业论文中关于小型宾馆管理系统的详细设计毕 业论文中关于小型宾馆...」· PAS 代码 · 共 210 行

PAS
210
字号
//
// This unit allows for the easy creation of a tree-view of a folder
//
// (c) ChiconySoftware 2001
//
// When       Who      How
// ---------  ---      -------------------------------------------------------
// 2001.5.31  century  Initial version
//
//
unit treeview;

interface

uses Windows, SysUtils, Classes, Graphics, Forms, Controls, StdCtrls,
  Buttons, ExtCtrls, ComCtrls, Dialogs, ImgList;

// The TNodeRec record is used in the tree to store information about nodes
type
   PNodeRec = ^TNodeRec;
   TNodeRec = record
     refnum: string;           // reference number of node, e.g. document ID
   end;

type
  TExplore = class(TForm)
    FldrTree: TTreeView;
    ImageList: TImageList;
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FldrTreeDblClick(Sender: TObject);
  private
    AlphaSort: Boolean;          // Set to TRUE to sort the tree
                                 // FALSE by default
                                 // See the method AlphaSorting
  public
    selected_item: String;       // The item that was selected
                                 // EMPTY string means nothing was selected

    procedure AlphaSorting(sort: Boolean);
    function AddItem(const parent_id: String; parent_level: Integer; const child_id, child_description: String): Boolean;
    function SelectItem(const item_id: String; item_level: Integer): Boolean;
  end;

implementation

{$R *.DFM}

//
// Highlight an item in the tree
//
// Args: id of the item to highlight (empty string means first item of item_level)
//
//       level of the item (0=top,1=second,etc.). pass -1 if the item
//       must be a leaf
//
// Returns: TRUE on success
//
// Example: To highlight the first level 1 item: SelectItem('', 1);
//
//          To highlight the first leaf: SelectItem('', -1);
//
function TExplore.SelectItem(const item_id: String; item_level: Integer): Boolean;
var i: Integer;
    NodePtr: PNodeRec;
begin
     for i:=0 to FldrTree.Items.Count - 1 do begin
         NodePtr:=PNodeRec(FldrTree.Items[i].Data);
         if ((item_level < 0) and (not FldrTree.Items[i].HasChildren)) or
            (FldrTree.Items[i].Level=item_level) then begin
            if (item_id='') or (NodePtr^.refnum=item_id) then begin
               FldrTree.Selected:=FldrTree.Items[i];
               selected_item:=NodePtr^.refnum;
               Result:=TRUE;
               Exit;
            end;
         end;
     end;
     Result:=FALSE;
end;

//
// Add a (child) item to the tree.
//
// Args: parent item to make this a child of (ignored if creating a root item)
//
//       the level of the parent (0=top,1=second,etc.). pass -1 to create a
//       root item
//
//       id of the child item being added to the tree. this value is not
//       displayed but returned when a user selects the item. its also used
//       when adding child items to this item
//
//       description of child item being added to the tree. this value is
//       displayed
//
// Returns: TRUE on success
//
function TExplore.AddItem(const parent_id: String; parent_level: Integer;
    const child_id, child_description: String): Boolean;
var i: Integer;
    NodePtr: PNodeRec;
    parent_item, child_item: TTreeNode;
begin
     // Adding a root item?
     if (parent_level < 0) then begin
        // First item in the tree
        New(NodePtr);
        NodePtr^.refnum:=child_id;

        // Add the node to the tree
        child_item:=FldrTree.Items.AddObject(nil, child_description, NodePtr);
        child_item.SelectedIndex:=child_item.Level;
        child_item.ImageIndex:=child_item.Level + 4;

        // Success
        Result:=TRUE;
        Exit;
     end;

     try
        // Are we adding this item to a specific parent?
        parent_item:=nil;
        if parent_id='' then begin
           // Add to the last item of that level
           for i:=0 to FldrTree.Items.Count - 1 do begin
               if FldrTree.Items[i].Level=parent_level then
                  parent_item:=FldrTree.Items[i];
           end;
        end else begin
           // Find the folder to add the transaction to
           for i:=0 to FldrTree.Items.Count - 1 do begin
               NodePtr:=PNodeRec(FldrTree.Items[i].Data);
               if (FldrTree.Items[i].Level=parent_level) and (NodePtr^.refnum=parent_id) then begin
                  parent_item:=FldrTree.Items[i];
                  Break;
               end;
           end;
        end;

        // Found the folder?
        if parent_item=nil then begin
           Result:=FALSE;
           Exit;
        end;

        // Create a new transaction node
        New(NodePtr);
        NodePtr^.refnum:=child_id;

        // Add the node to the tree
        if parent_item=nil then child_item:=FldrTree.Items.AddObject(nil, child_description, NodePtr)
        else child_item:=FldrTree.Items.AddChildObject(parent_item, child_description, NodePtr);
        child_item.SelectedIndex:=child_item.Level;
        child_item.ImageIndex:=child_item.Level + 4;

        // Expand this section of the tree automatically
        if parent_item<>nil then parent_item.Expand(FALSE);
        if AlphaSort then FldrTree.AlphaSort;

        // Success
        Result:=TRUE;
     except
        Result:=FALSE;
     end;
end;

// Initialise form
procedure TExplore.FormCreate(Sender: TObject);
begin
     AlphaSort:=FALSE;
     FldrTree.SortType:=stText;
end;

// Enable/disable tree sorting
procedure TExplore.AlphaSorting(sort: Boolean);
begin
     AlphaSort:=not AlphaSort;
     if AlphaSort then FldrTree.AlphaSort;
end;

// Initialise
procedure TExplore.FormShow(Sender: TObject);
begin
     selected_item:='';
end;

// Free up memory used by the form
procedure TExplore.FormDestroy(Sender: TObject);
var i: integer;
begin
     for i:=1 to FldrTree.Items.Count do Dispose(FldrTree.Items[i-1].Data);
end;

// The user has selected an item
procedure TExplore.FldrTreeDblClick(Sender: TObject);
begin
     // Valid item?
     if FldrTree.Selected=nil then Exit;

     // Child item?
     if not FldrTree.Selected.HasChildren then begin
        selected_item:=PNodeRec(FldrTree.Selected.Data)^.refnum;
        Close;
     end;
end;

end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?