文件复制过程中显示进度条(九)

来源:互联网 发布:淘宝怎么不能合并付款 编辑:程序博客网 时间:2024/05/01 12:22

 

文件复制过程中显示进度条()
本文讲述如何在文件复制过程中显示进度条。
1.      创建一个对话框工程:CopyFileProcessor
2.      添加一个文本框,用来显示浏览出来的源文件路径,添加一个按钮“浏览”,来选择源文件,如下:
       CFileDialoglog(TRUE,"文件","*.*",OFN_HIDEREADONLY,"FILE(*.*)|*.*||",NULL);
       if(log.DoModal()==IDOK)
       {
              pathname=log.GetPathName();
              strname=log.GetFileName();
              GetDlgItem(IDC_EDADD)->SetWindowText(pathname);
       }
3.      添加一个文本框来显示浏览出来的目标存放路径,添加一个按钮“浏览”,来选择目标存放路径,如下:
       if(strname.IsEmpty())
              return;
       BROWSEINFObi;  
       charbuffer[MAX_PATH];  
       ZeroMemory(buffer,MAX_PATH);  
       bi.hwndOwner=GetSafeHwnd();  
       bi.pidlRoot=NULL;  
       bi.pszDisplayName=buffer;  
       bi.lpszTitle="选择一个文件夹";  
       bi.ulFlags=BIF_EDITBOX;  
       bi.lpfn=NULL;  
       bi.lParam=0;  
       bi.iImage=0;
       LPITEMIDLISTpList=NULL;  
       if((pList=SHBrowseForFolder(&bi))!=NULL)  
       {  
              charpath[MAX_PATH];
              ZeroMemory(path,MAX_PATH);
              SHGetPathFromIDList(pList,path);
              fullname=path;
              if(fullname.Right(1)!="//")
                     fullname.Format("%s//%s",path,strname);
              else
                     fullname.Format("%s%s",path,strname);
              GetDlgItem(IDC_EDDEST)->SetWindowText(fullname);
       }
4.      添加一个进度条控件,添加一个按钮“复制”,代码如下:
       if(pathname.IsEmpty())
              return;
       if(fullname.IsEmpty())
              return;
       readfile = newCFile(pathname,CFile::modeRead);
       HANDLEhfile = ::CreateFile(fullname,GENERIC_WRITE|GENERIC_READ,0,0,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,0);
       CloseHandle(hfile);
       writefile = newCFile(fullname,CFile::modeWrite);
       filelen = readfile->GetLength();
       ldiv_tr;
       r = ldiv(filelen,100);
       longpos = r.quot;
       longipos;
       ipos = pos;
       inti = 0;
       hGlobal = GlobalAlloc(GMEM_MOVEABLE,512);
       pvData = GlobalLock(hGlobal);
       while(1)
       {
              ZeroMemory(pvData,512);
              readlen = readfile->ReadHuge(pvData,512);
              poslen = readfile->GetPosition();
              if(poslen>ipos)
              {
                     ipos += pos;
                     i++;
              }
              m_fileproc.SetPos(i);
              m_fileproc.Invalidate();
              writefile->WriteHuge(pvData,readlen);
              if(poslen == filelen)
                     break;
       }
       AfxMessageBox("复制完成");
       m_fileproc.SetPos(0);
       GlobalUnlock(hGlobal);
       readfile->Close();
       writefile->Close();
注意,其中有些变量的声明如下:
       CStringstrname;
       CStringfullname;
       CStringpathname;
       HGLOBALhGlobal;
       CFile* writefile;
       CFile* readfile;
       longreadlen,poslen,filelen;
       LPVOIDpvData;
并在构造函数中初始化,如下:
       readfile = NULL;
       writefile = NULL;
       pvData = NULL;
完成。