SHOpenFolderAndSelectItems (使用资源管理器打开一个文件夹并选中指定文件,如果文件夹已经被打开就直接选中)
function GetItemIdListFromPath (path:widestring; var lpItemIdList:PItemIdList):boolean;
var
pShellFolder:IShellFolder;
hr:HRESULT;
chused:cardinal;
attr:cardinal;
begin
result:=False;
pShellFolder:=nil;
// 获得接口
if SHGetDesktopFolder(pShellFolder)<>NOERROR then exit;
// 将路径转换为 ITEMIDLIST
hr:=pShellFolder.ParseDisplayName(0,nil{0},pwidechar(path),chused,lpitemidlist,attr);
if FAILED(hr) then begin
lpItemIdList:=nil;
exit;
end;
result:=true;
end;
//函数 使用资源管理器打开一个文件夹并选中指定文件,如果文件夹已经被打开就直接选中
procedure OpenfolderAndSelect(fname:string);
procedure old_locate(fname:string);
begin
ShellExecute(0,'explore',PChar('/n,/select,' + fname),'','',SW_SHOWNORMAL);
end;
type
SHOpenFolderAndSelectItems=function(pidlFolder:PItemIdList; cidl:cardinal; apidl:PItemIdList; dwflags:dword): HRESULT; stdcall;
var
ShOp:SHOpenFolderAndSelectItems;
idlist:PItemIdList;
hr:hresult;
dllHandle:Thandle;
begin
dllHandle:=SafeLoadLibrary('shell32.dll');
if dllHandle=0 then begin
old_locate(fname);
exit;
end;
ShOp:=GetProcAddress(dllHandle,'SHOpenFolderAndSelectItems');
if @ShOp=nil then begin
FreeLibrary(dllHandle);
old_locate(fname);
exit;
end;
CoInitialize(nil);
GetItemIdListFromPath(fname,idlist);
try
hr:=shop(idlist,0,nil,0);
except
old_locate(fname);
FreeLibrary(dllHandle);
CoUnInitialize;
exit;
end;
if FAILED(hr) then old_locate(fname);
FreeLibrary(dllHandle);
CoUnInitialize;
end;