先給 ImageList1 添加圖標如下:
圖片看不清楚?請點擊這裡查看原圖(大圖)。
關於 OverlayIndex 的提示:
在給一個節點指定 OverlayIndex 以前, 需要先用 ImageList1.Overlay 指定可用的 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.