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

📄 notepadedoc.cpp

📁 该程序基于 windows ce 6.0 的模拟器CHSEmu (ARMV4I)
💻 CPP
字号:
// NotePadeDoc.cpp : implementation of the CNotePadeDoc class
//

#include "stdafx.h"
#include "NotePade.h"

#include "NotePadeDoc.h"
#include "NotePadeView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CNotePadeDoc

IMPLEMENT_DYNCREATE(CNotePadeDoc, CDocument)

BEGIN_MESSAGE_MAP(CNotePadeDoc, CDocument)
    ON_COMMAND(ID_FILE_SAVE, &CNotePadeDoc::OnFileSave)
    ON_COMMAND(ID_FILE_NEW, &CNotePadeDoc::OnFileNew)
    ON_COMMAND(ID_FILE_OPEN, &CNotePadeDoc::OnFileOpen)
    ON_COMMAND(ID_APP_EXIT, &CNotePadeDoc::OnAppExit)
    ON_COMMAND(ID_FILE_SAVE_AS, &CNotePadeDoc::OnFileSaveAs)
END_MESSAGE_MAP()

// CNotePadeDoc construction/destruction

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

CNotePadeDoc::~CNotePadeDoc()
{
}

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

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

	return TRUE;
}

// CNotePadeDoc serialization

#ifndef _WIN32_WCE_NO_ARCHIVE_SUPPORT
void CNotePadeDoc::Serialize(CArchive& ar)
{
	(ar);
}
#endif // !_WIN32_WCE_NO_ARCHIVE_SUPPORT

// CNotePadeDoc diagnostics

#ifdef _DEBUG
void CNotePadeDoc::AssertValid() const
{
	CDocument::AssertValid();
}
#endif //_DEBUG

// CNotePadeDoc commands

void CNotePadeDoc::OnFileSave()
{
    // TODO: Add your command handler code here
    CNotePadeView *pView = (CNotePadeView *)m_viewList.GetHead();
    CEdit &theEdit = pView->GetEditCtrl();

    // 先复制到缓冲区
    WCHAR *buffer = new WCHAR[64 * 1024 + 1];  // wchar

    if (buffer == NULL)
    {
        AfxMessageBox(L"Malloc Memery Error!");
        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"Can't Save Data, Can't Open This File!");
            return;
        }
        file.SetLength(0);  // 清文件内容
        file.Write(buffer, strT.GetLength() * 2);   // wchar
        file.Close();		
    }
    else
    {
        // 文件保存对话框
        WCHAR chFilter[] = L"Unicode File (*.txt)|*.txt|All Files (*.*)|*.*||";		
        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();

        if (!file.Open(pathName, CFile::modeCreate | CFile::modeWrite))
        {
            AfxMessageBox(L"Can't Create File!");
            return;	
        }
        file.Write(buffer, strT.GetLength() * 2);   // wchar
        file.Close();
        pView->m_strFilePath = pathName;
    }

    theEdit.SetModify(FALSE);  // 设置状态为未修改
    delete[] buffer;
}

void CNotePadeDoc::OnFileNew()
{
    // TODO: Add your command handler code here
    CNotePadeView *pView = (CNotePadeView *)m_viewList.GetHead();
    CEdit &theEdit = pView->GetEditCtrl();

    if (theEdit.GetModify())
    {
        // 提示是否保存
        if (pView->MessageBox(L"Do you want to save the data!", NULL, MB_YESNO | MB_ICONQUESTION) == IDYES)
        {
            // 调用保存函数
            OnFileSave();
        }		
    }
    // 清除原来数据
    theEdit.SetWindowText(L"");
    pView->m_strFilePath = L"";
    theEdit.SetModify(FALSE);   // 设置状态为未修改
}

void CNotePadeDoc::OnFileOpen()
{
    // TODO: Add your command handler code here
    CNotePadeView *pView = (CNotePadeView *)m_viewList.GetHead();
    CEdit &theEdit = pView->GetEditCtrl();

    if (theEdit.GetModify())
    {
        // 提示是否保存
        if (pView->MessageBox(L"Do you want to save the data!", NULL, MB_YESNO | MB_ICONQUESTION) == IDYES)
        {
            // 调用保存函数
            OnFileSave();
        }		
    }	
    // 打开文件对话框,将文件内容添加到编辑框里
    WCHAR chFilter[] = L"Unicode File (*.txt)|*.txt|All Files (*.*)|*.*||";
    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"Can't Open This File!");
        return;
    }
    if (file.GetLength() > 128 * 1024)  // bytes
    {
        AfxMessageBox(L"File Is Too Big!");
        return;
    }

    WCHAR *buffer = new WCHAR[64 * 1024 + 1];  // wchar
    if (buffer == NULL)
    {
        AfxMessageBox(L"Malloc Memery Error!");
        return;
    }

    UINT uTmp = file.Read((void*)buffer, file.GetLength());

    if (uTmp != file.GetLength())
    {
        pView->MessageBox(L"Read Data Error!", NULL, MB_ICONERROR);
        return;
    }
    buffer[uTmp / 2] = NULL;            // '\0'
    file.Close();
    theEdit.SetWindowText(buffer);
    theEdit.SetModify(FALSE);           // 设置状态为未修改
    pView->m_strFilePath = pathName;    // 保存路径 
    delete[] buffer;
}

void CNotePadeDoc::OnAppExit()
{
    // TODO: Add your command handler code here
    CNotePadeView *pView = (CNotePadeView *)m_viewList.GetHead();
    CEdit &theEdit = pView->GetEditCtrl();

    if (theEdit.GetModify())
    {
        OnFileSave();
    }	
    AfxGetMainWnd()->SendMessage(WM_CLOSE);  // 发送消息关闭程序
}

void CNotePadeDoc::OnFileSaveAs()
{
    // TODO: Add your command handler code here
    CNotePadeView *pView = (CNotePadeView *)m_viewList.GetHead();
    CEdit &theEdit = pView->GetEditCtrl();

    // 先复制到缓冲区
    WCHAR *buffer = new WCHAR[64 * 1024 + 1];  // wchar

    if (buffer == NULL)
    {
        AfxMessageBox(L"Malloc Memery Error!");
        return;
    }

    CString strT;

    theEdit.GetWindowText(strT);
    wcscpy(buffer, strT);

    // 文件保存对话框
    WCHAR chFilter[] = L"Unicode File (*.txt)|*.txt|All Files (*.*)|*.*||";		
    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 file;

    if (!file.Open(pathName, CFile::modeCreate | CFile::modeWrite))
    {
        AfxMessageBox(L"Can't Create File!");
        return;	
    }
    file.Write(buffer, strT.GetLength() * 2);   // wchar
    file.Close();
    pView->m_strFilePath = pathName;

    theEdit.SetModify(FALSE);  // 设置状态为未修改
    delete[] buffer;
}

⌨️ 快捷键说明

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