方法一
uses FileCtrl;
var
szPath: string ;
if SelectDirectory( ' 选个目录看看 ' , '' ,szPath) then
Edit1.Text : = szPath;
方法二
Delphi 显示选择文件夹对话框 (有新建按钮) 附实例
uses ShlObj
选择文件夹对话框。
入口参数:
Handle:调用者句柄
Caption:对话框标题
InitFolder:初始文件夹;
SelectedFolder:返回选择结果
返回值:
如果用户选择了路径并确定,返回True。并由SelectedFolder返回选择结果。否则返回False}
function SelectFolderDialog( const Handle:integer; const Caption: string ;
const InitFolder: string ; var SelectedFolder: string ):boolean;
var
BInfo: _browseinfoA;
Buffer: array [ 0 ..MAX_PATH] of Char;
ID: IShellFolder;
Eaten, Attribute: Cardinal;
ItemID: PItemidlist;
begin
with BInfo do
begin
HwndOwner : = Handle;
lpfn : = nil ;
lpszTitle : = Pchar(Caption);
ulFlags : = BIF_RETURNONLYFSDIRS + BIF_NEWDIALOGSTYLE;
SHGetDesktopFolder(ID);
ID.ParseDisplayName( 0 , nil , ' / ' ,Eaten,ItemID,Attribute);
pidlRoot : = ItemID;
GetMem(pszDisplayName, MAX_PATH);
end ; FreeMem(pszDisplayName);
if SHGetPathFromIDList(SHBrowseForFolder(BInfo), Buffer) then
begin
SelectedFolder : = Buffer;
if Length(SelectedFolder) <> 3 then
SelectedFolder : = SelectedFolder;
result : = True;
end
else begin
SelectedFolder : = '' ;
result : = False;
end ;
end ;
//=====================================================
调用方法:
procedure TForm1.Button1Click(Sender: TObject);
var
NewDir: string ;
begin
if SelectFolderDialog(Handle, ' 选择 ' , ' C:/ ' ,NewDir) then
label1.Caption: = NewDir;
end ;