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

📄 mystdiofileview.cpp

📁 本文件中包含的是《VC编程100例》一书中的源代码
💻 CPP
字号:
// MyStdioFileView.cpp : implementation of the CMyStdioFileView class
//

#include "stdafx.h"
#include "MyStdioFile.h"

#include "MyStdioFileDoc.h"
#include "MyStdioFileView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMyStdioFileView

IMPLEMENT_DYNCREATE(CMyStdioFileView, CView)

BEGIN_MESSAGE_MAP(CMyStdioFileView, CView)
	//{{AFX_MSG_MAP(CMyStdioFileView)
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
	ON_COMMAND(ID_FILE_SAVE, OnFileSave)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyStdioFileView construction/destruction

CMyStdioFileView::CMyStdioFileView()
{
	// TODO: add construction code here

}

CMyStdioFileView::~CMyStdioFileView()
{
}

BOOL CMyStdioFileView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMyStdioFileView drawing

void CMyStdioFileView::OnDraw(CDC* pDC)
{
	CMyStdioFileDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	//要打开的文件存在且为读写标志为TRUE
	if(!m_strRPath.IsEmpty() && m_bWR)
	{
		CString str;				//存储读出文件内容
		int nRow = 0;				//控制打印输出的行位置
		
		//以只读方式打开文件
		CStdioFile myFile(m_strRPath, CFile::modeRead);
		for( ; ; )
		{
			//文件没有结束
			if(myFile.ReadString(str) == TRUE)
			{
				pDC->TextOut(0, nRow, str);
				nRow += 20;
			}
			//文件结束
			else 
				break;
		}
	}

	//要写入的文件名存在且读写标志为FALSE
	if(!m_strWPath.IsEmpty() && !m_bWR)
	{
		//原始文件为空
		if(m_strRPath.IsEmpty())
		{
			pDC->TextOut(0, 0, "您将文件 " + m_strWPath + " 置空了");
			CStdioFile myFile(m_strWPath, CFile::modeCreate);
		}
		//原始文件非空
		else
		{
			CString str;
			//读取原文件,写入新文件
			CStdioFile myFileRead(m_strRPath, CFile::modeRead);
			CStdioFile myFileWrite(m_strWPath, CFile::modeWrite | CFile::modeCreate);
			for( ; ; )
			{
				//原文件没有结束则读取一行
				if(myFileRead.ReadString(str) == TRUE)
				{
					//加换行符后写入新文件
					str += "\n";
					myFileWrite.WriteString(str);
				}
				//原文件结束
				else
					break;
			}
			//另存为消息的结束提示
			if(m_strRPath != m_strWPath)
				pDC->TextOut(0, 0, m_strRPath + " 已经另存为 " + m_strWPath);
			//保存消息的提示
			if(m_strRPath == m_strWPath)
				pDC->TextOut(0, 0, "文件" + m_strRPath + " 已经保存");
		}
	}
} 

/////////////////////////////////////////////////////////////////////////////
// CMyStdioFileView diagnostics

#ifdef _DEBUG
void CMyStdioFileView::AssertValid() const
{
	CView::AssertValid();
}

void CMyStdioFileView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CMyStdioFileDoc* CMyStdioFileView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyStdioFileDoc)));
	return (CMyStdioFileDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMyStdioFileView message handlers

//打开文件的处理
void CMyStdioFileView::OnFileOpen() 
{
	// TODO: Add your command handler code here
	//打开文件对话框
	CFileDialog dlg(TRUE, ".txt", "test.txt", OFN_HIDEREADONLY, "Txt files (*.txt)|*.txt|Header files (*.h)|*.h|C++ files (*.cpp)|*.cpp|All Files (*.*)|*.*||", NULL);
	if(dlg.DoModal() == IDOK)
	{
		m_strRPath = dlg.GetFileName();
		m_bWR = TRUE;
	}
	OnInitialUpdate();
}
//另存为消息的处理函数
void CMyStdioFileView::OnFileSaveAs() 
{
	// TODO: Add your command handler code here
	//另存为对话框
	CFileDialog dlg(FALSE, ".txt", "test.txt", OFN_HIDEREADONLY, "Txt files (*.txt)|*.txt|Header files (*.h)|*.h|C++ files (*.cpp)|*.cpp|All Files (*.*)|*.*||", NULL);
	if(dlg.DoModal() == IDOK)
	{
		m_strWPath = dlg.GetFileName();
		m_bWR = FALSE;
	}
	OnInitialUpdate();
}

void CMyStdioFileView::OnFileSave() 
{
	// TODO: Add your command handler code here
	//原文件不存在,同Save as处理
	if(m_strRPath.IsEmpty())
		OnFileSaveAs();
	//原文件存在
	else
	{
		CMyStdioFileDoc *dc = GetDocument();
		m_strWPath = dc->GetPathName();
		m_bWR = FALSE;
	}
}

⌨️ 快捷键说明

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