📄 notebookdoc.cpp
字号:
// NoteBookDoc.cpp : implementation of the CNoteBookDoc class
//
#include "stdafx.h"
#include "NoteBook.h"
#include "NoteBookView.h"
#include "NoteBookDoc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CNoteBookDoc
IMPLEMENT_DYNCREATE(CNoteBookDoc, CDocument)
BEGIN_MESSAGE_MAP(CNoteBookDoc, CDocument)
//{{AFX_MSG_MAP(CNoteBookDoc)
ON_COMMAND(ID_APP_EXIT2, OnAppExit2)
ON_COMMAND(ID_FILE_NEW2, OnFileNew2)
ON_COMMAND(ID_FILE_OPEN2, OnFileOpen2)
ON_COMMAND(ID_FILE_SAVE2, OnFileSave2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CNoteBookDoc construction/destruction
CNoteBookDoc::CNoteBookDoc()
{
}
CNoteBookDoc::~CNoteBookDoc()
{
}
/////////////////////////////////////////////////////////////////////////////
// CNoteBookDoc serialization
void CNoteBookDoc::Serialize(CArchive& ar)
{
/////((CEditView*)m_viewList.GetHead())->SerializeRaw(ar);
}
/////////////////////////////////////////////////////////////////////////////
// CNoteBookDoc diagnostics
#ifdef _DEBUG
void CNoteBookDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CNoteBookDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CNoteBookDoc commands
BOOL CNoteBookDoc::OnNewDocument()
{
//if (!CDocument::OnNewDocument())
// return FALSE;
//////((CEditView*)m_viewList.GetHead())->SetWindowText(L"");
return TRUE;
}
BOOL CNoteBookDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
//////return CDocument::OnSaveDocument(lpszPathName);
return TRUE;
}
BOOL CNoteBookDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
//if (!CDocument::OnOpenDocument(lpszPathName))
// return FALSE;
return TRUE;
}
void CNoteBookDoc::OnCloseDocument()
{
CDocument::OnCloseDocument(); ///必须要
}
BOOL CNoteBookDoc::SaveModified()
{
/////return CDocument::SaveModified();
return TRUE;
}
void CNoteBookDoc::DeleteContents()
{
/////CDocument::DeleteContents();
}
///////////////////////////////////////////////////////////
void CNoteBookDoc::OnFileNew2()
{
CNoteBookView* pView = (CNoteBookView*)m_viewList.GetHead();
CEdit& theEdit = pView->GetEditCtrl();
if(theEdit.GetModify())
{
///////提示是否保存
if(pView->MessageBox(L"是否保存修改过的数据", NULL, MB_YESNO | MB_ICONQUESTION) == IDYES)
{
//////调用保存函数
OnFileSave2();
}
}
//////清除原来数据
theEdit.SetWindowText(L"");
pView->m_strFilePath = L"";
theEdit.SetModify(FALSE); ///设置状态为未修改
}
void CNoteBookDoc::OnFileOpen2()
{
CNoteBookView* pView = (CNoteBookView*)m_viewList.GetHead();
CEdit& theEdit = pView->GetEditCtrl();
if(theEdit.GetModify())
{
///////提示是否保存
if(pView->MessageBox(L"是否保存修改过的数据", NULL, MB_YESNO | MB_ICONQUESTION) == IDYES)
{
//////调用保存函数
OnFileSave2();
}
}
//////////打开文件对话框,将文件内容添加到编辑框里
WCHAR chFilter[] = L"Unicode文件 (*.txt)|*.txt|所有文件 (*.*)|*.*||";
CFileDialog fileDlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, chFilter);
fileDlg.m_ofn.lpstrInitialDir = L"\\hard disk\\kernel";
CString pathName;
if(fileDlg.DoModal() != IDOK)
{
return;
}
pathName = fileDlg.GetPathName();
CFile file;
if(! file.Open(pathName, CFile::modeRead))
{
AfxMessageBox(L"不能打开此文件");
return;
}
if(file.GetLength() > 128 * 1024) ///bytes
{
AfxMessageBox(L"文件过大");
return;
}
WCHAR* buffer = new WCHAR[64 * 1024 + 1]; //wchar
if(buffer == NULL)
{
AfxMessageBox(L"内存不足");
return;
}
UINT uTmp = file.Read((void*)buffer, file.GetLength());
if(uTmp != file.GetLength())
{
pView->MessageBox(L"读数据出现错误", NULL, MB_ICONERROR);
return;
}
buffer[uTmp / 2] = NULL; ////'\0'
file.Close();
theEdit.SetWindowText(buffer);
theEdit.SetModify(FALSE); ///设置状态为未修改
pView->m_strFilePath = pathName; ///保存路径
delete[] buffer;
}
void CNoteBookDoc::OnFileSave2()
{
CNoteBookView* pView = (CNoteBookView*)m_viewList.GetHead();
CEdit& theEdit = pView->GetEditCtrl();
//////先复制到缓冲区
WCHAR* buffer = new WCHAR[64 * 1024 + 1]; //wchar
if(buffer == NULL)
{
AfxMessageBox(L"内存不足");
return;
}
CString strT;
theEdit.GetWindowText(strT);
wcscpy(buffer, strT);
CFile file;
if(pView->m_strFilePath != L"")
{
if(! file.Open(pView->m_strFilePath, CFile::modeWrite))
{
AfxMessageBox(L"无法保存数据,不能打开此文件");
return;
}
file.SetLength(0); /////清文件内容
file.Write(buffer, strT.GetLength() * 2); //wchar
file.Close();
}
else
{
//////文件保存对话框
WCHAR chFilter[] = L"Unicode文件 (*.txt)|*.txt|所有文件 (*.*)|*.*||";
CFileDialog fileDlg(FALSE, L"txt", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, chFilter);
fileDlg.m_ofn.lpstrInitialDir = L"\\hard disk\\kernel";
CString pathName;
if(fileDlg.DoModal() != IDOK)
{
return;
}
pathName = fileDlg.GetPathName();
CFile file2;
if(! file2.Open(pathName, CFile::modeCreate | CFile::modeWrite))
{
AfxMessageBox(L"不能创建此文件/cannot create");
return;
}
file2.Write(buffer, strT.GetLength() * 2); //wchar
file2.Close();
pView->m_strFilePath = pathName;
}
theEdit.SetModify(FALSE); ///设置状态为未修改
delete[] buffer;
}
void CNoteBookDoc::OnAppExit2()
{
CNoteBookView* pView = (CNoteBookView*)m_viewList.GetHead();
CEdit& theEdit = pView->GetEditCtrl();
if(theEdit.GetModify())
{
OnFileSave2();
}
AfxGetMainWnd()->SendMessage(WM_CLOSE); ///发送消息关闭程序
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -