I don't know for sure, but I have a problem with the icon size for some shell objects. They appear to be 32x32 even if I use GetIcon(16). Folders and some special objects work perfectly. Here is my updated TreeView listing which now works pretty much like Explorer, but it shows also the files.
I also found that Desktop.Description gives an empty string on my machine (WinXP Prof.), so I use .Name in that case.
Do you have any idea what's going wrong here?
Code: Select all
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, madshell;
type
TForm1 = class(TForm)
TreeView1: TTreeView;
procedure TreeView1Expanding(Sender: TObject; Node: TTreeNode;
var AllowExpansion: Boolean);
procedure TreeView1AdvancedCustomDrawItem(Sender: TCustomTreeView;
Node: TTreeNode; State: TCustomDrawState; Stage: TCustomDrawStage;
var PaintImages, DefaultDraw: Boolean);
procedure FormCreate(Sender: TObject);
private
{ Private-Deklarationen }
first:boolean;
treeitems:array of IShellObj;
procedure addnode(node:TTreeNode; ishell:IShellObj);
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.addnode;
//Creates a folder/file node and adds a ShellObj element in the TREEITEMS array
var
index:integer;
newnode:TTreeNode;
begin
//add new item to TREEITEMS array
index:=length(treeitems);
setlength(treeitems,index+1);
treeitems[index]:=ishell;
//create a corresponding node and hold the index of the TREEITEMS-item in
//node.data
with treeitems[index] do
if length(description)>0 then node:=TreeView1.Items.AddChild(node,Description)
else node:=TreeView1.Items.AddChild(node,name); //** Desktop's description is empty! **
if node<>nil then begin
node.Data:=pointer(index);
if treeitems[index].itemcount>0 then begin
//Add a dummy child to the node to get a '+' before the tree item.
//That dummy child is replaced by sub-items when the node is expanded.
newnode:=TreeView1.Items.AddChild(node,'*');
newnode.Data:=pointer(-1);
end; {if}
end; {if}
end;
procedure TForm1.FormCreate(Sender: TObject);
var
node,newnode:TTreeNode;
begin
//initialize the tree and create any of the roots.
setlength(treeitems,0);
addnode(nil,Desktop);
end;
procedure TForm1.TreeView1AdvancedCustomDrawItem(Sender: TCustomTreeView;
Node: TTreeNode; State: TCustomDrawState; Stage: TCustomDrawStage;
var PaintImages, DefaultDraw: Boolean);
var
rect:TRect;
icon:TIcon;
iss:IShellObj;
begin
if node<>nil then begin
rect:=node.DisplayRect(true);
with treeview1.Canvas do begin
brush.Color:=clYellow;
brush.Style:=bsSolid;
rect.Right:=rect.Right+16;
textrect(rect,rect.Left+16+2,rect.Top+2,node.text);
if integer(node.Data)>=0 then begin
iss:=treeitems[integer(node.Data)];
icon:=TIcon.Create;
icon.Handle:=iss.geticon(16);
draw(rect.Left,rect.Top,icon); //Sometimes 32x32!!!
icon.Free;
end; {if}
end; {with}
end; {if}
end;
procedure TForm1.TreeView1Expanding(Sender: TObject; Node: TTreeNode;
var AllowExpansion: Boolean);
var
i,index:integer;
iss:IShellObj;
newnode:TTreeNode;
begin
if (not first) and (node<>nil) then begin
//Zuerst den Dummy, falls vorhanden, löschen:
if node.HasChildren and (node.getFirstChild.text='*') then begin
node.DeleteChildren;
if integer(node.data)>=0 then begin
//Nun die Unter-Items bestimmen und anhängen:
iss:=treeitems[integer(node.Data)];
if iss.itemcount>0 then begin
for i:=0 to iss.ItemCount-1 do
addnode(node,iss.Items[i]);
end; {if}
end; {if}
end; {if}
end; {if}
end;
end.