From: "Mike Lischke" Newsgroups: borland.public.delphi.winapi References: <3bb22d66$1_1@dnews> Subject: Re: SHBrowseForFolder - set initial dir ? Date: Thu, 27 Sep 2001 20:54:40 +0200 Lines: 121 MIME-Version: 1.0 Content-Type: text/plain; charset="Windows-1252" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4522.1200 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200 NNTP-Posting-Host: 62.67.120.56 Message-ID: <3bb375e3$1_1@dnews> X-Trace: dnews 1001616867 62.67.120.56 (27 Sep 2001 11:54:27 -0700) Path: dnews Xref: dnews borland.public.delphi.winapi:147772 Hi Chris, here is one (copy and use) version I wrote some time ago: function BrowseCallbackProc(hwnd: HWND; uMsg: UINT; lParam, lpData: LPARAM): Integer; stdcall; // callback function used in SelectDirectory to set the status text and choose an initial dir var Path: array[0..MAX_PATH] of Char; begin case uMsg of BFFM_INITIALIZED: begin // Initialization has been done, now set our initial directory which is passed in lpData // (and set btw. the status text too). // Note: There's no need to cast lpData to a PChar since the following call needs a // LPARAM parameter anyway. SendMessage(hwnd, BFFM_SETSELECTION, 1, lpData); SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0, lpData); end; BFFM_SELCHANGED: begin // Set the status window to the currently selected path. if SHGetPathFromIDList(Pointer(lParam), Path) then SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0, Integer(@Path)); end; end; Result := 0; end; //------------------------------------------------------------------------------ ---------------------------------------- function SelectDirectory(const Caption, InitialDir: string; const Root: WideString; ShowStatus: Boolean; out Directory: string): Boolean; // Another (overloaded) browse-for-folder function with the ability to select an intial directory // (other SelectDirectory functions are in FileCtrl.pas). // I had to make this overloading unambiguous in its parameter list so I included a flag which // indicates whether or not to show a status text line in the dialog (which will receive the // currently selected path if enabled). var BrowseInfo: TBrowseInfo; Buffer: PChar; RootItemIDList, ItemIDList: PItemIDList; ShellMalloc: IMalloc; IDesktopFolder: IShellFolder; Eaten, Flags: LongWord; Windows: Pointer; Path: string; begin Result := False; Directory := ''; Path := InitialDir; if (Length(Path) > 0) and (Path[Length(Path)] = '\') then Delete(Path, Length(Path), 1); FillChar(BrowseInfo, SizeOf(BrowseInfo), 0); if (ShGetMalloc(ShellMalloc) = S_OK) and (ShellMalloc <> nil) then begin Buffer := ShellMalloc.Alloc(MAX_PATH); try SHGetDesktopFolder(IDesktopFolder); IDesktopFolder.ParseDisplayName(Application.Handle, nil, PWideChar(Root), Eaten, RootItemIDList, Flags); with BrowseInfo do begin hwndOwner := Application.Handle; pidlRoot := RootItemIDList; pszDisplayName := Buffer; lpszTitle := PChar(Caption); ulFlags := BIF_RETURNONLYFSDIRS; if ShowStatus then ulFlags := ulFlags or BIF_STATUSTEXT; lParam := Integer(PChar(Path)); lpfn := BrowseCallbackProc; end; // Make the browser dialog modal. Windows := DisableTaskWindows(Application.Handle); try ItemIDList := ShBrowseForFolder(BrowseInfo); finally EnableTaskWindows(Windows); end; Result := ItemIDList <> nil; if Result then begin ShGetPathFromIDList(ItemIDList, Buffer); ShellMalloc.Free(ItemIDList); Directory := Buffer; end; finally ShellMalloc.Free(Buffer); end; end; end; Ciao, Mike -- Homepage: http://www.lischke-online.de Delphi Unicode Center: http://www.delphi-unicode.net GraphicEx: http://www.lischke-online.de/Graphics.html Virtual Treeview: http://www.lischke-online.de/VirtualTreeview.html Joint Endavour of Delphi Innovators (JEDI): www.delphi-jedi.org