delphi中如何opendialog1.FileName;只能打开文件,如何打开目录获得目录路径
3个回答
展开全部
在uses中加入 FileCtrl;
function SelectDirectory(const Caption: string; const Root: WideString; out Directory: string): Boolean;
SelectDirectory弹出对话框让用户选择目录,不改变目录的键值,在linux和windows中格式有所不同,
caption标题是长字符串
const Root指定浏览的根目录
out Directory返回所选目录
ShowHidden标识所选目录是否显示隐藏子目录
例:
var
Save_Cursor: TCursor;
s : string;
begin
Save_Cursor := Screen.Cursor;
Screen.Cursor := crHourglass;
try
if SelectDirectory('请选择文件夹:','',s) then
begin
edtFileName.Text:=s;
end;
finally
Screen.Cursor := Save_Cursor;
end;
end;
function SelectDirectory(const Caption: string; const Root: WideString; out Directory: string): Boolean;
SelectDirectory弹出对话框让用户选择目录,不改变目录的键值,在linux和windows中格式有所不同,
caption标题是长字符串
const Root指定浏览的根目录
out Directory返回所选目录
ShowHidden标识所选目录是否显示隐藏子目录
例:
var
Save_Cursor: TCursor;
s : string;
begin
Save_Cursor := Screen.Cursor;
Screen.Cursor := crHourglass;
try
if SelectDirectory('请选择文件夹:','',s) then
begin
edtFileName.Text:=s;
end;
finally
Screen.Cursor := Save_Cursor;
end;
end;
展开全部
有很多,实在是没有标准答案。
可以用FileCtrl单元中的SelectDirectory()函数,不过我觉得太丑了。
给你个我自己用的
function __BrowseProc(hWnd: HWND; uMsg: UINT; lParam, lpData: LPARAM): Integer stdcall;
begin
Result := 0;
if uMsg = BFFM_INITIALIZED then
begin
if PWideChar(lpData) = '' then
Exit;
// 当使用BIF_NEWDIALOGSTYLE风格时,选择默认目录,TreeView不动自动滚动 // hWnd.SubWnd('SHBrowseForFolder ShellNameSpace Control').SubWnd('SysTreeView32')
SendMessage(hWnd, BFFM_SETSELECTION, Integer(True), Integer(lpData));
end;
end;
function SelectDirectory(var Directory: string; Handle: HWND;
const Caption: string; const RootDir: WideString;
const DefaultDir: string): Boolean;
var
WindowList : Pointer;
BrowseInfo: TBrowseInfo;
RootItemIDList, ItemIDList: PItemIDList;
IDesktopFolder: IShellFolder;
Eaten, Flags: DWORD;
ShellMalloc : IMalloc;
Buffer : PChar;
begin
Result := False;
Directory := '';
FillChar(BrowseInfo, SizeOf(BrowseInfo), 0);
if (ShGetMalloc(ShellMalloc) = S_OK) and (ShellMalloc <> nil) then
begin
Buffer := ShellMalloc.Alloc(MAX_PATH*SizeOf(Char));
ZeroMemory(Buffer, MAX_PATH*SizeOf(Char));
try
with BrowseInfo do
begin
RootItemIDList := nil;
if RootDir <> '' then
begin
SHGetDesktopFolder(IDesktopFolder);
IDesktopFolder.ParseDisplayName(Handle, nil, PWideChar(RootDir), Eaten, RootItemIDList, Flags);
end;
HwndOwner := Handle;
lpszTitle := PWideChar(Caption);
ulFlags := BIF_RETURNONLYFSDIRS; // BIF_NEWDIALOGSTYLE
pidlRoot := RootItemIDList;
pszDisplayName := Buffer;
lpfn := @__BrowseProc;
lParam := INT_PTR(PWideChar(DefaultDir));
end;
WindowList := DisableTaskWindows(0);
try
ItemIDList := ShBrowseForFolder(BrowseInfo);
finally
EnableTaskWindows(WindowList);
end;
if Assigned(ItemIDList) then
begin
ShGetPathFromIDList(ItemIDList, Buffer);
ShellMalloc.Free(ItemIDList);
Directory := Buffer;
if Directory[Length(Directory)] <> '\' then
Directory := Directory + '\';
Result := True; end;
finally
ShellMalloc.Free(Buffer);
ShellMalloc := nil;
end;
end;
end;
可以用FileCtrl单元中的SelectDirectory()函数,不过我觉得太丑了。
给你个我自己用的
function __BrowseProc(hWnd: HWND; uMsg: UINT; lParam, lpData: LPARAM): Integer stdcall;
begin
Result := 0;
if uMsg = BFFM_INITIALIZED then
begin
if PWideChar(lpData) = '' then
Exit;
// 当使用BIF_NEWDIALOGSTYLE风格时,选择默认目录,TreeView不动自动滚动 // hWnd.SubWnd('SHBrowseForFolder ShellNameSpace Control').SubWnd('SysTreeView32')
SendMessage(hWnd, BFFM_SETSELECTION, Integer(True), Integer(lpData));
end;
end;
function SelectDirectory(var Directory: string; Handle: HWND;
const Caption: string; const RootDir: WideString;
const DefaultDir: string): Boolean;
var
WindowList : Pointer;
BrowseInfo: TBrowseInfo;
RootItemIDList, ItemIDList: PItemIDList;
IDesktopFolder: IShellFolder;
Eaten, Flags: DWORD;
ShellMalloc : IMalloc;
Buffer : PChar;
begin
Result := False;
Directory := '';
FillChar(BrowseInfo, SizeOf(BrowseInfo), 0);
if (ShGetMalloc(ShellMalloc) = S_OK) and (ShellMalloc <> nil) then
begin
Buffer := ShellMalloc.Alloc(MAX_PATH*SizeOf(Char));
ZeroMemory(Buffer, MAX_PATH*SizeOf(Char));
try
with BrowseInfo do
begin
RootItemIDList := nil;
if RootDir <> '' then
begin
SHGetDesktopFolder(IDesktopFolder);
IDesktopFolder.ParseDisplayName(Handle, nil, PWideChar(RootDir), Eaten, RootItemIDList, Flags);
end;
HwndOwner := Handle;
lpszTitle := PWideChar(Caption);
ulFlags := BIF_RETURNONLYFSDIRS; // BIF_NEWDIALOGSTYLE
pidlRoot := RootItemIDList;
pszDisplayName := Buffer;
lpfn := @__BrowseProc;
lParam := INT_PTR(PWideChar(DefaultDir));
end;
WindowList := DisableTaskWindows(0);
try
ItemIDList := ShBrowseForFolder(BrowseInfo);
finally
EnableTaskWindows(WindowList);
end;
if Assigned(ItemIDList) then
begin
ShGetPathFromIDList(ItemIDList, Buffer);
ShellMalloc.Free(ItemIDList);
Directory := Buffer;
if Directory[Length(Directory)] <> '\' then
Directory := Directory + '\';
Result := True; end;
finally
ShellMalloc.Free(Buffer);
ShellMalloc := nil;
end;
end;
end;
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
里面有个设置项,设置后就会以获得目录型式运行。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询