FileCopyDemoDlg.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. // FileCopyDemoDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "FileCopyDemo.h"
  5. #include "FileCopyDemoDlg.h"
  6. #include <process.h>
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CAboutDlg dialog used for App About
  14. class CAboutDlg : public CDialog
  15. {
  16. public:
  17. CAboutDlg();
  18. // Dialog Data
  19. //{{AFX_DATA(CAboutDlg)
  20. enum { IDD = IDD_ABOUTBOX };
  21. //}}AFX_DATA
  22. // ClassWizard generated virtual function overrides
  23. //{{AFX_VIRTUAL(CAboutDlg)
  24. protected:
  25. virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
  26. //}}AFX_VIRTUAL
  27. // Implementation
  28. protected:
  29. //{{AFX_MSG(CAboutDlg)
  30. //}}AFX_MSG
  31. DECLARE_MESSAGE_MAP()
  32. };
  33. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  34. {
  35. //{{AFX_DATA_INIT(CAboutDlg)
  36. //}}AFX_DATA_INIT
  37. }
  38. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  39. {
  40. CDialog::DoDataExchange(pDX);
  41. //{{AFX_DATA_MAP(CAboutDlg)
  42. //}}AFX_DATA_MAP
  43. }
  44. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  45. //{{AFX_MSG_MAP(CAboutDlg)
  46. // No message handlers
  47. //}}AFX_MSG_MAP
  48. END_MESSAGE_MAP()
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CFileCopyDemoDlg dialog
  51. CFileCopyDemoDlg::CFileCopyDemoDlg(CWnd* pParent /*=NULL*/)
  52. : CDialog(CFileCopyDemoDlg::IDD, pParent)
  53. {
  54. //{{AFX_DATA_INIT(CFileCopyDemoDlg)
  55. m_strSrc = _T("");
  56. m_strDes = _T("");
  57. m_strInfo = _T("");
  58. m_strPercentage = _T("");
  59. m_strSpeed = _T("");
  60. m_bSync = TRUE;
  61. //}}AFX_DATA_INIT
  62. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  63. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  64. }
  65. void CFileCopyDemoDlg::DoDataExchange(CDataExchange* pDX)
  66. {
  67. CDialog::DoDataExchange(pDX);
  68. //{{AFX_DATA_MAP(CFileCopyDemoDlg)
  69. DDX_Control(pDX, IDC_EDIT_SPEED, m_editSpeed);
  70. DDX_Control(pDX, IDC_EDIT_PERCENTAGE, m_editPercentage);
  71. DDX_Control(pDX, IDC_PROGRESS_COPY, m_prgsCopy);
  72. DDX_Control(pDX, IDC_EDIT_INFO, m_editInfo);
  73. DDX_Text(pDX, IDC_EDIT_SRC, m_strSrc);
  74. DDX_Text(pDX, IDC_EDIT_DES, m_strDes);
  75. DDX_Text(pDX, IDC_EDIT_INFO, m_strInfo);
  76. DDX_Text(pDX, IDC_EDIT_PERCENTAGE, m_strPercentage);
  77. DDX_Text(pDX, IDC_EDIT_SPEED, m_strSpeed);
  78. DDX_Check(pDX, IDC_CHECK_SYNC, m_bSync);
  79. //}}AFX_DATA_MAP
  80. }
  81. BEGIN_MESSAGE_MAP(CFileCopyDemoDlg, CDialog)
  82. //{{AFX_MSG_MAP(CFileCopyDemoDlg)
  83. ON_WM_SYSCOMMAND()
  84. ON_WM_PAINT()
  85. ON_WM_QUERYDRAGICON()
  86. ON_BN_CLICKED(IDC_BUTTON_SRC, OnButtonSrc)
  87. ON_BN_CLICKED(IDC_BUTTON_DES, OnButtonDes)
  88. ON_BN_CLICKED(IDC_BUTTON_PAUSE, OnButtonPause)
  89. ON_BN_CLICKED(IDC_BUTTON_CANCEL, OnButtonCancel)
  90. ON_BN_CLICKED(IDC_BUTTON_RESUME, OnButtonResume)
  91. //}}AFX_MSG_MAP
  92. END_MESSAGE_MAP()
  93. /////////////////////////////////////////////////////////////////////////////
  94. // CFileCopyDemoDlg message handlers
  95. BOOL CFileCopyDemoDlg::OnInitDialog()
  96. {
  97. CDialog::OnInitDialog();
  98. // Add "About..." menu item to system menu.
  99. // IDM_ABOUTBOX must be in the system command range.
  100. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  101. ASSERT(IDM_ABOUTBOX < 0xF000);
  102. CMenu* pSysMenu = GetSystemMenu(FALSE);
  103. if (pSysMenu != NULL)
  104. {
  105. CString strAboutMenu;
  106. strAboutMenu.LoadString(IDS_ABOUTBOX);
  107. if (!strAboutMenu.IsEmpty())
  108. {
  109. pSysMenu->AppendMenu(MF_SEPARATOR);
  110. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  111. }
  112. }
  113. // Set the icon for this dialog. The framework does this automatically
  114. // when the application's main window is not a dialog
  115. SetIcon(m_hIcon, TRUE); // Set big icon
  116. SetIcon(m_hIcon, FALSE); // Set small icon
  117. // TODO: Add extra initialization here
  118. m_hCopyThread = NULL;
  119. return TRUE; // return TRUE unless you set the focus to a control
  120. }
  121. void CFileCopyDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
  122. {
  123. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  124. {
  125. CAboutDlg dlgAbout;
  126. dlgAbout.DoModal();
  127. }
  128. else
  129. {
  130. CDialog::OnSysCommand(nID, lParam);
  131. }
  132. }
  133. // If you add a minimize button to your dialog, you will need the code below
  134. // to draw the icon. For MFC applications using the document/view model,
  135. // this is automatically done for you by the framework.
  136. void CFileCopyDemoDlg::OnPaint()
  137. {
  138. if (IsIconic())
  139. {
  140. CPaintDC dc(this); // device context for painting
  141. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  142. // Center icon in client rectangle
  143. int cxIcon = GetSystemMetrics(SM_CXICON);
  144. int cyIcon = GetSystemMetrics(SM_CYICON);
  145. CRect rect;
  146. GetClientRect(&rect);
  147. int x = (rect.Width() - cxIcon + 1) / 2;
  148. int y = (rect.Height() - cyIcon + 1) / 2;
  149. // Draw the icon
  150. dc.DrawIcon(x, y, m_hIcon);
  151. }
  152. else
  153. {
  154. CDialog::OnPaint();
  155. }
  156. }
  157. // The system calls this to obtain the cursor to display while the user drags
  158. // the minimized window.
  159. HCURSOR CFileCopyDemoDlg::OnQueryDragIcon()
  160. {
  161. return (HCURSOR) m_hIcon;
  162. }
  163. void CFileCopyDemoDlg::OnButtonSrc()
  164. {
  165. // TODO: Add your control notification handler code here
  166. CFileDialog dlgFile(TRUE);
  167. if (IDOK != dlgFile.DoModal())
  168. {
  169. return ;
  170. }
  171. UpdateData(TRUE);
  172. m_strSrc = dlgFile.GetPathName();
  173. UpdateData(FALSE);
  174. }
  175. void CFileCopyDemoDlg::OnButtonDes()
  176. {
  177. // TODO: Add your control notification handler code here
  178. CFileDialog dlgFile(FALSE);
  179. if (IDOK != dlgFile.DoModal())
  180. {
  181. return ;
  182. }
  183. UpdateData(TRUE);
  184. m_strDes = dlgFile.GetPathName();
  185. UpdateData(FALSE);
  186. }
  187. BOOL __stdcall CopingProgressCB(
  188. const void * const pUserData,
  189. const DOUBLE dbSpeed,
  190. const DWORD dwPercentage
  191. )
  192. {
  193. CFileCopyDemoDlg *pDlg = (CFileCopyDemoDlg *)pUserData;
  194. if (NULL == pDlg)
  195. {
  196. return TRUE;
  197. }
  198. DOUBLE dbSpeedInSec = dbSpeed * 1000;
  199. if (dbSpeedInSec > 1024 * 1024)
  200. {
  201. dbSpeedInSec /= 1024 * 1024;
  202. pDlg->m_strSpeed.Format(_T("%.2lfMB/S"), dbSpeedInSec);
  203. }
  204. else if (dbSpeedInSec > 1024)
  205. {
  206. dbSpeedInSec /= 1024;
  207. pDlg->m_strSpeed.Format(_T("%.2lfKB/S"), dbSpeedInSec);
  208. }
  209. else
  210. {
  211. pDlg->m_strSpeed.Format(_T("%.2lfByte/S"), dbSpeedInSec);
  212. }
  213. pDlg->m_editSpeed.SetWindowText(pDlg->m_strSpeed);
  214. pDlg->m_strPercentage.Format(_T("%d%%"), dwPercentage);
  215. pDlg->m_editPercentage.SetWindowText(pDlg->m_strPercentage);
  216. pDlg->m_prgsCopy.SetPos(dwPercentage);
  217. return TRUE;
  218. }
  219. BOOL __stdcall AsyncCopyResultCB(
  220. const void * const pUserData,
  221. const BOOL bCopySuccess,
  222. const DOUBLE dbSpeed,
  223. const tstring strErrorMsg
  224. )
  225. {
  226. CFileCopyDemoDlg *pDlg = (CFileCopyDemoDlg *)pUserData;
  227. if (NULL == pDlg)
  228. {
  229. return TRUE;
  230. }
  231. CString strMsg;
  232. if (bCopySuccess)
  233. {
  234. CString strSpeed;
  235. DOUBLE dbSpeedInSec = dbSpeed * 1000;
  236. if (dbSpeedInSec > 1024 * 1024)
  237. {
  238. dbSpeedInSec /= 1024 * 1024;
  239. strSpeed.Format(_T("%.2lfMB/S"), dbSpeedInSec);
  240. }
  241. else if (dbSpeedInSec > 1024)
  242. {
  243. dbSpeedInSec /= 1024;
  244. strSpeed.Format(_T("%.2lfKB/S"), dbSpeedInSec);
  245. }
  246. else
  247. {
  248. strSpeed.Format(_T("%.2lfByte/S"), dbSpeedInSec);
  249. }
  250. pDlg->m_prgsCopy.SetPos(100);
  251. pDlg->m_strPercentage.Format(_T("%d%%"), 100);
  252. pDlg->m_strSpeed.Empty();
  253. strMsg.Format(_T("Asynchronous Copy Success, Speed = %s.\r\n"), strSpeed);
  254. }
  255. else
  256. {
  257. pDlg->m_prgsCopy.SetPos(0);
  258. pDlg->m_strPercentage.Format(_T("%d%%"), 0);
  259. pDlg->m_strSpeed.Empty();
  260. strMsg.Format(_T("Asynchronous Copy Fail : %s\r\n"), strErrorMsg.c_str());
  261. }
  262. pDlg->m_editSpeed.SetWindowText(pDlg->m_strSpeed);
  263. pDlg->m_editPercentage.SetWindowText(pDlg->m_strPercentage);
  264. pDlg->m_strInfo += strMsg;
  265. pDlg->m_editInfo.SetWindowText(pDlg->m_strInfo);
  266. pDlg->m_editInfo.SetSel(pDlg->m_strInfo.GetLength(), pDlg->m_strInfo.GetLength());
  267. return TRUE;
  268. }
  269. unsigned __stdcall SyncCopyThreadFunc(void* pArguments)
  270. {
  271. CFileCopyDemoDlg *pThis = (CFileCopyDemoDlg *)pArguments;
  272. if (NULL == pThis)
  273. {
  274. _endthreadex (1);
  275. return 1;
  276. }
  277. tstring strErrMsg;
  278. DOUBLE dbSpeed = 0.0;
  279. BOOL bRet = pThis->m_filecopy.CopyFile(
  280. pThis->m_strSrc,
  281. pThis->m_strDes,
  282. TRUE,
  283. 1000,
  284. dbSpeed,
  285. strErrMsg
  286. );
  287. CString strMsg;
  288. if (bRet)
  289. {
  290. CString strSpeed;
  291. DOUBLE dbSpeedInSec = dbSpeed * 1000;
  292. if (dbSpeedInSec > 1024 * 1024)
  293. {
  294. dbSpeedInSec /= 1024 * 1024;
  295. strSpeed.Format(_T("%.2lfMB/S"), dbSpeedInSec);
  296. }
  297. else if (dbSpeedInSec > 1024)
  298. {
  299. dbSpeedInSec /= 1024;
  300. strSpeed.Format(_T("%.2lfKB/S"), dbSpeedInSec);
  301. }
  302. else
  303. {
  304. strSpeed.Format(_T("%.2lfByte/S"), dbSpeedInSec);
  305. }
  306. pThis->m_prgsCopy.SetPos(100);
  307. pThis->m_strPercentage.Format(_T("%d%%"), 100);
  308. pThis->m_strSpeed.Empty();
  309. strMsg.Format(_T("Synchronous Copy Success, Speed = %s.\r\n"), strSpeed);
  310. }
  311. else
  312. {
  313. pThis->m_prgsCopy.SetPos(0);
  314. pThis->m_strPercentage.Format(_T("%d%%"), 0);
  315. pThis->m_strSpeed.Empty();
  316. strMsg.Format(_T("Synchronous Copy Fail : %s\r\n"), strErrMsg.c_str());
  317. }
  318. pThis->m_editSpeed.SetWindowText(pThis->m_strSpeed);
  319. pThis->m_editPercentage.SetWindowText(pThis->m_strPercentage);
  320. pThis->m_strInfo += strMsg;
  321. pThis->m_editInfo.SetWindowText(pThis->m_strInfo);
  322. pThis->m_editInfo.SetSel(pThis->m_strInfo.GetLength(), pThis->m_strInfo.GetLength());
  323. CloseHandle(pThis->m_hCopyThread);
  324. pThis->m_hCopyThread = NULL;
  325. return 1;
  326. }
  327. void CFileCopyDemoDlg::OnOK()
  328. {
  329. // TODO: Add extra validation here
  330. UpdateData(TRUE);
  331. m_filecopy.SetUserData(this);
  332. m_filecopy.SetCopingProgressCB(CopingProgressCB);
  333. m_filecopy.SetAsyncCopyResultCB(AsyncCopyResultCB);
  334. unsigned threadID = 0;
  335. if (m_bSync)
  336. {
  337. if (NULL != m_hCopyThread)
  338. {
  339. CString strMsg;
  340. strMsg.Format(_T("Synchronous Copy Fail : In coping.\r\n"));
  341. m_strInfo += strMsg;
  342. }
  343. else
  344. {
  345. m_hCopyThread =
  346. (HANDLE)_beginthreadex(NULL, 0, &SyncCopyThreadFunc, this, 0, &threadID);
  347. if (NULL == m_hCopyThread)
  348. {
  349. CString strMsg;
  350. strMsg.Format(_T("Create Synchronous Copy Thread Fail.\r\n"));
  351. m_strInfo += strMsg;
  352. }
  353. else
  354. {
  355. CString strMsg;
  356. strMsg.Format(_T("Create Synchronous Copy Thread Success.\r\n"));
  357. m_strInfo += strMsg;
  358. }
  359. }
  360. }
  361. else
  362. {
  363. tstring strErrMsg;
  364. DOUBLE dbSpeed = 0.0;
  365. BOOL bRet = m_filecopy.CopyFile(m_strSrc, m_strDes, m_bSync, 1000, dbSpeed, strErrMsg);
  366. if (bRet)
  367. {
  368. CString strMsg;
  369. strMsg.Format(_T("Send Asynchronous Copy Success.\r\n"));
  370. m_strInfo += strMsg;
  371. }
  372. else
  373. {
  374. CString strMsg;
  375. strMsg.Format(_T("Send Asynchronous Copy Fail : %s\r\n"), strErrMsg.c_str());
  376. m_strInfo += strMsg;
  377. }
  378. }
  379. UpdateData(FALSE);
  380. m_editInfo.SetSel(m_strInfo.GetLength(), m_strInfo.GetLength());
  381. //CDialog::OnOK();
  382. }
  383. void CFileCopyDemoDlg::OnButtonPause()
  384. {
  385. // TODO: Add your control notification handler code here
  386. m_filecopy.Pause();
  387. }
  388. void CFileCopyDemoDlg::OnButtonCancel()
  389. {
  390. // TODO: Add your control notification handler code here
  391. m_filecopy.Cancel();
  392. }
  393. void CFileCopyDemoDlg::OnCancel()
  394. {
  395. // TODO: Add extra cleanup here
  396. CDialog::OnCancel();
  397. }
  398. void CFileCopyDemoDlg::OnButtonResume()
  399. {
  400. // TODO: Add your control notification handler code here
  401. m_filecopy.Resume();
  402. }