当前位置:网站首页>MFC writes a suggested text editor

MFC writes a suggested text editor

2022-06-26 07:50:00 Don't hit me, it hurts!

1. Use ANSI Character set

2. open , preservation , Of the exit button ID Not for IDopen,IDsave,IDcancel

3. Create a variable for the text edit box m_Edit, Category is Value, The variable type is CString.


-------------------- Open the handler of the button

void CeditDlg::OnBnClickedopen()
{
// TODO: Add control notification handler code here
int i=0;
char s[10000];
char szFilter[]=" text file (*.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(" The reason why reading data failed is :%d",e->m_cause);
MessageBox(str);
fileOpen.Abort();
e->Delete();
}
}
CString str(s,i);
m_Edit1=str;
UpdateData(false);
}


--------------------------------------- Save button handler

void CeditDlg::OnBnClickedsave()
{
// TODO: Add control notification handler code here
UpdateData();
char szFilter[]=" text file (*.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(" The reason why saving data failed is :%d",e->m_cause);
MessageBox(str);
fileSave.Abort();
e->Delete();


}
}
}



---------------------------------------------- The exit button uses the default function

void CeditDlg::OnBnClickedCancel()
{
// TODO: Add control notification handler code here
CDialogEx::OnCancel();
}

原网站

版权声明
本文为[Don't hit me, it hurts!]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170609120508.html