前言
通过C++的MFC实现两个功能
1.通过键盘输入保存到text文本文件中,
2.能够读取text文本文件的内容,并显示到窗口上
创建工程
添加消息
添加变量
CTestDoc
CTestView
编辑消息
CTestDoc
CTestDoc::CTestDoc()
CTestDoc::CTestDoc()
{
// TODO: add one-time construction code here
flag=0;
}
void CTestDoc::Serialize(CArchive& ar)
void CTestDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
//保存
ar<<str;
flag=0;
}
else
{
//读取
// TODO: add loading code here
ar>>str;
flag=1;
}
}
CTestView
CTestView::CTestView()
CTestView::CTestView()
{
// TODO: add construction code here
flag1=0;
}
void CTestView::OnDraw(CDC* pDC)
void CTestView::OnDraw(CDC* pDC)
{
CTestDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//不同类的成员
//将窗口类的成员的值传到文本类
if(flag1==0){
if(pDoc->flag==1)
str1=pDoc->str;
}else{
pDoc->str=str1;
}
pDC->TextOut(0,0,str1);
}
void CTestView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
void CTestView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
str1+=nChar;
flag1=1;
this->Invalidate();
CView::OnChar(nChar, nRepCnt, nFlags);
}
运行
保存
读取