SelectDirectory显示的对话框未免太古老了吧。下面这个unit 封装了win95/98的
SHBrowseFolder()函数。另外我还做了一个tcomponent,增加几个onevent的功能
unit MyBrowseFolder;
interface
uses Windows,SysUtils;
function BrowseFolder(const Hwnd:THandle;const Title,Root,InitPath:String):String;
//root 指定最顶层的目录 initpath指定初始目录
implementation
uses ShlObj;
function BrowseProc(hwnd:HWND; uMsg:UINT;
lParam, lpData :LPARAM):Integer;stdcall;
begin
if uMsg <> bffm_selchanged then
SendMessage(hwnd,BFFM_SETSELECTIONA,Ord(False),lpData);
Result:=0;
end;
function BrowseFolder(const Hwnd:THandle;const Title,Root,InitPath:String):String;
var
fFolder :TBrowseInfo;
fPath
char;
fItemID
ItemIDList;
fRootItem
ItemIDList;
fInitpathItem
ItemIDList;
fDispName :array[0..MAX_PATH] of Char;
slFolder :IShellFolder;
A ,B :Cardinal;
begin
Result:='';
SHGetDesktopFolder(slFolder);
slFolder.ParseDisplayName(Hwnd,nil,
StringToOleStr(Root),
A,fRootItem,B);
slFolder.ParseDisplayName(Hwnd,nil,
StringToOleStr(InitPath),
A,fInitpathItem,B);
with fFolder do
begin
hwndOwner :=Hwnd;
pidlRoot :=fRootItem;
pszDisplayName :=@fdispname;
lpszTitle :=PChar(Title) ;
lpfn :=@BrowseProc;
ulFlags :=0;
lParam :=Integer(fInitpathItem);
end;
fItemID :=SHBrowseForFolder(fFolder);
if Assigned(fItemID)then
begin
Getmem(fPath,MAX_PATH );
try
if SHGetPathFromIDList(fItemID,fPath) then Result:=String(fPath);
finally
freeMem(fPath,MAX_PATH);
end;
end;
end;
end.