提示未定義IShellItem ,不知道是那個頭文件未引用,
系統是Win7,
delphi7和delphixe2都試過,頭文件引用了ShellAPI, ComObj還是不行;
應該是ShlObj;
給你段代碼參考
uses ActiveX, ComObj, ShlObj;
function TForm1.CopyItem(const aSrcItem, aDest, aNewName: string): HRESULT;
const
CLSID_FileOp: TGUID = '{3ad05575-8857-4850-9277-11b85bdb8e09}';
var
lFileOperation: IFileOperation;
psiFrom: IShellItem;
psiTo: IShellItem;
begin
//
// Initialize COM as STA.
//
Result := CoInitializeEx(nil, COINIT_APARTMENTTHREADED or COINIT_DISABLE_OLE1DDE);
if Succeeded(Result) then
begin
//
// Create the IFileOperation interface
//
Result := CoCreateInstance(CLSID_FileOp, nil, CLSCTX_ALL, IFileOperation,
lFileOperation);
if Succeeded(Result) then
begin
//
// Set the operation flags. Turn off all UI from being shown to the
// user during the operation. This includes error, confirmation,
// and progress dialogs.
//
Result := lFileOperation.SetOperationFlags(FOF_NO_UI);
if Succeeded(Result) then
begin
//
// Create an IShellItem from the supplied source path.
//
Result := SHCreateItemFromParsingName(aSrcItem,
nil,
IShellItem, psiFrom);
if Succeeded(Result) then
begin
if aDest <> '' then
begin
//
// Create an IShellItem from the supplied
// destination path.
//
Result := SHCreateItemFromParsingName(aDest,
nil,
IShellItem, psiTo);
end;
if Succeeded(Result) then
begin
//
// Add the operation
//
Result := lFileOperation.CopyItem(psiFrom, psiTo, aNewName, nil);
psiTo := nil;
end;
psiFrom := nil;
end;
if Succeeded(Result) then
begin
//
// Perform the operation to copy the file.
//
Result := lFileOperation.PerformOperations;
end;
end;
//
// Release the IFileOperation interface.
//
lFileOperation := nil;
end;
CoUninitialize;
end;
end;