方法:使用IFileOperation显示进度对话框 [英] HowTo: Display progress dialog using IFileOperation

查看:293
本文介绍了方法:使用IFileOperation显示进度对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一些示例代码中获取一个进度对话框显示我的复制操作。我正在使用 IFileOperation 因为我发现如果文件已经复制到目标位置,使用 SHFileOperation 显示进度对话框。我的希望是IFileOperation是一个更复杂一点,可以处理这种情况。
这是我试过的示例代码...。

  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();

示例尝试从一个位置复制所有文件和文件夹oldDirectory)到另一个(newDirectory)。 复制操作会正确复制所有内容。我正在寻找帮助获取在操作过程中显示的进度对话框。根据IFileOperation,我应该能够通过 IOperationsProgressDialog 。这方面的文档非常薄。我没有找到任何样品,显示如何将两个一起。

解决方案

如果您只需要进度对话框,请删除 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...");

这篇关于方法:使用IFileOperation显示进度对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆