function SelectDirCB(Wnd: HWND; uMsg: UINT; lParam, lpData: LPARAM): Integer stdcall;<br>begin<br> if (uMsg = BFFM_INITIALIZED) and (lpData <> 0) then<br> SendMessage(Wnd, BFFM_SETSELECTION, Integer(True), lpdata);<br> result := 0;<br>end;<br><br>function SelectDirectory(const Caption: string; const Root: WideString;<br> var Directory: string): Boolean;<br>var<br> WindowList: Pointer;<br> BrowseInfo: TBrowseInfo;<br> Buffer: PChar;<br> OldErrorMode: Cardinal;<br> RootItemIDList, ItemIDList: PItemIDList;<br> ShellMalloc: IMalloc;<br> IDesktopFolder: IShellFolder;<br> Eaten, Flags: LongWord;<br>begin<br> Result := False;<br> if not DirectoryExists(Directory) then<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 Root <> '' then<br> begin<br> SHGetDesktopFolder(IDesktopFolder);<br> IDesktopFolder.ParseDisplayName(Application.Handle, nil,<br> POleStr(Root), Eaten, RootItemIDList, Flags);<br> end;<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> if Directory <> '' then<br> begin<br> lpfn := SelectDirCB;<br> lParam := Integer(PChar(Directory));<br> end;<br> end;<br> WindowList := DisableTaskWindows(0);<br> OldErrorMode := SetErrorMode(SEM_FAILCRITICALERRORS);<br> try<br> ItemIDList := ShBrowseForFolder(BrowseInfo);<br> finally<br> SetErrorMode(OldErrorMode);<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的FileCtrl.pas中取出来的.