imagelist里放未选择和选择的图片对应0、1位置
这里我为了方便简单的用node.data存是否点击,你可以写个结构放到data里来存储
设置treeview的images为你的imagelist
procedure TForm1.FormCreate(Sender: TObject);
var
i,j:integer;
node,pnode:ttreenode;
begin
for i:=0 to 4do
begin
pnode:=treeview1.Items.AddChild(nil,'PIndex'+inttostr(i));
pnode.ImageIndex:=0;
pnode.Data:=pointer(false);
for j:=0 to 4do
begin
node:=treeview1.Items.AddChild(pnode,'CIndex'+inttostr(j));
node.ImageIndex:=0;
node.Data:=pointer(false);
end;
end;
nflash:=false;
end;
procedure TForm1.TreeView1MouseDown(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer);
var
node:TTreeNode;
r:TRect;
begin
node:=TTreeView(Sender).GetNodeAt(x,y);
if assigned(node) then
begin
r:=node.DisplayRect(true);
r.Left:=r.Left-ImageList1.Width;
if ptinrect(r,point(x,y)) then
{判断是否点在收缩按钮上或者节点外}
begin
node.Data:=pointer(not boolean(node.Data));
invalidateRect(TTreeView(Sender).Handle,@r,true);
end;
end;
end;
procedure TForm1.TreeView1AdvancedCustomDrawItem(Sender: TCustomTreeView;
Node: TTreeNode;
State: TCustomDrawState;
Stage: TCustomDrawStage;
var PaintImages, DefaultDraw: Boolean);
var
r:TRect;
begin
DefaultDraw:=true;
PaintImages:=false;
if assigned(node) then
begin
r:=node.DisplayRect(true);
with TTreeView(Sender)do
begin
Canvas.FillRect(r);
if boolean(node.Data) then
imagelist1.Draw(Canvas,r.Left-imagelist1.Width,r.Top-1,0)
else
imagelist1.Draw(Canvas,r.Left-imagelist1.Width,r.Top-1,1);
Canvas.TextOut(r.Left+2,r.Top+1,node.Text);
end;
end;
end;