用以下方法可以展开结构化文件的结构到 TTreeView
const
c_OpenStorage = STGM_READWRITE or STGM_DIRECT or STGM_SHARE_EXCLUSIVE;
function TForm1.ProcessFile(AFileName: WideString; tv: TTreeView): Boolean;
var
Hr: HResult;
Root: IStorage;
begin
Hr := StgOpenStorage(PWideChar(AFileName), nil,c_OpenStorage,nil,0,Root);
Result := SUCCEEDED(Hr);
if not Result then begin Exit;
tv.Items.Clear;
tv.Items.Add(nil,ExtractFileName(AFileName));
EnumInStorage(Root,tv,tv.Items[0]);
tv.Items[0].Expand(False);
end;
procedure TForm1.EnumInStorage(AStor: IStorage; tv: TTreeView; tn: TTreeNode);
var
Hr: HResult;
Enum: IEnumSTATSTG;
SubNode: TTreeNode;
StatStg: TStatStg;
SubStor: IStorage;
HrSubStor: HResult;
NumFetched: Integer;
begin
Hr := AStor.EnumElements(0,nil,0,Enum);
if not Succeeded(Hr) then Exit;
repeat
Hr := Enum.Next(1,StatStg,@NumFetched);
if(Hr <> S_OK) then continue;
case StatStg.dwType of
STGTY_STORAGE: begin //子存储
SubNode := tv.Items.AddChild(tn,'[' + String(StatStg.pwcsName) + ']');
HrSubStor := AStor.OpenStorage(StatStg.pwcsName,nil,c_OpenStorage,
nil,0,SubStor);
if(SUCCEEDED(HrSubStor)) then EnumInStorage(SubStor,tv,SubNode);
end;
STGTY_STREAM: tv.Items.AddChild(tn,StatStg.pwcsName); //存储流
end;
until (Hr <> S_OK);
end;