⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 minieditdoc.cpp

📁 c++从入门到精通
💻 CPP
字号:
// miniEditDoc.cpp : implementation of the CMiniEditDoc class
//

#include "stdafx.h"
#include "miniEdit.h"
#include "miniEditDoc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMiniEditDoc

IMPLEMENT_DYNCREATE(CMiniEditDoc, CDocument)

BEGIN_MESSAGE_MAP(CMiniEditDoc, CDocument)
	//{{AFX_MSG_MAP(CMiniEditDoc)
	ON_COMMAND(ID_FILE_NEW, OnFileNew)
	ON_COMMAND(ID_FILE_SAVE, OnFileSave)
	ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMiniEditDoc construction/destruction

CMiniEditDoc::CMiniEditDoc()
{
	// TODO: add one-time construction code here

}

CMiniEditDoc::~CMiniEditDoc()
{
}

BOOL CMiniEditDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CMiniEditDoc serialization

void CMiniEditDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CMiniEditDoc diagnostics

#ifdef _DEBUG
void CMiniEditDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CMiniEditDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMiniEditDoc commands

void CMiniEditDoc::OnFileNew() 
{
	// TODO: Add your command handler code here
	if(!strList.IsEmpty())
	{
		if(AfxMessageBox(" 是否保存当前的文件",MB_YESNO)==IDYES)
			OnFileSave();
		strFilePath="";
	}
	if(!strList.IsEmpty())
		strList.RemoveAll();
}

void CMiniEditDoc::OnFileOpen() 
{
	if(!strList.IsEmpty())//如果字符串链表strList不空,则置空链表
		strList.RemoveAll();
	// TODO: Add your command handler code here
	CFileDialog fdlg( TRUE, NULL, NULL, //创建一个文件打开对话框
		OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, 
		_T("TXT Files(*.txt)|*.txt||"), NULL );
	if(fdlg.DoModal()==IDOK)
	{
		strFilePath = fdlg.GetPathName();
		CStdioFile cf(strFilePath,CFile::modeRead);
		char buf[256];//定义一个字符缓冲区,用来存放从文件读出的字符
		int i=0;
		CString str;
		while(cf.ReadString(buf, 256)!=NULL)
		{
			str=CString(buf);
			str.Replace("\t","    ");
			str.Remove('\n');
			strList.AddTail(str);
			i++;
		}
		iLineCount=i;//*******CDocument's member*****
		UpdateAllViews(NULL);
	}
}

void CMiniEditDoc::OnFileSave() 
{
	// TODO: Add your command handler code here
	if((strFilePath=="")&&(!strList.IsEmpty()))
		OnFileSaveAs();
	else
	{
		CStdioFile cf(strFilePath,CFile::modeWrite);
		POSITION pos=strList.GetHeadPosition();
		CString str;
		int l=strList.GetCount();
		for(int i=0;i<l;i++)
		{
			str=strList.GetNext(pos);
			str+="\n";
			cf.WriteString(str);
		}
		strList.RemoveAll();
	}
}

void CMiniEditDoc::OnFileSaveAs() 
{
	// TODO: Add your command handler code here
	CFileDialog fdlg( FALSE, NULL, NULL, //创建一个文件另存为对话框
		OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, 
		_T("TXT Files(*.txt)|*.txt||"), NULL );
	//如果字符串链表strList不空,则置空链表
	if(fdlg.DoModal()==IDOK)
	{
		strFilePath = fdlg.GetPathName();
		CStdioFile cf(strFilePath,CFile::modeWrite|CFile::modeCreate);
		//定义一个字符缓冲区,用来存放从文件读出的字符
		POSITION pos=strList.GetHeadPosition();
		CString str;
		int l=strList.GetCount();
		for(int i=0;i<l;i++)
		{
			str=strList.GetNext(pos);
			str+="\n";
			cf.WriteString(str);
		}
	}
}

void CMiniEditDoc::FileNew()
{
	OnFileNew();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -