MFC写一个简单记事本

304 阅读1分钟

这里写图片描述
这里写图片描述
这里写图片描述

void CLinjtlDlg::OnClickedOpen()
{
    // TODO: 在此添加控件通知处理程序代码
    CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"ALL Files(*.TXT)|*.TXT||",AfxGetMainWnd());
    CString strPath,strText="";
    if(dlg.DoModal()==IDOK)
    {
        strPath =dlg.GetPathName();
        m_OP.SetWindowText(strPath);
        CFile file(strPath,CFile::modeRead);
        char read[10000];
        file.Read(read,10000);
        for(int i=0;i<file.GetLength();i++)
        {
            strText+=read[i];
        }
        file.Close();
        m_Edit1.SetWindowText(strText);
    }
}


void CLinjtlDlg::OnBnClickedSave()
{
    // TODO: 在此添加控件通知处理程序代码
    CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"ALL Files(*.TXT)|*.TXT||",AfxGetMainWnd());
    CString strPath,strText="";
    char write[1000];
    if(dlg.DoModal()==IDOK)
    {
        strPath=dlg.GetPathName();
        if(strPath.Right(4)!=".txt")
            strPath+=".txt";
        m_SP.SetWindowText(strPath);
        CFile file(_T(strPath),CFile::modeCreate|CFile::modeWrite);
        m_Edit1.GetWindowText(strText);
        strcpy(write,strText);
        file.Write(write,strText.GetLength());
        file.Close();
    }