写在前面
今天是软件实习的第一天,老师布置了一个小作业,利用老古董MFC写一个小程序,说是想看看咱们的水平。好,
不给点颜色看看,还真以为我很厉害了(手动狗头保命) 对于大佬而言,这个作业信手拈来;但对于我这种0基础渣渣,直接原地芜湖起飞。还好还好有学长大神的引导,我也快要摸到MFC的门了,也算是有点小收获吧,想着记录下来。
小程序运行录屏
演示一下下
三段代码(A&&B)
打开文件
A
void Cdemo2Dlg::OnClickedPathS()
{
CFileDialog mFileDlg(TRUE, NULL, NULL,
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT,
_T("All Files (*.*)|*.*||"), AfxGetMainWnd());
if (mFileDlg.DoModal() != IDCANCEL)
{
m_sPath = mFileDlg.GetPathName();
if (m_sPath != "") {
GetDlgItem(IDC_ADDHW)->EnableWindow(true);
GetDlgItem(IDC_ADDSWJTU)->EnableWindow(true);
}
else {
GetDlgItem(IDC_ADDHW)->EnableWindow(false);
GetDlgItem(IDC_ADDSWJTU)->EnableWindow(false);
}
UpdateData(false);
}
}
B
void CMFC0701Dlg::OnBnClickedButton1()
{
CFileDialog filePicker(TRUE);
if (filePicker.DoModal() == IDOK)
{
CString filePath = filePicker.GetPathName();
Edit.SetWindowTextW(filePath);
}
}
“hello world” “hello 交大”
A--hello world
void Cdemo2Dlg::OnClickedAddhw()
{
CFile WriteFile;
WriteFile.Open(m_sPath, CFile::modeWrite | CFile::modeRead | CFile::modeCreate);
WriteFile.SeekToEnd();
WriteFile.Write("Hello World", strlen("Hello World"));
WriteFile.Close();
MessageBox(_T("恭喜你,写入成功!"), _T("提示"), MB_OK);
}
A--hello 交大
void Cdemo2Dlg::OnClickedAddswjtu()
{
CFile WriteFile;
WriteFile.Open(m_sPath, CFile::modeWrite | CFile::modeRead | CFile::modeCreate | CFile::modeNoTruncate);
WriteFile.SeekToEnd();
WriteFile.Write("Hello 交大", strlen("Hello 交大"));
WriteFile.Close();
MessageBox(_T("恭喜你,字符追加成功!"), _T("提示"), MB_OK);
}
B
void CMFC0701Dlg::AppendToFile(CString str)
{
CString path;
Edit.GetWindowTextW(path);
std::wofstream f(path, std::ios::app);
if (!f.is_open()) {
MessageBox(L"the file doesn't exit", L"Error");
return;
}
f.imbue(std::locale("chs"));
f << str.GetString() << std::endl;
f.close();
}
void CMFC0701Dlg::OnBnClickedButton2()
{
AppendToFile(L"hello world");
MessageBox(L"hello world已写入", L"提示");
}
void CMFC0701Dlg::OnBnClickedButton3()
{
AppendToFile(L"hello 交大");
MessageBox(L"hello 交大 已写入", L"提示");
}
写在后面
感谢看到这儿的大伙们!!!
写下这篇博客纯粹是为了记录自己在计算机学习中的成长点滴,以后可以翻来看看自己的学习过程和经历,不失为一件好事情。同时本人也热衷于写博客写小作文,对markdown十分感兴趣,因此拿来练练手。
再次感谢拉到这儿的松弟集美们!!