unit Unit1; <br>interface <br>uses <br> ……shlobj,ActiveX; <br> ……<br><br>var<br> Form1: TForm1; <br> Path: string; //起始路径<br><br>implementation <br><br>{$R *.DFM} <br><br>function BrowseCallbackProc(hwnd: HWND;uMsg: UINT;lParam: Cardinal;lpData: Cardinal): integer; stdcall; <br>begin <br> if uMsg=BFFM_INITIALIZED then <br> result :=SendMessage(Hwnd,BFFM_SETSELECTION,Ord(TRUE),Longint(PChar(Path))) <br> else <br> result :=1 <br>end; <br><br>function SelDir(const Caption: string; const Root: WideString; 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 Root <> '' then begin <br> SHGetDesktopFolder(IDesktopFolder); <br> IDesktopFolder.ParseDisplayName(Application.Handle, nil, POleStr(Root), Eaten, RootItemIDList, Flags); <br> end; <br> with BrowseInfo do begin <br> hwndOwner := Application.Handle; <br> pidlRoot := RootItemIDList; <br> pszDisplayName := Buffer; <br> lpszTitle := PChar(Caption); <br> ulFlags := BIF_RETURNONLYFSDIRS; <br> lpfn :=@BrowseCallbackProc; <br> lParam :=BFFM_INITIALIZED; <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 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><br>procedure TForm1.SpeedButton1Click(Sender: TObject); <br>var <br> Path1: string; <br>begin <br> Path :=Edit1.Text; <br> SelDir('SelectDirectory Sample','',Path1); <br> Edit1.Text :=Path1 <br>end; <br><br>end. <br>