且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

方法:使用IFileOperation显示进度对话框

更新时间:2023-01-23 20:49:22

如果您只需要进度对话框,请删除 IOperationsProgressDialog



CopyItem 只准备要复制的项目,因此 IOperationsProgressDialog :: Update ... 不会更新任何UI。如果只有几个文件,则不会显示UI对话框,如果 PerformOperations 因为它做得太快了。也许你想要删除 FOF_RENAMEONCOLLISION ,以便于测试。这应该与 SHFileOperation 完全相同。

  CString srcDir = L c:\\test\\src; 
CString dstDir = Lc:\\test\\dst;

CComPtr< IFileOperation> fileOp;
fileOp.CoCreateInstance(CLSID_FileOperation);
fileOp-> SetOperationFlags(FOFX_SHOWELEVATIONPROMPT);

srcDir + = L\\ *;
CFileFind finder;
BOOL next = finder.FindFile(srcDir);
while(next)
{
next = finder.FindNextFile();
if(finder.IsDots())
continue;

CComPtr< IShellItem> src,dst
if(S_OK!= SHCreateItemFromParsingName(finder.GetFilePath(),NULL,IID_PPV_ARGS(& src)))

if(S_OK!= SHCreateItemFromParsingName(dstDir,NULL,IID_PPV_ARGS(& dst)))
continue;

fileOp-> CopyItem(src,dst,0,NULL);
}

MessageBox(Lnothing copied so far ...);
hr = fileOp-> PerformOperations();
MessageBox(Ldone ...);


I’m trying to get a progress dialog to display for my copy operation in some sample code. I’m using IFileOperation because I found that using SHFileOperation will not display a progress dialog if the files have already been copied to the target location. My hope is that IFileOperation is a little more sophisticated and can handle that situation. Here’s the sample code I’ve tried….

CComPtr<IOperationsProgressDialog> opProgressDlg;
HRESULT hr = opProgressDlg.CoCreateInstance(CLSID_ProgressDialog);

CComPtr<IFileOperation> fileOp;
hr = fileOp.CoCreateInstance(CLSID_FileOperation);
hr = fileOp->SetOperationFlags(FOF_RENAMEONCOLLISION | FOFX_PRESERVEFILEEXTENSIONS | FOFX_NOMINIMIZEBOX);
hr = fileOp->SetProgressDialog(opProgressDlg);

hr = opProgressDlg->StartProgressDialog(m_hWnd, OPPROGDLG_DEFAULT);
hr = opProgressDlg->SetMode(PDM_DEFAULT);
hr = opProgressDlg->SetOperation(SPACTION_COPYING);
oldDirectory += _T("*.*");

CFileFind finder;
BOOL bWorking = finder.FindFile(oldDirectory);
while (bWorking)
    {
    bWorking = finder.FindNextFile();
    if (finder.IsDots())
        continue;
    CString name = finder.GetFilePath();
    IShellItem *psiFrom = nullptr;
    hr = SHCreateItemFromParsingName(CT2CW(name), NULL, IID_PPV_ARGS(&psiFrom));
    IShellItem *psiTo = NULL;
    hr = SHCreateItemFromParsingName(CT2CW(newDirectory), NULL, IID_PPV_ARGS(&psiTo));
    hr = fileOp->CopyItem(psiFrom, psiTo, CT2CW(finder.GetFileName()), NULL);
    //hr = opProgressDlg->UpdateLocations(psiFrom, psiTo, psiTo);
    }

opProgressDlg->StopProgressDialog();

hr = fileOp->PerformOperations();

The sample is trying to copy all files and folders from one location (oldDirectory) to another (newDirectory). The copy operation does copy everything correctly. I’m looking for help in getting the progress dialog to display during the operation. According to IFileOperation, I should be able to set the progress dialog through IOperationsProgressDialog. The documentation for this is extremely thin. I have not been able to find any samples showing how to fit the two together. Does anyone have experience with these two interfaces?

If you just need progress dialog, then remove the reference to IOperationsProgressDialog

CopyItem only prepares the items for copying, therefore IOperationsProgressDialog::Update... will not update any UI. Actual copying begins when PerformOperations is called.

UI dialog won't show if there are only a few files, because it's done too quickly. Maybe you want to remove FOF_RENAMEONCOLLISION to make it easier for testing. This should be exact same thing as SHFileOperation.

CString srcDir = L"c:\\test\\src";
CString dstDir = L"c:\\test\\dst";

CComPtr<IFileOperation> fileOp;
fileOp.CoCreateInstance(CLSID_FileOperation);
fileOp->SetOperationFlags(FOFX_SHOWELEVATIONPROMPT);

srcDir += L"\\*";
CFileFind finder;
BOOL next = finder.FindFile(srcDir);
while (next)
{
    next = finder.FindNextFile();
    if (finder.IsDots())
        continue;

    CComPtr<IShellItem> src, dst;
    if (S_OK != SHCreateItemFromParsingName(finder.GetFilePath(), NULL, IID_PPV_ARGS(&src)))
        continue;

    if (S_OK != SHCreateItemFromParsingName(dstDir, NULL, IID_PPV_ARGS(&dst)))
        continue;

    fileOp->CopyItem(src, dst, 0, NULL);
}

MessageBox(L"nothing copied so far...");
hr = fileOp->PerformOperations();
MessageBox(L"done...");