学习 TTreeView [14] - StateIndex(状态图标)、OverlayIndex(叠加图标)

先给 ImageList1 添加图标如下:

学习 TTreeView [14] - StateIndex(状态图标)、OverlayIndex(叠加图标)
关于 OverlayIndex 的提示:
在给一个节点指定 OverlayIndex 以前, 需要先用 ImageList1.Overlay 指定可用的 OverlayIndex 号.

测试效果图:

学习 TTreeView [14] - StateIndex(状态图标)、OverlayIndex(叠加图标)
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    ImageList1: TImageList;
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure Edit2KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure TreeView1Change(Sender: TObject; Node: TTreeNode);
    procedure Button1Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  node: TTreeNode;
begin
  {指定 ImageList}
  TreeView1.Images      := ImageList1;
  TreeView1.StateImages := ImageList1;

  {设定 OverlayIndex, 最多 4 个}
  ImageList1.Overlay(3,0); {参数1是 ImageList 中的序号; 参数2是指定可选的 OverlayIndex}
  ImageList1.Overlay(4,1);
  ImageList1.Overlay(5,2);
  ImageList1.Overlay(6,3);

  Label1.Caption := 'StateIndex:';
  Label2.Caption := 'OverlayIndex:';

  Edit1.Text := '-1';
  Edit2.Text := '-1';

  Button1.Caption := '初始化';

  {给 TreeView1 填充}
  with TreeView1 do begin
    Align := alLeft;
    Items.Add(nil, 'AAA');
    Items.Add(nil, 'BBB');
    Items.Add(nil, 'CCC');
    Items.Add(nil, 'DDD');
    Items.Add(nil, 'EEE');
    Items.AddChild(Items[4], '111');
    Items.AddChild(Items[4], '222');
    Items.AddChild(Items[4], '333');
    Items.AddChild(Items[4], '444');
  end;

  {指定图标状态}
  for node in TreeView1.Items do
  begin
    node.ImageIndex := 0;    {默认图标为第 0 个}
    node.SelectedIndex := 1; {选择图标为第 1 个}
    node.StateIndex := -1;   {状态图标暂无}
  end;
end;

{Edit1 控制 StateIndex}
procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
var
  c: Char;
  edit: TEdit;
begin
  edit := TEdit(Sender);
  c := Chr(Key);
  if c in ['0'..'6'] then edit.Text := c else edit.Text := '-1'; {字符限制}

  TreeView1.Selected.StateIndex := StrToInt(edit.Text);          {作用于 TreeView}
end;

{Edit2 控制 OverlayIndex}
procedure TForm1.Edit2KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
var
  c: Char;
  edit: TEdit;
begin
  edit := TEdit(Sender);
  c := Chr(Key);
  if c in ['0'..'3'] then edit.Text := c else edit.Text := '-1'; {字符限制}

  TreeView1.Selected.OverlayIndex := StrToInt(edit.Text);        {作用于 TreeView}
end;

procedure TForm1.TreeView1Change(Sender: TObject; Node: TTreeNode);
begin
  Edit1.Text := IntToStr(Node.StateIndex);
  Edit2.Text := IntToStr(Node.OverlayIndex);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  TreeView1.Items.Clear;
  FormCreate(nil);
end;

end.

 
 
 
 
 

 

 
  

你可能感兴趣的:(学习 TTreeView [14] - StateIndex(状态图标)、OverlayIndex(叠加图标))