本文共 3726 字,大约阅读时间需要 12 分钟。
UINT CFileCopyView::CopyThread(){ int nCount = m_SourceListCtrl.GetCheckCount(); if (nCount == 0) { return 0; } // 2:Begin m_progressDlgExt.Start(); HRESULT hr = S_OK; LVITEM lvItem = { 0 }; CString strFilePath = _T(""); LPAFX_SHELLITEMINFO2 pItem = NULL; int nResult = COPY_Status_OK; int nFileOK = 0; ::CoInitialize(NULL); // 2.1:リリース for (int nIndex = 0; nIndex < m_SourceListCtrl.GetItemCount(); nIndex++) { if (m_SourceListCtrl.GetCheck(nIndex) == FALSE) continue; // init memset(&lvItem, 0x00, sizeof(LVITEM)); strFilePath = _T(""); pItem = NULL; // GetItem lvItem.iItem = nIndex; lvItem.mask = LVIF_PARAM; m_SourceListCtrl.GetItem(&lvItem); pItem = (LPAFX_SHELLITEMINFO2)lvItem.lParam; if (pItem == NULL) continue; strFilePath = SHGetFilePath(pItem->pParentFolder, pItem->pidlRel); if (strFilePath.IsEmpty()) continue; // Copy CString strDestPath = m_strTargetPath + pItem->szFQPath; IFileOperation* fileoperation = NULL; hr = ::CoCreateInstance(CLSID_FileOperation, NULL, CLSCTX_ALL, IID_IFileOperation, (void**)&fileoperation); if (hr == S_OK && fileoperation) { if (COPYFILE) fileoperation->SetOperationFlags(FOF_SIMPLEPROGRESS | FOF_NOCONFIRMMKDIR); else fileoperation->SetOperationFlags(FOF_NO_UI); fileoperation->SetOwnerWindow(NULL); // From IShellItem *pItemFrom = NULL; IShellItem *pItemTo = NULL; BOOL aborted = false; // 1.From hr = SHCreateItemFromParsingName(strFilePath, NULL, IID_IShellItem, (void**)(&pItemFrom)); if (hr == S_OK && pItemFrom) { // 2.To hr = SHCreateItemFromParsingName(strDestPath, NULL, IID_IShellItem, (void**)(&pItemTo)); // Check Folder if (hr != S_OK || pItemTo == NULL) // Folder not exiset { hr = SHCreateDirectory(NULL, strDestPath); if (hr == S_OK) { hr = SHCreateItemFromParsingName(strDestPath, NULL, IID_IShellItem, (void**)(&pItemTo)); } } if (hr == S_OK && pItemTo) { // 3.Copy hr = fileoperation->CopyItem(pItemFrom, pItemTo, NULL, NULL); fileoperation->PerformOperations(); fileoperation->GetAnyOperationsAborted(&aborted); if (aborted) { nResult = COPY_Status_Aborted; } else { nFileOK++; } pItemTo->Release(); } else { nResult = COPY_Status_Error; } pItemFrom->Release(); } else { nResult = COPY_Status_Error; } fileoperation->Release(); } else { nResult = COPY_Status_Error; } } ::CoUninitialize(); m_progressDlgExt.Stop(); m_hThread = NULL; CString strMessage = _T(""); if (nResult == COPY_Status_OK) { strMessage.Format(DllLoadIDResource(IDS_MSGInfo_CopyFiles1), nCount, nFileOK); } else { strMessage.Format(DllLoadIDResource(IDS_MSGInfo_CopyFiles2), nCount, nFileOK, nCount - nFileOK); } MessageBox(strMessage, DllLoadIDResource(IDS_MSGTitle_Release)); return 0;}
转载地址:http://hnsws.baihongyu.com/