ex010206doc.cpp

来自「深入浅出Visual C++入门进阶与应用实例 随书光盘 作者 何志丹」· C++ 代码 · 共 178 行

CPP
178
字号
// Ex010206Doc.cpp : implementation of the CEx010206Doc class
//

#include "stdafx.h"
#include "Ex010206.h"

#include "Ex010206Doc.h"
#include "CntrItem.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CEx010206Doc

IMPLEMENT_DYNCREATE(CEx010206Doc, CRichEditDoc)

BEGIN_MESSAGE_MAP(CEx010206Doc, CRichEditDoc)
	//{{AFX_MSG_MAP(CEx010206Doc)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
	// Enable default OLE container implementation
	ON_UPDATE_COMMAND_UI(ID_OLE_EDIT_LINKS, CRichEditDoc::OnUpdateEditLinksMenu)
	ON_COMMAND(ID_OLE_EDIT_LINKS, CRichEditDoc::OnEditLinks)
	ON_UPDATE_COMMAND_UI_RANGE(ID_OLE_VERB_FIRST, ID_OLE_VERB_LAST, CRichEditDoc::OnUpdateObjectVerbMenu)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEx010206Doc construction/destruction

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

}

CEx010206Doc::~CEx010206Doc()
{
}

BOOL CEx010206Doc::OnNewDocument()
{
	if (!CRichEditDoc::OnNewDocument())
		return FALSE;

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

	return TRUE;
}

CRichEditCntrItem* CEx010206Doc::CreateClientItem(REOBJECT* preo) const
{
	// cast away constness of this
	return new CEx010206CntrItem(preo, (CEx010206Doc*) this);
}



/////////////////////////////////////////////////////////////////////////////
// CEx010206Doc serialization

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

	// Calling the base class CRichEditDoc enables serialization
	//  of the container document's COleClientItem objects.
	// TODO: set CRichEditDoc::m_bRTF = FALSE if you are serializing as text
	CRichEditDoc::Serialize(ar);
}

/////////////////////////////////////////////////////////////////////////////
// CEx010206Doc diagnostics

#ifdef _DEBUG
void CEx010206Doc::AssertValid() const
{
	CRichEditDoc::AssertValid();
}

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

/////////////////////////////////////////////////////////////////////////////
// CEx010206Doc commands

BOOL CEx010206Doc::OnOpenDocument(LPCTSTR lpszPathName) 
{
	if (!CRichEditDoc::OnOpenDocument(lpszPathName))
		return FALSE;
	
	bool bRet = TRUE ;//记录返回值
	CRichEditView *pView = NULL ;
	char * pfileContent = NULL ;
	//读取文件内容
	CFile file ;
	try
	{
		file.Open(lpszPathName,CFile::modeRead);
		pfileContent = new char[file.GetLength() + 1 ] ;//动态数组
		memset(pfileContent, 0 , file.GetLength() + 1 );
		file.ReadHuge(pfileContent,file.GetLength());
		file.Close();
	}
	catch(CFileException * e)
	{		
		ASSERT(false);
		e->m_cause ;
		bRet = FALSE ;
		goto end ;
	}
	
	pView = GetView();
	if(NULL == pView)
	{
		bRet = FALSE ;
		goto end ;
	}	
	{//不以在goto之后定义变量,所以只好加一个大括号
		CRichEditCtrl& edit   = pView->GetRichEditCtrl();	
		edit.SetWindowText(pfileContent);
		//设置为未修改,避免关闭的时候弹出提示保存对话框
		SetModifiedFlag(false);
		goto end ;
	}
	
end:
	if(NULL != pfileContent)
	{
		delete [] pfileContent ;
		pfileContent = NULL ;
	}	
	return bRet;
}

BOOL CEx010206Doc::OnSaveDocument(LPCTSTR lpszPathName) 
{
	//取得多功能编辑框的内容
	CString strText ;
	CRichEditView *pView = GetView();
	if(NULL == pView)
		return false ;
	CRichEditCtrl &edit = pView->GetRichEditCtrl();	
	edit.GetWindowText(strText);
	
	//将内容存进文件
	CFile file ;
	try
	{
		file.Open(lpszPathName,CFile::modeCreate|CFile::modeWrite);
		file.WriteHuge((LPCTSTR)strText,strText.GetLength());
		file.Close();
	}
	catch(CFileException * e)
	{
		ASSERT(false);
		e->m_cause ;
		return false ;
	}
	
	return CRichEditDoc::OnSaveDocument(lpszPathName);
}

⌨️ 快捷键说明

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