学习 TTreeView [11] - Images、ImageIndex、SelectedIndex、StateImages、StateIndex

本例效果图:

学习 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 可以再添加一个状态图标, 譬如:

学习 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;

  {多添加了下面两行}

  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.


 
   

你可能感兴趣的:(treeview)