Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TOpenDialog Select Folder? 1

Status
Not open for further replies.

ug505

Programmer
Jan 2, 2010
52
US
Is there a way to select a folder using the open dialog so that I may use the folder path that users select. I don't want any components to download, I just want to use what comes with Delphi.

I have Delphi 7.
 
there’s a few ways all of which are easily found using Google.

Code:
procedure TForm1.Button1Click(Sender: TObject);
var 
  TitleName : string; 
  lpItemID : PItemIDList; 
  BrowseInfo : TBrowseInfo; 
  DisplayName : array[0..MAX_PATH] of char; 
  TempPath : array[0..MAX_PATH] of char; 
begin 
  FillChar(BrowseInfo, sizeof(TBrowseInfo), #0); 
  BrowseInfo.hwndOwner := Handle; 
  BrowseInfo.pszDisplayName := @DisplayName; 
  TitleName := 'Please specify a directory'; 
  BrowseInfo.lpszTitle := PChar(TitleName); 
  BrowseInfo.ulFlags := BIF_RETURNONLYFSDIRS; 
  lpItemID := SHBrowseForFolder(BrowseInfo); 
  if lpItemId <> nil then begin 
    SHGetPathFromIDList(lpItemID, TempPath); 
    ShowMessage(TempPath); 
    GlobalFreePtr(lpItemID); 
  end; 
end;

Aaron
 
Tried Google, all I tried failed. The one you just gave me gave me errors.
 
SHBrowseForFolder has so many good options that anything reusable to a significant degree involving it will be a component simply because that's the easiest. I posted a component implementation here: faq102-7233

That said, there are stripped down implementations that people are using, too. Here's the previous version of that code in the FAQ:

Code:
unit d3_bdir;

  { revision 3, Shell BrowseDirectory }

  interface
    function BrowseDirectory(handle: cardinal; Title, startdir: string;
                           var outdir: string): Boolean;

 implementation
   uses windows, shlobj;

   function BD_Callback(wnd: hwnd; umsg: uint;
                         lparam, lpdata: lparam): integer stdcall;
      { delphi.about.com, centers browse directory dialog }
      var
        wa, rect: TRect;
        dialogPT: TPoint;
      begin
        if uMsg = BFFM_INITIALIZED then
          begin
            wa.Top := 0; wa.Left := 0;
            Wa.Right := GetSystemMetrics(SM_CXSCREEN);
            Wa.Bottom := GetSystemMetrics(SM_CYSCREEN);
            GetWindowRect(Wnd, Rect);
            dialogPT.X := ((wa.Right - wa.Left) div 2) -
                          ((rect.Right - rect.Left) div 2);
            dialogPT.Y := ((wa.Bottom - wa.Top) div 2) -
                          ((rect.Bottom - rect.Top) div 2);
            MoveWindow(Wnd, dialogPT.X, dialogPT.Y, rect.Right - Rect.Left,
                       Rect.Bottom - Rect.Top, True);
            { set browse directory }
            SendMessage(wnd, BFFM_SETSELECTIONA, Longint(true), lpdata);
          end;
        Result := 0;
      end;

  function BrowseDirectory(handle: cardinal; Title, startdir: string;
                           var outdir: string): Boolean;
    { delphi.about.com, browse directory dialog }

    var
      lpItemID : PItemIDList;
      BrowseInfo : TBrowseInfo;
      DisplayName : array[0..MAX_PATH] of char;
      TempPath : array[0..MAX_PATH] of char;
      flag: integer;
    begin
      Flag := BIF_RETURNONLYFSDIRS;
      FillChar(BrowseInfo, sizeof(TBrowseInfo), #0);
      with BrowseInfo do begin
        hwndOwner := Handle;
        pszDisplayName := @DisplayName;
        lpszTitle := PChar(Title);
        lpfn := BD_Callback;
        lparam := Longint(PChar(StartDir));
        ulFlags := Flag;
      end;
      lpItemID := SHBrowseForFolder(BrowseInfo);
      if lpItemId <> nil then
        begin
          SHGetPathFromIDList(lpItemID, TempPath);
          outdir := temppath;
          Result := true;
          GlobalFreePtr(lpItemID);
        end
      else
        Result := false;
    end;

end.

Hope it helps.

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top