我们看下面一个例子:
//打开一些特殊的文件夹,需要加上ActiveX,shellapi,shlObj单元
Uses ActiveX, ShellApi, ShlObj;
Function OpenSpecialFolder(Flag:Integer;Handle: HWND = 0):Boolean;
//这里的Flag就是我们需要打开的文件夹的CSIDL值
Procedure FreePidl(pidl: PItemIDList);//释放掉PItemIDList实例
var
allocator: IMalloc;
begin
if Succeeded(shlobj.SHGetMalloc(allocator)) then
begin
allocator.Free(pidl);
{$IFDEF VER90}
allocator.Release;
{$ENDIF}
end;
end;
var
exInfo: TShellExecuteInfo;
begin
FillChar(exInfo, SizeOf(exInfo), 0);//给exInfo设置初始值
with exInfo do
begin
cbSize:= Sizeof(exInfo);
fMask:= SEE_MASK_FLAG_DDEWAIT or SEE_MASK_IDLIST;
Wnd:= handle;
nShow:= SW_SHOWNORMAL;
lpVerb:= 'open';
SHGetSpecialFolderLocation(Handle, Flag, PItemIDLIst(lpIDList));//定位到由CSIDL值指定的文件夹
end;
ShellExecuteEx(@exInfo);//打开文件夹
FreePidl(exInfo.lpIDList);
end;
//调用上面写的这个函数:
procedure TForm1.Button5Click(Sender: TObject);
begin
OpenSpecialFolder($0);//或是OPenSpecialFolder(CSIDL_DESKTOP),打开“桌面”窗口
end;