﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-这里的黄昏静悄悄-随笔分类-MFC/SDK</title><link>http://www.cnitblog.com/jackrain/category/215.html</link><description>I Like C++</description><language>zh-cn</language><lastBuildDate>Sat, 01 Oct 2011 03:25:42 GMT</lastBuildDate><pubDate>Sat, 01 Oct 2011 03:25:42 GMT</pubDate><ttl>60</ttl><item><title>用CFile实现文件拷贝</title><link>http://www.cnitblog.com/jackrain/archive/2005/06/28/640.html</link><dc:creator>这里的黄昏静悄悄</dc:creator><author>这里的黄昏静悄悄</author><pubDate>Tue, 28 Jun 2005 10:56:00 GMT</pubDate><guid>http://www.cnitblog.com/jackrain/archive/2005/06/28/640.html</guid><wfw:comment>http://www.cnitblog.com/jackrain/comments/640.html</wfw:comment><comments>http://www.cnitblog.com/jackrain/archive/2005/06/28/640.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cnitblog.com/jackrain/comments/commentRss/640.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/jackrain/services/trackbacks/640.html</trackback:ping><description><![CDATA[熟悉CFile和CFileDialog类的使用。选择源文件和目标文件，然后执行拷贝工作。如下图所示<BR><IMG height=151 alt=copyfile.jpg src="http://www.cnitblog.com/images/cnitblog_com/jackrain/image/copyfile.jpg" width=575 border=0><BR>下面说一下具体的实现。<BR>1、用向导生成一个基于对话框的MFC应用程序，然后按照上图添加控件：2个static控件，2个Edit控件，3个Button控件。然后为3个Button控件添加鼠标单击的消息处理函数。<BR>2、在Dlg.h中添加变量<BR>&nbsp;DWORD dwRead;// 读取的字节数<BR>&nbsp;<FONT color=#0000ff>char</FONT> lpStrFile[50];// 文件名<BR>&nbsp;<FONT color=#0000ff>char</FONT> sPath[250];// 源文件路径<BR>&nbsp;<FONT color=#0000ff>char</FONT> dPath[250];// 目标文件路径<BR>&nbsp;<FONT color=#0000ff>char</FONT> szBuffer[1024]; <BR>3、在Dlg.cpp中添加代码<BR>首先要加入#include "afxdlgs.h" 这样才能用CFileDialog类<BR>在本例中不允许用户自己输入文件的路径，所以一开始要禁止输入，所以在OnInitDialog()中添加代码<BR>&nbsp;<FONT color=#0000ff>GetDlgItem(IDC_EDIT1)-&gt;EnableWindow(false);<BR>&nbsp;GetDlgItem(IDC_EDIT2)-&gt;EnableWindow(false);<BR></FONT>在未选择源文件之前，不能选择目标文件，不能点击“CopyFile”按钮，所以再添加如下代码<BR>&nbsp;<FONT color=#0000ff>GetDlgItem(IDC_BUTTON2)-&gt;EnableWindow(false);<BR>&nbsp;GetDlgItem(IDC_BUTTON3)-&gt;EnableWindow(false);<BR></FONT>&nbsp;然后初始化dwRead变量<BR>&nbsp;<FONT color=#0000ff>dwRead = 0;<BR></FONT>4、好了，为OnBnClickedButton1()添加代码来选择源文件<BR>首先我们要生成CFileDialog 类的实例m_OpenFileDlg，其中第一个参数决定了是打开还是保存对话框。<BR><FONT color=#0000ff>CFileDialog&nbsp; m_OpenFileDlg(1,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"text file(*.txt)|*.txt|picture file(*.jpg)|*.jpg|All file(*.*)|*.*||");<BR><FONT color=#000000>这样基本上就够了，不过我们仍然可以修改对话框的一些属性，不过必须在调用它的DoModal函数之前。</FONT><BR>&nbsp;OPENFILENAME &amp;openFile = m_OpenFileDlg.GetOFN();//注意这里是引用。<BR>&nbsp;openFile.lpstrTitle = "Source File Path";<BR>&nbsp;openFile.lpstrFilter = "All Files(*.*)|(*.*)";<BR>&nbsp;openFile.lpstrInitialDir = "H:\\";<BR>&nbsp;if(m_OpenFileDlg.DoModal()== IDOK)<BR>&nbsp;{<BR>&nbsp;&nbsp;CString filepath = m_OpenFileDlg.GetPathName();//<FONT color=#ff1493>获得文件路径名<BR></FONT>&nbsp;&nbsp;SetDlgItemText(IDC_EDIT1,filepath);<BR>&nbsp;&nbsp;memset(lpStrFile,0,50);//<FONT color=#ff1493>这个主要是为了在选择目标文件的时候用</FONT><BR>&nbsp;&nbsp;strcpy(lpStrFile,m_OpenFileDlg.GetFileName());<BR>&nbsp;&nbsp;memset(sPath,0,250);//<FONT color=#ff1493>清空，为了下次再用</FONT><BR>&nbsp;&nbsp;strcpy(sPath,m_OpenFileDlg.GetPathName());<BR>&nbsp;&nbsp;GetDlgItem(IDC_BUTTON2)-&gt;EnableWindow();//<FONT color=#ff1493>现在允许选择目标文件了。</FONT><BR>&nbsp;}<BR><BR><FONT color=#000000>接下来就是为OnBnClickedButton2()添加代码来选择目标文件了</FONT><BR>CFileDialog m_SaveFileDlg(0,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"text file(*.txt)|*.txt|picture file(*.jpg)|*.jpg|All file(*.*)|*.*||");<BR><FONT color=#000000>基本上跟打开文件对话框类似，不过这里要修改默认的保存的文件名，这就要用到上面的文件名了</FONT><BR>&nbsp;OPENFILENAME &amp;openFile = m_SaveFileDlg.GetOFN();<BR>&nbsp;openFile.lpstrTitle = "Dest File Path";<BR>&nbsp;strcpy(openFile.lpstrFile ,lpStrFile);<BR>&nbsp;//strcpy(openFile.lpstrDefExt,lpFileEx);<BR>&nbsp;openFile.lpstrFilter = "All Files(*.*)|(*.*)";<BR>&nbsp;openFile.lpstrInitialDir = "H:\\";<BR>&nbsp;if(m_SaveFileDlg.DoModal()== IDOK)<BR>&nbsp;{<BR>&nbsp;&nbsp;CString filepath = m_SaveFileDlg.GetPathName();<BR>&nbsp;&nbsp;SetDlgItemText(IDC_EDIT2,filepath);<BR>&nbsp;&nbsp;memset(dPath,0,250);<BR>&nbsp;&nbsp;strcpy(dPath,m_SaveFileDlg.GetPathName());<BR>&nbsp;&nbsp;GetDlgItem(IDC_BUTTON3)-&gt;EnableWindow();<BR>}<BR><FONT color=#000000>接下来是执行拷贝了，在OnBnClickedButtonCopy()中添加代码</FONT><BR>CFile m_Source,m_Destine;<BR>&nbsp;m_Source.Open(sPath,CFile::modeRead|CFile::shareDenyWrite,0);<BR>&nbsp;m_Destine.Open(dPath,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite|CFile::shareExclusive,0);//<FONT color=#000000>没有的话就创建</FONT><BR>&nbsp;do<BR>&nbsp;{<BR>&nbsp;&nbsp;memset(szBuffer,0,1024);<BR>&nbsp;&nbsp;dwRead = m_Source.Read(szBuffer,1024);<BR>&nbsp;&nbsp;m_Destine.Write(szBuffer,dwRead);<BR>&nbsp;}<BR>&nbsp;while(dwRead &gt; 0);<BR>&nbsp;m_Source.Close();<BR>&nbsp;m_Destine.Close();<BR>&nbsp;MessageBox("File Copy Success!");<BR>&nbsp;return;<BR></FONT><FONT color=#000000>函数很简单，首先打开文件，然后读取写入最后关闭就OK啦，简单吧。自己也试试吧。</FONT><img src ="http://www.cnitblog.com/jackrain/aggbug/640.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/jackrain/" target="_blank">这里的黄昏静悄悄</a> 2005-06-28 18:56 <a href="http://www.cnitblog.com/jackrain/archive/2005/06/28/640.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>