unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, ShlObj;
type
PTreeViewItem = ^TTreeViewItem;
TTreeViewItem = record
ParentFolder: IShellFolder;
Pidl, FullPidl: PItemIDList;
HasExpanded: Boolean;
end;
TBrowseTreeView = class(TForm)
TreeView1: TTreeView;
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure TreeView1Expanding(Sender: TObject;
Node: TTreeNode;
var AllowExpansion: Boolean);
private
FItemList: TList;
procedure SetTreeViewImageList;
procedure FillTreeView(Folder: IShellFolder;
FullPIDL: PItemIDList;
ParentNode: TTreeNode);
{ Private declarations }
public
{ Public declarations }
end;
var
BrowseTreeView: TBrowseTreeView;
implementation
uses
ActiveX, ComObj, ShellAPI, CommCtrl;
{$R *.DFM}
procedure DisposePIDL(ID: PItemIDList);
var
Malloc: IMalloc;
begin
if ID = nil then
Exit;
OLECheck(SHGetMalloc(Malloc));
Malloc.Free(ID);
end;
function CopyItemID(ID: PItemIDList): PItemIDList;
var
Malloc: IMalloc;
begin
Result := nil;
OLECheck(SHGetMalloc(Malloc));
if Assigned(ID) then
begin
Result := Malloc.Alloc(ID^.mkid.cb + sizeof(ID^.mkid.cb));
CopyMemory(Result, ID, ID^.mkid.cb + sizeof(ID^.mkid.cb));
end;
end;
function NextPIDL(ID: PItemIDList): PItemIDList;
begin
Result := ID;
Inc(PChar(Result), ID^.mkid.cb);
end;
function GetPIDLSize(ID: PItemIDList): Integer;
begin
Result := 0;
if Assigned(ID) then
begin
Result := sizeof(ID^.mkid.cb);
while ID^.mkid.cb <> 0do
begin
Inc(Result, ID^.mkid.cb);
ID := NextPIDL(ID);
end;
end;
end;
function CreatePIDL(Size: Integer): PItemIDList;
var
Malloc: IMalloc;
HR: HResult;
begin
Result := nil;
HR := SHGetMalloc(Malloc);
if Failed(HR) then
Exit;
try
Result := Malloc.Alloc(Size);
if Assigned(Result) then
FillChar(Result^, Size, 0);
finally
end;
end;
function ConcatPIDLs(ID1, ID2: PItemIDList): PItemIDList;
var
cb1, cb2: Integer;
begin
if Assigned(ID1) then
cb1 := GetPIDLSize(ID1) - sizeof(ID1^.mkid.cb)
else
cb1 := 0;
cb2 := GetPIDLSize(ID2);
Result := CreatePIDL(cb1 + cb2);
if Assigned(Result) then
begin
if Assigned(ID1) then
CopyMemory(Result, ID1, cb1);
CopyMemory(PChar(Result) + cb1, ID2, cb2);
end;
end;
function GetDisplayName(Folder: IShellFolder;
PIDL: PItemIDList;
ForParsing: Boolean): String;
var
StrRet: TStrRet;
P: PChar;
Flags: Integer;
begin
Result := '';
if ForParsing then
Flags := SHGDN_FORPARSING
else
Flags := SHGDN_NORMAL;
Folder.GetDisplayNameOf(PIDL, Flags, StrRet);
case StrRet.uType of
STRRET_CSTR:
SetString(Result, StrRet.cStr, lStrLen(StrRet.cStr));
STRRET_OFFSET:
begin
P := @PIDL.mkid.abID[StrRet.uOffset - sizeof(PIDL.mkid.cb)];
SetString(Result, P, PIDL.mkid.cb - StrRet.uOffset);
end;
STRRET_WSTR:
Result := StrRet.pOleStr;
end;
end;
function GetIcon(PIDL: PItemIDList;
Open: Boolean): Integer;
const
IconFlag = SHGFI_PIDL or SHGFI_SYSICONINDEX or SHGFI_SMALLICON;
var
FileInfo: TSHFileInfo;
Flags: Integer;
begin
if Open then
Flags := IconFlag or SHGFI_OPENICON
else
Flags := IconFlag;
SHGetFileInfo(PChar(PIDL), 0, FileInfo, sizeof(TSHFileInfo), Flags);
Result := FileInfo.iIcon;
end;
procedure TBrowseTreeView.FormDestroy(Sender: TObject);
var
I: Integer;
begin
try
for I := 0 to FItemList.Count-1do
begin
DisposePIDL(PTreeViewItem(FItemList).PIDL);
DisposePIDL(PTreeViewItem(FItemList).FullPIDL);
end;
FItemList.Clear;
FItemList.Free;
except
end;
end;
procedure GetItemIcons(FullPIDL: PItemIDList;
TreeNode: TTreeNode);
begin
with TreeNodedo
begin
ImageIndex := GetIcon(FullPIDL, False);
SelectedIndex := GetIcon(FullPIDL, True);
end;
end;
procedure TBrowseTreeView.SetTreeViewImageList;
var
ImageList: THandle;
FileInfo: TSHFileInfo;
begin
ImageList := SHGetFileInfo(PChar('C:/'), 0, FileInfo,
sizeof(TSHFileInfo), SHGFI_SYSICONINDEX or SHGFI_SMALLICON);
if ImageList <> 0 then
TreeView_SetImageList(TreeView1.Handle, ImageList, 0);
end;
procedure TBrowseTreeView.FillTreeView(Folder: IShellFolder;
FullPIDL: PItemIDList;
ParentNode: TTreeNode);
var
TreeViewItem: PTreeViewItem;
EnumIDList: IEnumIDList;
PIDLs, FullItemPIDL: PItemIDList;
NumID: LongWord;
ChildNode: TTreeNode;
Attr: Cardinal;
begin
try
OLECheck(Folder.EnumObjects(Handle, SHCONTF_FOLDERS, EnumIDList));
while EnumIDList.Next(1, PIDLs, NumID) = S_OKdo
begin
FullItemPIDL := ConcatPIDLs(FullPIDL, PIDLs);
TreeViewItem := New(PTreeViewItem);
TreeViewItem.ParentFolder := Folder;
TreeViewItem.Pidl := CopyItemID(PIDLs);
TreeViewItem.FullPidl := FullItemPIDL;
TreeViewItem.HasExpanded := False;
FItemList.Add(TreeViewItem);
ChildNode := TreeView1.Items.AddChildObject(ParentNode,
GetDisplayName(Folder, PIDLs, False), TreeViewItem);
GetItemIcons(FullItemPIDL, ChildNode);
Attr := SFGAO_HASSUBFOLDER or SFGAO_FOLDER;
Folder.GetAttributesOf(1, PIDLs, Attr);
if Bool(Attr and (SFGAO_HASSUBFOLDER or SFGAO_FOLDER)) then
if Bool(Attr and SFGAO_FOLDER) then
if Bool(Attr and SFGAO_HASSUBFOLDER) then
ChildNode.HasChildren := True;
end;
except
end;
end;
procedure TBrowseTreeView.FormCreate(Sender: TObject);
var
Folder: IShellFolder;
begin
SetTreeViewImageList;
OLECheck(SHGetDesktopFolder(Folder));
FItemList := TList.Create;
FillTreeView(Folder, nil, nil);
end;
procedure TBrowseTreeView.TreeView1Expanding(Sender: TObject;
Node: TTreeNode;
var AllowExpansion: Boolean);
var
TVItem: PTreeViewItem;
SHFolder: IShellFolder;
begin
TVItem := PTreeViewItem(Node.Data);
if TVItem.HasExpanded then
Exit;
OLECheck(TVItem.ParentFolder.BindToObject(TVItem^.Pidl,
nil, IID_IShellFolder, Pointer(SHFolder)));
FillTreeView(SHFolder, TVItem^.FullPidl, Node);
Node.AlphaSort;
TVItem^.HasExpanded := True;
end;
end.
-----------
原来是VC呀