function SelectDirectory(const Caption: string;out Directory: string): Boolean;<br>var<br> WindowList: Pointer;<br> BrowseInfo: TBrowseInfo;<br> Buffer: PChar;<br> RootItemIDList, ItemIDList: PItemIDList;<br> ShellMalloc: IMalloc;<br> IDesktopFolder: IShellFolder;<br> Eaten, Flags: LongWord;<br>begin<br> Result := False;<br> Directory := '';<br> FillChar(BrowseInfo, SizeOf(BrowseInfo), 0);<br> if (ShGetMalloc(ShellMalloc) = S_OK) and (ShellMalloc <> nil) then<br> begin<br> Buffer := ShellMalloc.Alloc(MAX_PATH);<br> try<br> RootItemIDList := nil;<br> if SHGetSpecialFolderLocation(Application.Handle, CSIDL_NETWORK, RootItemIDList) <> S_OK then<br> Abort;<br> with BrowseInfo do<br> begin<br> hwndOwner := Application.Handle;<br> pidlRoot := RootItemIDList;<br> pszDisplayName := Buffer;<br> lpszTitle := PChar(Caption);<br> ulFlags := BIF_RETURNONLYFSDIRS;<br> end;<br> WindowList := DisableTaskWindows(0);<br> try<br> ItemIDList := ShBrowseForFolder(BrowseInfo);<br> finally<br> EnableTaskWindows(WindowList);<br> end;<br> Result := ItemIDList <> nil;<br> if Result then<br> begin<br> ShGetPathFromIDList(ItemIDList, Buffer);<br> ShellMalloc.Free(ItemIDList);<br> Directory := Buffer;<br> end;<br> finally<br> ShellMalloc.Free(Buffer);<br> end;<br> end;<br>end;<br>从 Delphi 的 SelectDirectory 改了一点,用 SHBrowseFolder 只能选目录,计算机和打印<br>机等,不能选文件的。