学习 TTreeView [11] - Images、ImageIndex、SelectedIndex、StateImages、StateIndex
学习 TTreeView [11] - Images、ImageIndex、SelectedIndex、StateImages、StateIndex
涂孟超 发表于3年前
学习 TTreeView [11] - Images、ImageIndex、SelectedIndex、StateImages、StateIndex
  • 发表于 3年前
  • 阅读 2
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

本例效果图:


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.

 
 
 
 
 

 

 

  
共有 人打赏支持
粉丝 13
博文 2004
码字总数 14107
×
涂孟超
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: