Link to home
Start Free TrialLog in
Avatar of QC20N
QC20NFlag for Denmark

asked on

Opendialog only for Folder

Hi, I'm looking for a OpenDialog but only for folders.

I need to have the user of my application to open all files from a folder with a specific extension.

How do I do that? I know the OpenDialog. But that is only for files.
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

You can download and install the Jedi Pack (http://www.delphi-jedi.org/) which holds tenth of useful components. There is  TJvDirectoryEdit also which does exactly what you're looking for.

Cheers
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Use this function

 
uses
  ShlObj;

function BrowseDialogCallBack(Wnd: HWnd; uMsg: UINT; lParam, lpData: LParam): Integer stdcall;
var
  WA, R: TRect;
  PT: TPoint;
begin
  if uMsg = BFFM_INITIALIZED then
  begin
    WA := Screen.WorkAreaRect;
    GetWindowRect(Wnd, R);
    PT.X := ((WA.Right - WA.Left) div 2) - ((R.Right - R.Left) div 2);
    PT.Y := ((WA.Bottom - WA.Top) div 2) - ((R.Bottom - R.Top) div 2);
    MoveWindow(Wnd, PT.X, PT.Y, R.Right - R.Left, R.Bottom - R.Top, True);

    SendMessage(Wnd, BFFM_SETSELECTION, Ord(True), Integer(lpData));
  end;
  Result := 0;
end;

function BrowseDirectoryDialog(const ATitle: string; const AFlag: Integer;
  var Dir: string): Boolean;
var
  lpItemID: PItemIDList;
  BrowseInfo: TBrowseInfo;
  DisplayName, TempPath: array[0..MAX_PATH] of Char;
begin
  FillChar(BrowseInfo, SizeOf(TBrowseInfo), #0);
  with BrowseInfo do
  begin
    hwndOwner := Application.Handle;
    pszDisplayName := @DisplayName;
    lpszTitle := PChar(ATitle);
    ulFlags := AFlag;
    lpfn := BrowseDialogCallBack;
    lParam := Integer(PChar(Dir));
  end;

  lpItemID := ShlObj.SHBrowseForFolder(BrowseInfo);
  Result := lpItemId <> nil;
  if Result then
  begin
    ShlObj.SHGetPathFromIDList(lpItemID, TempPath);
    Dir := TempPath;
    GlobalFreePtr(lpItemID);
  end;
end;

Open in new window


Call it this way

var
  Folder: string;
begin
  Folder := 'c:\';  //initial folder
  if BrowseDirectoryDialog('Select Folder', BIF_RETURNONLYFSDIRS, Folder) then
  begin
      //your code
  end;  
end;
Excuse me, ewangoya, but why you suggest to use something else than the built in function? I ask this because I really don't uderstand where is the advantage using such a function as that you have suggested...