uses FileCtrl;const SELDIRHELP = 1000; procedure TForm1.Button1Click(Sender: TObject); var Dir: string; begin Dir := 'C:\MYDIR'; if SelectDirectory(Dir, [sdAllowCreate, sdPerformCreate, sdPrompt],SELDIRHELP) then Label1.Caption := Dir; end;见帮助文件!!!hehe
将 Dir := 'C:\MYDIR'; 改成 Dir := '\\blue\mydir';试试
这是需要使用ShBrowserFolder + CallBack实现的,Delphi实现的是设定Root,不是初始化选定目录,你可以采取下面的方法来设定初始目录: 下面的例子是正确的,但是为什么在Callback里面,必须采用全局变量,只要采用局部变量就会出现错误?我始终无法理解,难道是内存分配的原因?只要在Callback里面分配内存,就会出现错误!不过那个Longint(pchar(path))可以用一个integer(pchar('D:\TEMP'))常量来代替,却又是正确的!真的很奇怪。我用HeapAlloc来分配内存也无法达到全局变量的效果。 unit Unit1; interface uses shlobj,ActiveX; var Form1: TForm1; Path: string; //起始路径implementation {$R *.DFM} function BrowseCallbackProc(hwnd: HWND;uMsg: UINT;lParam: Cardinal;lpData: Cardinal): integer; stdcall; begin if uMsg=BFFM_INITIALIZED then result :=SendMessage(Hwnd,BFFM_SETSELECTION,Ord(TRUE),Longint(PChar(Path))) else result :=1 end; function SelDir(const Caption: string; const Root: WideString; out Directory: string): Boolean; var WindowList: Pointer; BrowseInfo: TBrowseInfo; Buffer: PChar; RootItemIDList, ItemIDList: PItemIDList; ShellMalloc: IMalloc; IDesktopFolder: IShellFolder; Eaten, Flags: LongWord; begin Result := False; Directory := ''; FillChar(BrowseInfo, SizeOf(BrowseInfo), 0); if (ShGetMalloc(ShellMalloc) = S_OK) and (ShellMalloc <> nil) then begin Buffer := ShellMalloc.Alloc(MAX_PATH); try RootItemIDList := nil; if Root <> '' then begin SHGetDesktopFolder(IDesktopFolder); IDesktopFolder.ParseDisplayName(Application.Handle, nil, POleStr(Root), Eaten, RootItemIDList, Flags); end; with BrowseInfo do begin hwndOwner := Application.Handle; pidlRoot := RootItemIDList; pszDisplayName := Buffer; lpszTitle := PChar(Caption); ulFlags := BIF_RETURNONLYFSDIRS; lpfn :=@BrowseCallbackProc; lParam :=BFFM_INITIALIZED; end; WindowList := DisableTaskWindows(0); try ItemIDList := ShBrowseForFolder(BrowseInfo); finally EnableTaskWindows(WindowList); end; Result := ItemIDList <> nil; if Result then begin ShGetPathFromIDList(ItemIDList, Buffer); ShellMalloc.Free(ItemIDList); Directory := Buffer; end; finally ShellMalloc.Free(Buffer); end; end; end; procedure TForm1.SpeedButton1Click(Sender: TObject); var Path1: string; begin Path :=Edit1.Text; SelDir('SelectDirectory Sample','d:\temp',Path1); Edit1.Text :=Path1 end; end.
一个改进的SelectDirectoryEx: uses ShlObj, ActiveX;{*****************************************************} { The SelectDirectoryEx function like SelectDirectory } { But you can specify the Init Dir } { hOwn:Parent Window Handle } { Path:In and Out,In-->Init Dir } { Caption:Hint text } { Root:Root Dir } { uFlag:Which Style you want to use,like } { BIF_RETURNONLYFSDIRS or BIF_VALIDATE } { Please see Win32SDK for more detial } {*****************************************************}function SelectDirectoryEx(hOwn: HWND; var Path: string; Caption, Root: string; uFlag: DWORD = $25): Boolean; const BIF_NEWDIALOGSTYLE = $0040; var BrowseInfo: TBrowseInfo; Buffer: PChar; RootItemIDList, ItemIDList: PItemIDList; ShellMalloc: IMalloc; IDesktopFolder: IShellFolder; Dummy: LongWord; function BrowseCallbackProc(hwnd: HWND; uMsg: UINT; lParam: Cardinal; lpData: Cardinal): integer; stdcall; var PathName: array[0..MAX_PATH] of char; begin case uMsg of BFFM_INITIALIZED: SendMessage(Hwnd, BFFM_SETSELECTION, Ord(True), Integer(lpData)); BFFM_SELCHANGED: begin SHGetPathFromIDList(PItemIDList(lParam), @PathName); SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0, LongInt(PChar(@PathName))); end; end; Result := 0; end;begin Result := False; FillChar(BrowseInfo, SizeOf(BrowseInfo), 0); if (ShGetMalloc(ShellMalloc) = S_OK) and (ShellMalloc <> nil) then begin Buffer := ShellMalloc.Alloc(MAX_PATH); try RootItemIDList := nil; if Root <> '' then begin SHGetDesktopFolder(IDesktopFolder); IDesktopFolder.ParseDisplayName(hOwn, nil, POleStr(WideString(Root)), Dummy, RootItemIDList, Dummy); end; with BrowseInfo do begin hwndOwner := hOwn; pidlRoot := RootItemIDList; pszDisplayName := Buffer; lpszTitle := PChar(Caption); ulFlags := uFlag; lpfn := @BrowseCallbackProc; lParam := Integer(Pchar(Path)); end; ItemIDList := ShBrowseForFolder(BrowseInfo); Result := ItemIDList <> nil; if Result then begin ShGetPathFromIDList(ItemIDList, Buffer); ShellMalloc.Free(ItemIDList); Path := StrPas(Buffer); end; finally ShellMalloc.Free(Buffer); end; end; end; procedure TForm1.SpeedButton1Click(Sender: TObject); var Path: string; begin Path := 'C:\WinNT'; if SelectDirectoryEx(Handle, Path, 'Select Directory Sample', 'C:\') then ShowMessage(Path); end;
to Kingron(单身走我路……) 你这个连网上邻居都访问不了,如初始目录设成\\192.168.0.100\得话,不行
SELDIRHELP = 1000;
procedure TForm1.Button1Click(Sender: TObject);
var
Dir: string;
begin
Dir := 'C:\MYDIR';
if SelectDirectory(Dir, [sdAllowCreate, sdPerformCreate, sdPrompt],SELDIRHELP) then
Label1.Caption := Dir;
end;见帮助文件!!!hehe
Dir := 'C:\MYDIR';
改成
Dir := '\\blue\mydir';试试
下面的例子是正确的,但是为什么在Callback里面,必须采用全局变量,只要采用局部变量就会出现错误?我始终无法理解,难道是内存分配的原因?只要在Callback里面分配内存,就会出现错误!不过那个Longint(pchar(path))可以用一个integer(pchar('D:\TEMP'))常量来代替,却又是正确的!真的很奇怪。我用HeapAlloc来分配内存也无法达到全局变量的效果。
unit Unit1;
interface
uses
shlobj,ActiveX;
var
Form1: TForm1;
Path: string; //起始路径implementation {$R *.DFM} function BrowseCallbackProc(hwnd: HWND;uMsg: UINT;lParam: Cardinal;lpData: Cardinal): integer; stdcall;
begin
if uMsg=BFFM_INITIALIZED then
result :=SendMessage(Hwnd,BFFM_SETSELECTION,Ord(TRUE),Longint(PChar(Path)))
else
result :=1
end; function SelDir(const Caption: string; const Root: WideString; out Directory: string): Boolean;
var
WindowList: Pointer;
BrowseInfo: TBrowseInfo;
Buffer: PChar;
RootItemIDList, ItemIDList: PItemIDList;
ShellMalloc: IMalloc;
IDesktopFolder: IShellFolder;
Eaten, Flags: LongWord;
begin
Result := False;
Directory := '';
FillChar(BrowseInfo, SizeOf(BrowseInfo), 0);
if (ShGetMalloc(ShellMalloc) = S_OK) and (ShellMalloc <> nil) then
begin
Buffer := ShellMalloc.Alloc(MAX_PATH);
try
RootItemIDList := nil;
if Root <> '' then begin
SHGetDesktopFolder(IDesktopFolder);
IDesktopFolder.ParseDisplayName(Application.Handle, nil, POleStr(Root), Eaten, RootItemIDList, Flags);
end;
with BrowseInfo do begin
hwndOwner := Application.Handle;
pidlRoot := RootItemIDList;
pszDisplayName := Buffer;
lpszTitle := PChar(Caption);
ulFlags := BIF_RETURNONLYFSDIRS;
lpfn :=@BrowseCallbackProc;
lParam :=BFFM_INITIALIZED;
end;
WindowList := DisableTaskWindows(0);
try
ItemIDList := ShBrowseForFolder(BrowseInfo);
finally
EnableTaskWindows(WindowList);
end;
Result := ItemIDList <> nil;
if Result then begin
ShGetPathFromIDList(ItemIDList, Buffer);
ShellMalloc.Free(ItemIDList);
Directory := Buffer;
end;
finally
ShellMalloc.Free(Buffer);
end;
end;
end; procedure TForm1.SpeedButton1Click(Sender: TObject);
var
Path1: string;
begin
Path :=Edit1.Text;
SelDir('SelectDirectory Sample','d:\temp',Path1);
Edit1.Text :=Path1
end; end.
uses ShlObj, ActiveX;{*****************************************************}
{ The SelectDirectoryEx function like SelectDirectory }
{ But you can specify the Init Dir }
{ hOwn:Parent Window Handle }
{ Path:In and Out,In-->Init Dir }
{ Caption:Hint text }
{ Root:Root Dir }
{ uFlag:Which Style you want to use,like }
{ BIF_RETURNONLYFSDIRS or BIF_VALIDATE }
{ Please see Win32SDK for more detial }
{*****************************************************}function SelectDirectoryEx(hOwn: HWND; var Path: string; Caption, Root: string; uFlag: DWORD = $25): Boolean;
const
BIF_NEWDIALOGSTYLE = $0040;
var
BrowseInfo: TBrowseInfo;
Buffer: PChar;
RootItemIDList, ItemIDList: PItemIDList;
ShellMalloc: IMalloc;
IDesktopFolder: IShellFolder;
Dummy: LongWord; function BrowseCallbackProc(hwnd: HWND; uMsg: UINT; lParam: Cardinal; lpData: Cardinal): integer; stdcall;
var
PathName: array[0..MAX_PATH] of char;
begin
case uMsg of
BFFM_INITIALIZED:
SendMessage(Hwnd, BFFM_SETSELECTION, Ord(True), Integer(lpData));
BFFM_SELCHANGED:
begin
SHGetPathFromIDList(PItemIDList(lParam), @PathName);
SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0, LongInt(PChar(@PathName)));
end;
end;
Result := 0;
end;begin
Result := False;
FillChar(BrowseInfo, SizeOf(BrowseInfo), 0);
if (ShGetMalloc(ShellMalloc) = S_OK) and (ShellMalloc <> nil) then
begin
Buffer := ShellMalloc.Alloc(MAX_PATH);
try
RootItemIDList := nil;
if Root <> '' then begin
SHGetDesktopFolder(IDesktopFolder);
IDesktopFolder.ParseDisplayName(hOwn, nil, POleStr(WideString(Root)), Dummy, RootItemIDList, Dummy);
end;
with BrowseInfo do begin
hwndOwner := hOwn;
pidlRoot := RootItemIDList;
pszDisplayName := Buffer;
lpszTitle := PChar(Caption);
ulFlags := uFlag;
lpfn := @BrowseCallbackProc;
lParam := Integer(Pchar(Path));
end;
ItemIDList := ShBrowseForFolder(BrowseInfo);
Result := ItemIDList <> nil;
if Result then
begin
ShGetPathFromIDList(ItemIDList, Buffer);
ShellMalloc.Free(ItemIDList);
Path := StrPas(Buffer);
end;
finally
ShellMalloc.Free(Buffer);
end;
end;
end;
procedure TForm1.SpeedButton1Click(Sender: TObject);
var
Path: string;
begin
Path := 'C:\WinNT';
if SelectDirectoryEx(Handle, Path, 'Select Directory Sample', 'C:\') then
ShowMessage(Path);
end;
DirName必须是共享的