Steve Magness
Steve Magness

Reputation: 895

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

Upvotes: 4

Views: 3867

Answers (2)

Denis Nazarenko
Denis Nazarenko

Reputation: 1

if SelectDirectory('Select a directory', chosenDirectory, chosenDirectory) then

Upvotes: 0

Steve Magness
Steve Magness

Reputation: 895

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;

Upvotes: 3

Related Questions