Search code examples
delphidelphi-xe2firemonkey

TFileOpenDialog in FireMonkey Application


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

Solution

  • 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;