mfc课程设计报告.docx
《mfc课程设计报告.docx》由会员分享,可在线阅读,更多相关《mfc课程设计报告.docx(22页珍藏版)》请在冰豆网上搜索。
mfc课程设计报告
课程设计报告
课题:
基于MFC学生成绩管理系统
院系:
数学与计算机学院
专业:
软件工程1203
***********************************
学号:
**********
日期:
2014.12.20
学生信息管理系统
一、设计任务与目标
1.了解MFC应用向导所生成应用程序的基本结构;
2.掌握对话框编程的一般方法;
3.学习用类向导添加消息响应函数及控件关联变量;
4.掌握Windows通用控件使用方法;
5.了解Windows通用对话框编程的使用方法
二、方案设计与论证
项目名称:
student,选择对话框应用程序框架。
本程序中管理的学生信息包括:
学号、姓名、性别、专业、年级、出生年月。
在第一个对话框里使用控件工具创建控件完成登录界面
1.设置对话框IDD_STUDENT_DIALOG
调出MFCWizard在menbervariables设置文本框的类型与成员名称,实现消息映射,实现登录过程中用户名和密码的输入,在创建的button中添加代码实现登录功能。
2.设置对话框IDD_DIALOG1
在对话框中添加四个按钮,分别实现调用对话框实现学生信息的录入、查询、删除和退出程序
3.设置对话框IDD_INFOR_DIALOG1
在对话框中添加控件,调出MFCWizard在menbervariables设置文本框的类型与成员名称其他对话框的设置也类似。
4.对话框之间使用模态对话框,先将个对话框之间实现互相调用。
三、程序框图或流程图,程序清单与调用关系
四、全部源程序清单
1.登录模块
在登录对话框的按钮中添加代码
头文件
#include"stdafx.h"
#include"Student.h"
#include"StudentDlg.h"
#include"infor.h"
#include//文件
voidCStudentDlg:
:
OnOK()
{
//TODO:
Addextravalidationhere
UpdateData(TRUE);
charname[50];
charpwd[50];
CStringaa;
ifstreammf("password.txt");//打开保存密码的文件
mf.getline(name,sizeof(name));//获取
mf.getline(pwd,sizeof(pwd));
aa=name;
if(m_name.IsEmpty())//假如名字为空弹出窗口
{
MessageBox("用户名不能为空,请输入用户名!
","注意");
return;
}
elseif(m_password.IsEmpty())
{
AfxMessageBox("密码不能为空,请输入密码!
");
return;
}
ifstreamf("password.txt");
while(!
f.eof())//直到0结束
{//for(inti=1;i<30;i++)
//{
f.getline(name,sizeof(name));
f.getline(pwd,sizeof(pwd))
if(m_name==name&&m_password==pwd)//假如用户名密码正确,showwindow
{
UpdateData(FALSE);
ofstreammf("password.txt");
mf<m_jdt.ShowWindow(SW_SHOW);
mf.close();
for(intkk=0;kk<100;kk++)
{
m_jdt.SetPos(kk);
:
:
Sleep(20);
}
EndDialog
(1);//模态对话框的调用
infora;//infor是主菜单
a.DoModal();
return;
}
if(m_name==name&&m_password!
=pwd)
{
AfxMessageBox("密码错误!
");
return;
}
//}
}
if(m_name!
=name&&m_password!
=pwd)
AfxMessageBox("用户信息不正确,登陆失败!
");//弹出错误提示框
UpdateData(FALSE);
}
2.主菜单模块
voidinfor:
:
OnButton1()//点击按钮进入录入对话框
{
//TODO:
Addyourcontrolnotificationhandlercodehere
EndDialog
(1);//模态对话框的调用
Cluruaa;
aa.DoModal();
return;
}
voidinfor:
:
OnButton2()//点击按钮退出
{
//TODO:
Addyourcontrolnotificationhandlercodehere
EndDialog
(1);
}
//点击按钮进入删除对话框
voidinfor:
:
OnButton3()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
EndDialog
(1);
Cdeletecc;
cc.DoModal();
return;
}
点击按钮进入查询对话框
voidinfor:
:
OnButton4()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
EndDialog
(1);
Cxiugaidd;
dd.DoModal();
return;
}
3.录入模块
在class中建立一个基本类write
classwrite:
publicCluru
{
public:
write();
virtual~write();
};
然后写下边的代码
write:
:
write()
{
CStud*stu;
stu->m_strName=m_sName;
stu->m_strMajor=m_sMajor;
stu->m_strGrade=m_sGrade;
stu->m_strDate=m_sBirthday;
stringtemp;
if(m_cMale.GetCheck())
temp="男";
else
temp="女";
ofstreamou;///是从内存到硬盘
ou.open("record.txt",ios:
:
app);//选择附加模式。
以追加的方式打开文件
ou<ou<m_strID;
ou<m_strName;
if(m_cMale.GetCheck())
temp="男";
else
temp="女";
ou<ou<m_strMajor;
ou<m_strGrade;
ou<m_strDate;
ou<ou.close();
}
write:
:
~write(){}
录入按钮的功能代码
voidCluru:
:
OnButton1()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
EndDialog
(1);
inforz;
z.DoModal();
return;
}
voidCluru:
:
OnOK()
{
//TODO:
Addextravalidationhere
UpdateData();
if(m_sNum==""||m_sName==""||m_sMajor==""||m_sGrade==""||
m_sBirthday==""||(!
m_cMale.GetCheck()&&!
m_cFemale.GetCheck())){
MessageBox("学生信息不完整,无法录入!
","错误");
return;
}
//查询学号是否存在
intflag=0;
CStud*stu=newCStud;
stu->m_strID=m_sNum;
stringtemp;
ifstreamin("record.txt");
while(in>>temp){
if(temp==stu->m_strID){
flag=1;
MessageBox("该学号已存在!
","注意");
in.close();
m_sNum="";
UpdateData(false);//只将学号清空,其余信息保留
return;
}
}
in.close();
//说明该学生信息还没存档,以下将其信息存档
write(stu);
deletestu;
MessageBox("录入完毕!
","成功");
clean();//清空表格
//TODO:
Addextravalidationhere
//CDialog:
:
OnOK();
}
voidCluru:
:
clean()
{
m_sNum="";
m_sName="";
m_sMajor="";
m_sGrade="";
m_sBirthday="";
m_cMale.SetCheck(0);
m_cFemale.SetCheck(0);
UpdateData(false);
}
voidCluru:
:
write(CStud*stu)
{
stu->m_strName=m_sName;
stu->m_strMajor=m_sMajor;
stu->m_strGrade=m_sGrade;
stu->m_strDate=m_sBirthday;
stringtemp;
if(m_cMale.GetCheck())
temp="男";
else
temp="女";
ofstreamou;
ou.open("record.txt",ios:
:
app);//选择附加模式
ou<ou<m_strID;
ou<m_strName;
if(m_cMale.GetCheck())
temp="男";
else
temp="女";
ou<ou<m_strMajor;
ou<m_strGrade;
ou<m_strDate;
ou<ou.close();
}
查询模块
voidCxiugai:
:
OnButton1()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
UpdateData();
m_sName="";
m_sMajor="";
m_sGrade="";
m_sBirthday="";
m_cMale.SetCheck(0);
m_cFemale.SetCheck(0);
UpdateData(false);//清空上次查找记录,只保留现在输入的学号
if(m_sNum==""){
MessageBox("学号为空,无法查询!
","错误");
return;
}
ifstreamin("record.txt");
stringtemp,str;
intflag=0;
while(in>>temp){
str=m_sNum;
if(temp==str){
flag=1;//说明找到了
in>>temp;
m_sName=temp.c_str();//类型转换
in>>temp;
if(temp=="男")
m_cMale.SetCheck
(1);
else
m_cFemale.SetCheck
(1);
in>>temp;
m_sMajor=temp.c_str();
in>>temp;
m_sGrade=temp.c_str();
in>>temp;
m_sBirthday=temp.c_str();
UpdateData(false);
in.close();
break;
}
in.ignore(1024,'\n');
}
in.close();
if(!
flag){
MessageBox("该学生信息不存在!
","注意");
m_sNum="";
UpdateData(false);
}
删除模块
voidCdelete:
:
OnButton1()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
find=false;//该变量表示是否找到
searched=true;//该变量表示是否进行过查询
UpdateData();
clean();//清空上次查询结果,仅保留现在输入的学号
if(m_sNum=="")
{
MessageBox("学号为空,无法查询!
","错误");
searched=false;
return;
}
index=0;//表示要查找的学生信息在哪一行,作为删除的索引
ifstreamin("record.txt");
stringtemp,str;
while(in>>temp)
{
index++;
str=m_sNum;
if(str==temp)
{
find=true;//找到了
in>>temp;
m_sName=temp.c_str();
in>>temp;
if(temp=="男")
m_cMale.SetCheck
(1);
else
m_cFemale.SetCheck
(1);
in>>temp;
m_sMajor=temp.c_str();
in>>temp;
m_sGrade=temp.c_str();
in>>temp;
m_sBirthday=temp.c_str();
UpdateData(false);
in.close();
break;
}
in.ignore(1024,'\n');
}
in.close();
if(!
find){
MessageBox("该学生信息不存在!
","注意");
m_sNum="";
UpdateData(false);
searched=false;
}
voidCdelete:
:
clean()
{
m_sMajor="";
m_sGrade="";
m_sBirthday="";
m_cMale.SetCheck(0);
m_cFemale.SetCheck(0);
UpdateData(false);
}
背景添加
引入位图,为对话框添加一个变量用来设定对话框背景图片
在对话框类中加入代码
CBitmap*pBitmap=newCBitmap;
ASSERT(pBitmap);
pBitmap->LoadBitmap(引入位图的ID);
m_Brush.CreatePatternBrush(pBitmap);
为对话框添加映射WM_CTLCOLOR消息。
并加入代码
HBRUSHCluru:
:
OnCtlColor(CDC*pDC,CWnd*pWnd,UINTnCtlColor)
{
HBRUSHhbr=CDialog:
:
OnCtlColor(pDC,pWnd,nCtlColor);
if(nCtlColor==CTLCOLOR_DLG)
return(HBRUSH)m_Brush.GetSafeHandle();
returnCDialog:
:
OnCtlColor(pDC,pWnd,nCtlColor);
}
五、程序运行的测试与分析
登陆界面,实现登录。
主菜单
信息录入界面,能够实现信息的录入
查询界面,能够实现查询功能
删除界面,能够实现信息的删除
六、结论与心得
通过做这个系统,我巩固和运用了很多课上学过的知识,也学会了很多课上没有讲过的知识,感受到了解课外知识的重要性,可谓是受益匪浅。
调试过程中经常出现错误,刚开始检查了代码好几遍发现没有问题,后来才知道这与头文件有关,这期间也用了很长的时间。
在文本框的数据输入中也碰见了问题,不知道数据怎么传递,后来文全刚老师帮我理了一下思路。
是在对话框的MFCclassWizard的menbervariable中为各个框添加类型和成员名称,实现消息的映射。
进入界面设计。
其中又遇到了很多困难。
在插入背景图时,使用,这是我不知道的,通过参考书和讨论终于完成这一步。
从中,我更加坚定了讨论的重要作用。
其次,代码部分很多很复杂,但是结构确是很清晰的。
我想这应该是MFC的一个很大的优点。
根据参考书上的讲解和自己的揣摩,我明白了整个算法的灵魂。
编程很多时候要用到函数,但由于上课时学的知识很多没有认真去体会,就要花时间去找那个函数,或者说自己写代码来实现那个函数的功能。
所以,从现在开始,我会认真面对每一次编程,即使是小程序,也一样能锻炼人。
这个系统还有很多可以改进的地方,例如添加修改密码功能,添加注册功能,添加修改功能。
所以在以后我还会继续完善这个程序。
把它做得更好。