zoukankan      html  css  js  c++  java
  • 学习 TTreeView [11] Images、ImageIndex、SelectedIndex、StateImages、StateIndex

    本例效果图:


    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ImgList, ComCtrls;
    
    type
      TForm1 = class(TForm)
        TreeView1: TTreeView;
        ImageList1: TImageList;
        procedure FormCreate(Sender: TObject);
        procedure TreeView1Expanded(Sender: TObject; Node: TTreeNode);
        procedure TreeView1Collapsed(Sender: TObject; Node: TTreeNode);
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    procedure TForm1.FormCreate(Sender: TObject);
    var
      stream: TStream;
      node: TTreeNode;
    begin
      TreeView1.Align := alLeft;
      stream := TMemoryStream.Create;
      with TStringList.Create do begin
        Add('一连长');
        Add(' 一排长');
        Add('  一班长');
        Add('   战士1');
        Add('   战士2');
        Add('   战士3');
        Add('   战士4');
        Add('  二班长');
        Add('  三班长');
        Add(' 二排长');
        Add(' 三排长');
        Add('二连长');
        Add('三连长');
        SaveToStream(stream);
        Free;
      end;
      stream.Position := 0;
      TreeView1.LoadFromStream(stream);
      stream.Free;
    
      {指定图像列表}
      TreeView1.Images := ImageList1;
    
      {初始化图标}
      for node in TreeView1.Items do
      begin
        if node.HasChildren then begin
          Node.ImageIndex := 0;
          Node.SelectedIndex := 0;
        end else begin
          node.ImageIndex := 2;
          node.SelectedIndex := 3;
        end;
      end;
    end;
    
    {闭合时}
    procedure TForm1.TreeView1Collapsed(Sender: TObject; Node: TTreeNode);
    begin
      Node.ImageIndex := 0;
      Node.SelectedIndex := 0;
    end;
    
    {展开时}
    procedure TForm1.TreeView1Expanded(Sender: TObject; Node: TTreeNode);
    begin
      Node.ImageIndex := 1;
      Node.SelectedIndex := 1;
    end;
    
    end.
    
    通过 StateImages、StateIndex 可以再添加一个状态图标, 譬如:



    比上面的程序多了两行代码:
    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ImgList, ComCtrls;
    
    type
      TForm1 = class(TForm)
        TreeView1: TTreeView;
        ImageList1: TImageList;
        procedure FormCreate(Sender: TObject);
        procedure TreeView1Expanded(Sender: TObject; Node: TTreeNode);
        procedure TreeView1Collapsed(Sender: TObject; Node: TTreeNode);
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    procedure TForm1.FormCreate(Sender: TObject);
    var
      stream: TStream;
      node: TTreeNode;
    begin
      TreeView1.Align := alLeft;
      stream := TMemoryStream.Create;
      with TStringList.Create do begin
        Add('一连长');
        Add(' 一排长');
        Add('  一班长');
        Add('   战士1');
        Add('   战士2');
        Add('   战士3');
        Add('   战士4');
        Add('  二班长');
        Add('  三班长');
        Add(' 二排长');
        Add(' 三排长');
        Add('二连长');
        Add('三连长');
        SaveToStream(stream);
        Free;
      end;
      stream.Position := 0;
      TreeView1.LoadFromStream(stream);
      stream.Free;
    
      TreeView1.Images := ImageList1;
      {多添加了下面两行}
      TreeView1.StateImages := ImageList1;
      TreeView1.Items[0].StateIndex := 4; {使用 ImageList1 中的第五个图标}
    
      for node in TreeView1.Items do
      begin
        if node.HasChildren then begin
          Node.ImageIndex := 0;
          Node.SelectedIndex := 0;
        end else begin
          node.ImageIndex := 2;
          node.SelectedIndex := 3;
        end;
      end;
    end;
    
    procedure TForm1.TreeView1Collapsed(Sender: TObject; Node: TTreeNode);
    begin
      Node.ImageIndex := 0;
      Node.SelectedIndex := 0;
    end;
    
    procedure TForm1.TreeView1Expanded(Sender: TObject; Node: TTreeNode);
    begin
      Node.ImageIndex := 1;
      Node.SelectedIndex := 1;
    end;
    
    end.
    
  • 相关阅读:
    Sqlserver中的触发器
    Memcache在.Net中的使用
    Sqlserver中的储存过程
    Ado.NET基础必备
    MVC项目报错 ”基础提供程序在 Open 上失败”
    【Darwin学习笔记】之获取系统处理器数量的方法
    Wireshark抓包工具--TCP数据包seq ack等解读
    TCP:WireShark分析,序列号Seq和确认号Ack
    【Darwin学习笔记】之TaskThread
    boost::asio::io_context类
  • 原文地址:https://www.cnblogs.com/del/p/1104790.html
Copyright © 2011-2022 走看看