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

📄 myfileview.cpp

📁 这个是我们学校用的VC++教案
💻 CPP
字号:
// MyFileView.cpp : implementation of the CMyFileView class
//

#include "stdafx.h"
#include "MyFile.h"

#include "MyFileDoc.h"
#include "MyFileView.h"



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

/////////////////////////////////////////////////////////////////////////////
// CMyFileView

IMPLEMENT_DYNCREATE(CMyFileView, CView)

BEGIN_MESSAGE_MAP(CMyFileView, CView)
	//{{AFX_MSG_MAP(CMyFileView)
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyFileView construction/destruction

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

}

CMyFileView::~CMyFileView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMyFileView drawing

void CMyFileView::OnDraw(CDC* pDC)
{
	CMyFileDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CMyFileView printing

BOOL CMyFileView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CMyFileView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CMyFileView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CMyFileView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMyFileView message handlers
#include "FileDlg.h"
#include "fstream.h"
void CMyFileView::OnFileOpen() 
{
	CString  strFile,  strFileR4;
	CString  strMessage;
	fstream  f;
	CFileDlg  dlg;
	if(dlg.DoModal()==IDOK)  //输入要打开的文件名,包括后缀
	{
		TRY
		{
			strFile=dlg.m_filename;  // 得到要打开的文件名
			strFileR4=strFile.Right(4);
			strFileR4.MakeUpper();
			if(strFileR4.Compare(".TXT")!=0)  // 引发异常
				AfxThrowFileException(CFileException::generic);
			f.open(strFile,ios::in|ios::nocreate);
			if(!f.good())
				AfxThrowFileException(CFileException::fileNotFound);
			MessageBox("Open successfully !");
		}
		CATCH(CFileException,pe)
		{
			switch(pe->m_cause)
			{
			 case  CFileException::generic:
				strMessage=strFile+" file is a non_TXT file !";  // 文件类型不正确
				break;
			 case  CFileException::fileNotFound:
				strMessage=strFile+" file isn't found !";  // 文件不存在
				break;
			 default:
				strMessage="Error of not captured !";  // 未捕获的文件错误
			}
			MessageBox(strMessage,"File Exception");
		}
		END_CATCH
		f.close();
	}
}

⌨️ 快捷键说明

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