摘要
關鍵字
一
Windows
二
Windows文件夾管理樹的實現實質上是對Windows名空間(Namespace)的遍歷
下面解釋將要用到的幾個主要函數
該函數通過ppshf獲得桌面文件夾的IShellFolder接口
該函數獲得一個IEnumIDList接口
該函數獲得某個子文件夾的IShellFolder接口
其它函數可以查閱Delphi提供的《Win
三
下面的源代碼在Windows
unit BrowseTreeView;
interface
uses
Windows
ShlObj
type
PTreeViewItem = ^TTreeViewItem;
TTreeViewItem = record
ParentFolder: IShellFolder; // 接點對應的文件夾的父文件夾的IShellFolder接口
Pidl
HasExpanded: Boolean; // 接點是否展開
end;
TForm
TreeView
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure TreeView
var AllowExpansion: Boolean);
private
FItemList: TList;
procedure SetTreeViewImageList;
procedure FillTreeView(Folder: IShellFolder; FullPIDL: PItemIDList; ParentNode: TTreeNode);
end;
var
Form
implementation
{$R *
uses
ActiveX
// 以下是幾個對項目標識符進行操作的函數
procedure DisposePIDL(ID: PItemIDList);
var
Malloc: IMalloc;
begin
if ID = nil then Exit;
OLECheck(SHGetMalloc(Malloc));
Malloc
end;
function CopyItemID(ID: PItemIDList): PItemIDList;
var
Malloc: IMalloc;
begin
Result := nil;
OLECheck(SHGetMalloc(Malloc));
if Assigned(ID) then
begin
Result := Malloc
CopyMemory(Result
end;
end;
function NextPIDL(ID: PItemIDList): PItemIDList;
begin
Result := ID;
Inc(PChar(Result)
end;
function GetPIDLSize(ID: PItemIDList): Integer;
begin
Result :=
if Assigned(ID) then
begin
Result := sizeof(ID^
while ID^
begin
Inc(Result
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
if Assigned(Result) then
FillChar(Result^
finally
end;
end;
function ConcatPIDLs(ID
var
cb
begin
if Assigned(ID
cb
else
cb
cb
Result := CreatePIDL(cb
if Assigned(Result) then
begin
if Assigned(ID
CopyMemory(Result
CopyMemory(PChar(Result) + cb
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
case StrRet
STRRET_CSTR:
SetString(Result
STRRET_OFFSET:
begin
P := @PIDL
SetString(Result
end;
STRRET_WSTR:
Result := StrRet
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)
Result := FileInfo
end;
// 獲得每個文件夾在系統中的圖標
procedure GetItemIcons(FullPIDL: PItemIDList; TreeNode: TTreeNode);
begin
with TreeNode do
begin
ImageIndex := GetIcon(FullPIDL
SelectedIndex := GetIcon(FullPIDL
end;
end;
// 獲得系統的圖標列表
procedure TForm
var
ImageList: THandle;
FileInfo: TSHFileInfo;
begin
ImageList := SHGetFileInfo(PChar(
sizeof(TSHFileInfo)
if ImageList <>
TreeView_SetImageList(TreeView
end;
// 生成文件夾管理樹
procedure TForm
FullPIDL: PItemIDList; ParentNode: TTreeNode);
var
TreeViewItem: PTreeViewItem;
EnumIDList: IEnumIDList;
PIDLs
NumID: LongWord;
ChildNode: TTreeNode;
Attr: Cardinal;
begin
try
OLECheck(Folder
while EnumIDList
begin
FullItemPIDL := ConcatPIDLs(FullPIDL
TreeViewItem := New(PTreeViewItem);
TreeViewItem
TreeViewItem
TreeViewItem
TreeViewItem
FItemList
ChildNode := TreeView
GetDisplayName(Folder
GetItemIcons(FullItemPIDL
Attr := SFGAO_HASSUBFOLDER or SFGAO_FOLDER;
Folder
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
end;
except
// 你可在此處對異常進行處理
end;
end;
procedure TForm
var
I: Integer;
begin
try
for I :=
begin
DisposePIDL(PTreeViewItem(FItemList[i])
DisposePIDL(PTreeViewItem(FItemList[i])
end;
FItemList
FItemList
except
end;
end;
procedure TForm
var
Folder: IShellFolder;
begin
SetTreeViewImageList;
OLECheck(SHGetDesktopFolder(Folder));
FItemList := TList
FillTreeView(Folder
end;
procedure TForm
var AllowExpansion: Boolean);
var
TVItem: PTreeViewItem;
SHFolder: IShellFolder;
begin
TVItem := PTreeViewItem(Node
if TVItem
OLECheck(TVItem
nil
FillTreeView(SHFolder
Node
TVItem^
end;
end
From:http://tw.wingwit.com/Article/program/Delphi/201311/25086.html