使用TOpenDialog选择目录

时间:2011-09-14 20:38:45

标签: delphi delphi-2010

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

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

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

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

enter image description here

感谢您的所有答案。

5 个答案:

答案 0 :(得分:64)

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

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

就个人而言,我总是在Vista上使用TFileOpenDialog并在XP上使用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)

答案 1 :(得分:55)

你知道名为FileCtrl.SelectDirectory的两个重载函数产生完全不同的对话框,对吗?

SelectDirectory(s, [], 0);

Screenshot http://privat.rejbrand.se/oldseldir.png

SelectDirectory('Select a directory', s, s, []);

Screenshot http://privat.rejbrand.se/newseldir.png

答案 2 :(得分:6)

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

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

我一直在寻找这样的东西很长一段时间以为我会在这里发布,以便其他人可以从中受益。

这就是Win 7中的样子:

screen capture

//***********************
//** 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;

答案 3 :(得分:6)

请加入

FileCtrl.pas

var
  sDir:String;
begin
  SelectDirectory('Your caption','',sDir);
end;

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

例如:

SelectDirectory('Your caption','C:\',sDir)不允许您选择C:\以外的任何内容,例如D:\E:\等。

所以把它留空是好的。

答案 4 :(得分:2)

如果您使用的是JVCL,则可以使用TJvSelectDirectory。有了这个,您可以通过设置属性在旧样式和新样式之间切换。例如:

Dlg := TJvSelectDirectory.Create(Self);
try
    Dlg.Title := MyTitle;
    Dlg.InitialDir := MyStartDir;
    Dlg.Options := Dlg.Options + [sdAllowCreate, sdPerformCreate];     
    Dlg.ClassicDialog := False;   //switch style
    if Dlg.Execute() then
      NewDir := Dlg.Directory;
finally
    Dlg.Free;
end;