چطور میتونمfolder را انتخاب کنم نه فایل را
Printable View
چطور میتونمfolder را انتخاب کنم نه فایل را
سلام
کد:uses
ShlObj, ActiveX;
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.Button1Click(Sender: TObject);
var
Path: string;
begin
Path := 'C:\Windows';
if SelectDirectoryEx(Handle, Path, 'Select Directory Sample', 'C:\') then
ShowMessage(Path);
end;