delphi如何调用window的folderBrowserDialog函数,选择文件目录.

解决方案 »

  1.   

    给个C#的代码吧
    System.Windows.Forms.FolderBrowserDialog  dlg = new FolderBrowserDialog();
    if (dlg.ShowDialog() == DialogResult.OK)
    {
    MessageBox.Show(dlg.SelectedPath);

    }
      

  2.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants,shlobj, Classes, Graphics, Controls, Forms,
      Dialogs;type
      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    function SelectDirectory(handle:hwnd;const Caption: string; const Root: WideString;out Directory: string): Boolean;
    var
      lpbi:_browseinfo;
      buf:array [0..MAX_PATH] of char;
      id:ishellfolder;
      eaten,att:cardinal;
      rt:pitemidlist;
      initdir:pwidechar;
    begin
      result:=false;
      lpbi.hwndOwner:=handle;
      lpbi.lpfn:=nil;
      lpbi.lpszTitle:=pchar(caption);
      lpbi.ulFlags:=BIF_RETURNONLYFSDIRS+BIF_EDITBOX;
      SHGetDesktopFolder(id);
      initdir:=pwchar(root);
      id.ParseDisplayName(0,nil,initdir,eaten,rt,att);
      lpbi.pidlRoot:=rt;
      getmem(lpbi.pszDisplayName,MAX_PATH);
      try
       result:=shgetpathfromidlist(shbrowseforfolder(lpbi),buf);
      except
       freemem(lpbi.pszDisplayName);
      end;
      if result then
      begin
       directory:=buf;
       if length(directory)<>3 then directory:=directory+'\';
      end;
    end;procedure TForm1.FormCreate(Sender: TObject);
    var dir:string;
    begin
    if selectdirectory(handle,'请选择文件夹','',dir) then showmessage(dir);end;end.