MFC编写一个建议的文本编辑器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37827702/article/details/77987503

1.使用ANSI字符集

2.打开,保存,退出按钮的ID非别为IDopen,IDsave,IDcancel

3.为文本编辑框创建变量m_Edit,类别为Value,变量类型为CString。


--------------------打开按钮的处理函数

void CeditDlg::OnBnClickedopen()
{
// TODO: 在此添加控件通知处理程序代码
int i=0;
char s[10000];
char szFilter[]="文本文件(*.txt)|*.txt|All File(*,*)|*,*||";
CFileDialog OpenDlg(true,"*.txt",0,0,szFilter);
int x=OpenDlg.DoModal();
if(x==IDOK)
{
CFile fileOpen;
try{
fileOpen.Open(OpenDlg.GetPathName(),CFile::modeRead);
i=fileOpen.GetLength();
fileOpen.Read(s,i);
fileOpen.Close();
}catch(CFileException *e){
CString str;
str.Format("读取数据失败的原因是:%d",e->m_cause);
MessageBox(str);
fileOpen.Abort();
e->Delete();
}
}
CString str(s,i);
m_Edit1=str;
UpdateData(false);
}


---------------------------------------保存按钮的处理函数

void CeditDlg::OnBnClickedsave()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData();
char szFilter[]="文本文件(*.txt)|*.txt|All File(*,*)|*,*||";
CFileDialog SaveDlg(false,"*.txt",0,0,szFilter);
int x=SaveDlg.DoModal();
if(x==IDOK)
{
CFile fileSave;
try{
fileSave.Open(SaveDlg.GetPathName(),CFile::modeCreate|CFile::modeWrite);
fileSave.Write(m_Edit1,m_Edit1.GetLength());
fileSave.Close();

}catch(CFileException *e){
CString str;
str.Format("保存数据失败的原因是:%d",e->m_cause);
MessageBox(str);
fileSave.Abort();
e->Delete();


}
}
}



----------------------------------------------退出按钮使用默认的函数即可

void CeditDlg::OnBnClickedCancel()
{
// TODO: 在此添加控件通知处理程序代码
CDialogEx::OnCancel();
}

猜你喜欢

转载自blog.csdn.net/m0_37827702/article/details/77987503
今日推荐