4

I'm using FireMonkey and want the user to select a directory using the interface supplied by a TFileOpenDialog (I find the SelectDirectory interface outdated at best - yes, even with the sdNewUI option).

TFileOpenDialog with [fdoPickFolders] option

Firstly, Is it bad practice to include the VCL.Dialogs unit (to use a TFileOpenDialog) in a FireMonkey application?

Secondly, this is still only possible with Windows Vista and above. Is this the correct way to check for a compatible Windows versions?

{IFDEF WIN32 or WIN64}
  if Win32MajorVersion >= 6 then
    // Create TOpenFileDialog with fdoPickFolders option
Steve Magness
  • 863
  • 14
  • 25
  • Your test for Vista up is fine. Personally I would not bother with `TFileOpenDialog` and use `IFileDialog` directly. – David Heffernan Jun 12 '12 at 14:12
  • Ummm... How would I do this with fdoPickFolders option, isn't IFileDialog just the interface for a standard TOpenDialog/TSaveDialog, or am I missing something? – Steve Magness Jun 12 '12 at 14:51
  • 1
    No, `IFileDialog` is the file dialog for Vista, in all its forms. Everything else is legacy. Call `IFileDialog.SetOptions` passing `FOS_PICKFOLDERS`. – David Heffernan Jun 12 '12 at 15:06

2 Answers2

2

For future reference, use of IFileDialog to create a Windows Vista and above folder dialog:

uses
  ShlObj, ActiveX;

...

var
  FolderDialog : IFileDialog;
  hr: HRESULT;
  IResult: IShellItem;
  FileName: PChar;
  Settings: DWORD;
begin
  if Win32MajorVersion >= 6 then
    begin
      hr := CoCreateInstance(CLSID_FileOpenDialog,
                   nil,
                   CLSCTX_INPROC_SERVER,
                   IFileDialog,
                   FolderDialog);

      if hr = S_OK then
        begin
          FolderDialog.GetOptions(Settings);
          FolderDialog.SetOptions(Settings or FOS_PICKFOLDERS);
          FolderDialog.GetOptions(Settings);
          FolderDialog.SetOptions(Settings or FOS_FORCEFILESYSTEM);
          FolderDialog.SetOkButtonLabel(PChar('Select'));
          FolderDialog.SetTitle(PChar('Select a Directory'));

          hr := FolderDialog.Show(Handle);
          if hr = S_OK then
            begin
              hr := FolderDialog.GetResult(IResult);

              if hr = S_OK then
                begin
                  IResult.GetDisplayName(SIGDN_FILESYSPATH,FileName);
                  ConfigPathEdit.Text := FileName;
                end;
            end;
        end;
    end;
end;
Steve Magness
  • 863
  • 14
  • 25
0
if SelectDirectory('Select a directory', chosenDirectory, chosenDirectory) then
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • 9
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Suraj Rao Apr 18 '19 at 09:33