CopyFile从硬盘拷贝文件到U盘,希望用CProgressCtrl显示拷贝的进度,请达人赐教,最好有代码

解决方案 »

  1.   

    用以下两个函数:
    BOOL CopyFileEx(LPCTSTR lpExistingFileName, // name of existing file
    LPCTSTR lpNewFileName, // name of new file
    LPPROGRESS_ROUTINE lpProgressRoutine, // callback function
    LPVOID lpData, // callback parameter
    LPBOOL pbCancel, // cancel status
    DWORD dwCopyFlags // copy options
    );DWORD CALLBACK CopyProgressRoutine(
    LARGE_INTEGER TotalFileSize, // file size
    LARGE_INTEGER TotalBytesTransferred, // bytes transferred
    LARGE_INTEGER StreamSize, // bytes in stream
    LARGE_INTEGER StreamBytesTransferred, // bytes transferred for stream
    DWORD dwStreamNumber, // current stream
    DWORD dwCallbackReason, // callback reason
    HANDLE hSourceFile, // handle to source file
    HANDLE hDestinationFile, // handle to destination file
    LPVOID lpData // from CopyFileEx
    );比如
    DWORD CALLBACK MyCopyProgressRoutine(LARGE_INTEGER TotalFileSize, // file size
    LARGE_INTEGER TotalBytesTransferred, // bytes transferred
    LARGE_INTEGER StreamSize, // bytes in stream
    LARGE_INTEGER StreamBytesTransferred, // bytes transferred for stream
    DWORD dwStreamNumber, // current stream
    DWORD dwCallbackReason, // callback reason
    HANDLE hSourceFile, // handle to source file
    HANDLE hDestinationFile, // handle to destination file
    LPVOID lpData // from CopyFileEx
    )
    {
      CDialogTest* ptr = (CDialogTest*)lpData;
      // do something with pointer, e.g. ptr->m_Progressbar.Pos blahblah   DWORD percent = TotalBytesTransferred*100/TotalFileSize;
      return TRUE;
    }BOOL CDialogTest::Copy(CString from, CString to)
    {
      BOOL cancel;
      return CopyFileEx( from, to, (LPPROGRESS_ROUTINE)MyCopyProgressRoutine, (this)this, &cancel, FALSE);
    }