使用TOpenDialog选择目录

ple*_*103 48 delphi delphi-2010

我真的想知道用TOpenDialog选择目录的各种方法,无论是下载新组件还是使用Delphi提供的内容,但最好使用Delphi提供的内容.

在此之前,我一直在使用SelectDirectory命令,但我认为我的程序用户查找指定目录会很困难.

我认为SelectDirectory是"弱"的,因为在搜索你想要的目录时它可能是一个很长的过程.比如说,您想要导航到Application Data目录.在那里航行需要多长时间或多长时间?最后,用户甚至可能无法访问他们想要的目录.

我需要这样的东西,用户可以将目录复制并粘贴到顶部的目录地址栏中.

在此输入图像描述

谢谢你的所有答案.

And*_*and 66

你可以使用TFileOpenDialog(在Vista +上):

with TFileOpenDialog.Create(nil) do
  try
    Options := [fdoPickFolders];
    if Execute then
      ShowMessage(FileName);
  finally
    Free;
  end;
Run Code Online (Sandbox Code Playgroud)

就个人而言,我总是TFileOpenDialog在XP上使用Vista +并使用SelectDirectory(好的!)回退,如下所示:

if Win32MajorVersion >= 6 then
  with TFileOpenDialog.Create(nil) do
    try
      Title := 'Select Directory';
      Options := [fdoPickFolders, fdoPathMustExist, fdoForceFileSystem]; // YMMV
      OkButtonLabel := 'Select';
      DefaultFolder := FDir;
      FileName := FDir;
      if Execute then
        ShowMessage(FileName);
    finally
      Free;
    end
else
  if SelectDirectory('Select Directory', ExtractFileDrive(FDir), FDir,
             [sdNewUI, sdNewFolder]) then
    ShowMessage(FDir)
Run Code Online (Sandbox Code Playgroud)

  • TFileOpenDialog!= TOpenDialog ... TOpenDialog没有这样的选项,FireMonkey中不存在TFileOpenDialog (2认同)

And*_*and 58

你知道调用的两个重载函数FileCtrl.SelectDirectory会产生完全不同的对话框,对吧?

SelectDirectory(s, [], 0);
Run Code Online (Sandbox Code Playgroud)

截图http://privat.rejbrand.se/oldseldir.png

SelectDirectory('Select a directory', s, s, []);
Run Code Online (Sandbox Code Playgroud)

截图http://privat.rejbrand.se/newseldir.png

  • 你真的应该将你的两个答案合二为一,并添加一些关于Vista +和XP之间行为切换的讨论. (7认同)
  • "Välfmapp"肯定胜过"选择目录"+1 (3认同)

小智 7

只是包括

FileCtrl.pas

var
  sDir:String;
begin
  SelectDirectory('Your caption','',sDir);
end;
Run Code Online (Sandbox Code Playgroud)

如果要查看包括桌面在内的所有目录,请将第二个参数留空.如果您将第二个参数设置为任何有效的路径,那么您的对话框将具有该顶级文件夹的路径,并且您无法导航.

例如:

SelectDirectory('Your caption','C:\',sDir)不会让你选择任何超越C:\,像D:\E:\ 等.

所以把它留空是件好事.


Rob*_*ank 6

刚刚发现下面的代码似乎在XP和Vista,Win7中运行良好.它为用户提供了选择目录的UI.它使用TOpenDialog,但为了选择目录,它会发送一些消息来清理外观.

在遭受Windows本身提供的有限功能之后,很高兴能够为我的用户提供熟悉的用户界面,以便他们可以轻松地浏览和选择文件夹.

我一直在寻找类似这样的东西,所以我想在这里发布,以便其他人可以从中受益.

这是Win 7中的样子:

屏幕截图

//***********************
//** Choose a directory **
//**   uses Messages   **
//***********************
  //General usage here:
  //  http://www.delphipages.com/forum/showthread.php?p=185734
  //Need a class to hold a procedure to be called by Dialog.OnShow:
  type TOpenDir = class(TObject)
  public
    Dialog: TOpenDialog;
    procedure HideControls(Sender: TObject);
  end;
  //This procedure hides de combo box of file types...
  procedure TOpenDir.HideControls(Sender: TObject);
  const
    //CDM_HIDECONTROL and CDM_SETCONTROLTEXT values from:
    //  doc.ddart.net/msdn/header/include/commdlg.h.html
    //  CMD_HIDECONTROL = CMD_FIRST + 5 = (WM_USER + 100) + 5;
    //Usage of CDM_HIDECONTROL and CDM_SETCONTROLTEXT here:
    //  msdn.microsoft.com/en-us/library/ms646853%28VS.85%29.aspx
    //  msdn.microsoft.com/en-us/library/ms646855%28VS.85%29.aspx
    CDM_HIDECONTROL =    WM_USER + 100 + 5;
    CDM_SETCONTROLTEXT = WM_USER + 100 + 4;
    //Component IDs from:
    //  msdn.microsoft.com/en-us/library/ms646960%28VS.85%29.aspx#_win32_Open_and_Save_As_Dialog_Box_Customization
    //Translation into exadecimal in dlgs.h:
    //  www.koders.com/c/fidCD2C946367FEE401460B8A91A3DB62F7D9CE3244.aspx
    //
    //File type filter...
    cmb1: integer  = $470; //Combo box with list of file type filters
    stc2: integer  = $441; //Label of the file type
    //File name const...
    cmb13: integer = $47c; //Combo box with name of the current file
    edt1: integer  = $480; //Edit with the name of the current file
    stc3: integer  = $442; //Label of the file name combo
  var H: THandle;
  begin
    H:= GetParent(Dialog.Handle);
    //Hide file types combo...
    SendMessage(H, CDM_HIDECONTROL, cmb1,  0);
    SendMessage(H, CDM_HIDECONTROL, stc2,  0);
    //Hide file name label, edit and combo...
    SendMessage(H, CDM_HIDECONTROL, cmb13, 0);
    SendMessage(H, CDM_HIDECONTROL, edt1,  0);
    SendMessage(H, CDM_HIDECONTROL, stc3,  0);
    //NOTE: How to change label text (the lentgh is not auto):
    //SendMessage(H, CDM_SETCONTROLTEXT, stc3, DWORD(pChar('Hello!')));
  end;
//Call it when you need the user to chose a folder for you...
function GimmeDir(var Dir: string): boolean;
var
  OpenDialog: TOpenDialog;
  OpenDir: TOpenDir;
begin
  //The standard dialog...
  OpenDialog:= TOpenDialog.Create(nil);
  //Objetc that holds the OnShow code to hide controls
  OpenDir:= TOpenDir.create;
  try
    //Conect both components...
    OpenDir.Dialog:= OpenDialog;
    OpenDialog.OnShow:= OpenDir.HideControls;
    //Configure it so only folders are shown (and file without extension!)...
    OpenDialog.FileName:= '*.';
    OpenDialog.Filter:=   '*.';
    OpenDialog.Title:=    'Chose a folder';
    //No need to check file existis!
    OpenDialog.Options:= OpenDialog.Options + [ofNoValidate];
    //Initial folder...
    OpenDialog.InitialDir:= Dir;
    //Ask user...
    if OpenDialog.Execute then begin
      Dir:= ExtractFilePath(OpenDialog.FileName);
      result:= true;
    end else begin
      result:= false;
    end;
  finally
    //Clean up...
    OpenDir.Free;
    OpenDialog.Free;
  end;
end;
Run Code Online (Sandbox Code Playgroud)

  • 我喜欢这个,但是你必须在最后一步选择两次文件夹,这很烦人.除了树中的最后一个目录外,您无法选择任何内容. (2认同)