gusucode.com > VC记录最近打开文件源码程序 > VC记录最近打开文件/MruFileTestView.cpp
// MruFileTestView.cpp : implementation of the CMruFileTestView class // #include "stdafx.h" #include "MruFileTest.h" #include "MruFileTestDoc.h" #include "MruFileTestView.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CMruFileTestView IMPLEMENT_DYNCREATE(CMruFileTestView, CFormView) BEGIN_MESSAGE_MAP(CMruFileTestView, CFormView) //{{AFX_MSG_MAP(CMruFileTestView) ON_COMMAND(ID_FILE_NEW, OnFileNew) ON_COMMAND(ID_FILE_OPEN, OnFileOpen) ON_COMMAND(ID_FILE_SAVE, OnFileSave) ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs) ON_EN_CHANGE(IDC_TEXTEDIT, OnChangeTextedit) ON_COMMAND(ID_MRU1, OnMru1) ON_COMMAND(ID_MRU2, OnMru2) ON_COMMAND(ID_MRU3, OnMru3) ON_COMMAND(ID_MRU4, OnMru4) ON_COMMAND(ID_MRU_CLR, OnMruClr) ON_UPDATE_COMMAND_UI(ID_MRU_CLR, OnUpdateMruClr) ON_COMMAND(ID_EDIT_CUT, OnEditCut) ON_COMMAND(ID_EDIT_COPY, OnEditCopy) ON_COMMAND(ID_EDIT_PASTE, OnEditPaste) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CMruFileTestView construction/destruction CMruFileTestView::CMruFileTestView() : CFormView(CMruFileTestView::IDD) { //{{AFX_DATA_INIT(CMruFileTestView) m_strText = _T(""); //}}AFX_DATA_INIT // TODO: add construction code here m_PathName = _T(""); //打开的文件路径名 m_FileName = _T(""); //打开的文件名 b_ModifiedFlag = false; //文件修改标志 b_SelFlag = false; //选择标志 } CMruFileTestView::~CMruFileTestView() { } void CMruFileTestView::DoDataExchange(CDataExchange* pDX) { CFormView::DoDataExchange(pDX); //{{AFX_DATA_MAP(CMruFileTestView) DDX_Control(pDX, IDC_TEXTEDIT, m_TextEdit); DDX_Text(pDX, IDC_TEXTEDIT, m_strText); //}}AFX_DATA_MAP } BOOL CMruFileTestView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CFormView::PreCreateWindow(cs); } void CMruFileTestView::OnInitialUpdate() { CFormView::OnInitialUpdate(); GetParentFrame()->RecalcLayout(); ResizeParentToFit(); CMruFileTestApp* app = (CMruFileTestApp *)AfxGetApp(); //应用程序指针 m_CMruFile.m_IniFileName = app->m_exePath+_T("MruFile.ini"); //设置ini文件名 m_CMruFile.ReadMru(); //读取ini文件中的最近文件 } ///////////////////////////////////////////////////////////////////////////// // CMruFileTestView diagnostics #ifdef _DEBUG void CMruFileTestView::AssertValid() const { CFormView::AssertValid(); } void CMruFileTestView::Dump(CDumpContext& dc) const { CFormView::Dump(dc); } CMruFileTestDoc* CMruFileTestView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMruFileTestDoc))); return (CMruFileTestDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CMruFileTestView message handlers //文件内容装入Edit控件 void CMruFileTestView::LoadFile(LPCTSTR PathName) { CFile file; //构造一个CFile类的对象 if( file.Open( PathName, CFile::modeRead )==0 ) //以读方式打开文件 return; int len = file.GetLength(); //求文件长度 m_strText.Empty(); file.Read( m_strText.GetBuffer(len), len ); m_strText.ReleaseBuffer(); file.Close(); //关闭文件 UpdateData( false ); } //保存编辑框内容到文件 void CMruFileTestView::SaveFile(LPCTSTR PathName) { UpdateData( true ); CFile file; if( file.Open( PathName, CFile::modeCreate | CFile::modeWrite )==0 ) //新建并以写方式打开文件 return; file.Write( (LPCTSTR)m_strText, m_strText.GetLength() ); //把字符串内容写入文件 file.Close(); //关闭文件 } //新建文件 void CMruFileTestView::OnFileNew() { if( b_ModifiedFlag ) //检查修改标志 { int t = ::MessageBox( NULL, m_FileName+"的文字已经改变,要存盘吗?", "警告", MB_YESNOCANCEL | MB_ICONWARNING ); if( t == 0 || t == IDCANCEL ) return; if( t == IDYES ) OnFileSave(); //调用保存 } m_TextEdit.SetWindowText( _T("") ); //清空编辑框 b_ModifiedFlag = false; AfxGetMainWnd()->SetWindowText( _T("未命名") ); //修改窗口标题 } //打开文件 void CMruFileTestView::OnFileOpen() { if( b_ModifiedFlag ) //检查修改标志 { int t = ::MessageBox( NULL, m_FileName+"的文字已经改变,要存盘吗?", "警告", MB_YESNOCANCEL | MB_ICONWARNING ); if( t == 0 || t == IDCANCEL ) return; if( t == IDYES ) OnFileSave(); //调用保存 } CString sFilter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*||"; //限定文件类型为txt文件 CFileDialog m_Dlg( TRUE, "", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, (LPCTSTR)sFilter, NULL ); //定制打开文件对话框 if( m_Dlg.DoModal() == IDOK ) //弹出打开文件对话框 { m_PathName = m_Dlg.GetPathName(); //获取文件路径名 m_FileName = m_Dlg.GetFileName(); //获取文件名 LoadFile( m_PathName ); //打开文件并装入编辑控件 b_ModifiedFlag = false; //文件未修改过 m_CMruFile.AddMru( m_PathName ); //添加到最近文件菜单 AfxGetMainWnd()->SetWindowText( m_FileName ); //修改窗口标题 } } //保存文件 void CMruFileTestView::OnFileSave() { if( b_ModifiedFlag ) //检查修改标志 { if( m_FileName.IsEmpty() ) //没有文件名 { OnFileSaveAs(); } else { SaveFile( m_PathName ); //编辑控件内容装入文件 } b_ModifiedFlag = false; } } //另存为 void CMruFileTestView::OnFileSaveAs() { CString sFilter = "文本文件(*.txt)|所有文件(*.*)|*.*||"; CFileDialog m_Dlg( FALSE, "txt", m_FileName, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, (LPCTSTR)sFilter,NULL ); if( m_Dlg.DoModal() == IDOK ) //弹出另存为对话框 { m_PathName=m_Dlg.GetPathName(); //获取文件路径名 m_FileName=m_Dlg.GetFileName(); //获取文件名 SaveFile( m_PathName ); //编辑控件内容装入文件 m_CMruFile.AddMru( m_PathName ); //添加到最近文件菜单 b_ModifiedFlag = false; AfxGetMainWnd()->SetWindowText( m_FileName ); //修改窗口标题 } } //编辑控件内容改变 void CMruFileTestView::OnChangeTextedit() { b_ModifiedFlag = true; } //打开最近文件 void CMruFileTestView::OpenMruFile(int nMru) { if( b_ModifiedFlag ) //检查修改标志 { int t = ::MessageBox( NULL, m_FileName+"的文字已经改变,要存盘吗?", "警告", MB_YESNOCANCEL | MB_ICONWARNING ); if( t == 0 || t == IDCANCEL ) return; if( t == IDYES ) OnFileSave(); //调用保存 } m_PathName = m_CMruFile.m_PathName[nMru]; int i=0, j=0; while( i<m_PathName.GetLength() ) { if( m_PathName[i]=='\\' ) j = i; i++; } m_FileName = m_PathName.Mid( j+1 ); //分离文件名 LoadFile( m_PathName ); //打开文件并装入编辑控件 b_ModifiedFlag = false; //文件未修改过 m_CMruFile.AddMru( m_PathName ); //添加到最近文件菜单 AfxGetMainWnd()->SetWindowText( m_FileName ); //修改窗口标题 } void CMruFileTestView::OnMru1() { OpenMruFile(0); } void CMruFileTestView::OnMru2() { OpenMruFile(1); } void CMruFileTestView::OnMru3() { OpenMruFile(2); } void CMruFileTestView::OnMru4() { OpenMruFile(3); } //清除最近文件 void CMruFileTestView::OnMruClr() { m_CMruFile.ClearMru(); } void CMruFileTestView::OnUpdateMruClr(CCmdUI* pCmdUI) { pCmdUI->Enable( m_CMruFile.m_CurNum ); } //剪切 void CMruFileTestView::OnEditCut() { m_TextEdit.Cut(); } //复制 void CMruFileTestView::OnEditCopy() { m_TextEdit.Copy(); } //粘贴 void CMruFileTestView::OnEditPaste() { m_TextEdit.Paste(); }