Windows程序课程设计报告_windows课程设计报告
Windows程序课程设计报告由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“windows课程设计报告”。
window程序课程设计
简单的学生信息 管理应用程序
学生姓名: 韩 慧 学生学号: 031140312 班 级: 031140--3
二〇一三年十二月
简单的学生信息管理应用程序
一、设计目的、内容及要求 1.1设计目的1.了解Windows编程的基础知识,掌握MFC应用程序的基本知识;2.基本掌握面向对象程序设计的基本思路和方法; 3.掌握用VC++开发应用程序的的一般步骤和方法;
1.2设计内容
利用所学的基本知识, 设计一个简单的学生信息管理应用程序,具有以下功能:①实现学生信息的存储,可以用文件来实现。②编程实现学生信息的添加、删除、修改、浏览和查询等功能。
1.3设计要求
1.用VC++进行编码,实现应用程序的功能。注重编码质量,代码要有适当的注释;
2.采用单文档多视图方式,左边为树视图,右边为列表视图,左边一级节点为中原工学院,二级节点为计算机学院和软件学院,三级节点为计算机学院的三个专业。要求可以动态增加节点。楼便为列表视图,包含学生的学号、性别、姓名各科成绩等。右边也可以实现动态增加3.当选中节点时,显示对应的学生信息。如当选中”中原工学院”时选中所用的学生信息,选中“计算机学院”时,显示所有计算机学院学生信息 4.采用序列化方式保存和读取学生信息
二、主要的数据结构
1.数组集合类: CObArray m_stuObArray;2.树:
3.序列化类数据
三、基本思路
基于树视图类建立单文档应用程序,插入列表视图类,切分窗口,左边为树视图,右边为列表视图。插入学生信息对话框,创建序列化类,并利用数组集合类储存学生信息。在主菜单中添加添加、修改、删除、查询菜单,并在文 3 档类中一一添加其映射函数。在列表视图类写入输出函数,在树视图类中添加click映射函数,调用列表试图中的输出函数,是两个视图之间建立关系。
四、步骤及主要代码
4.1设计并创建切分窗口完善视图
1.用MFC AppWizard(exe)创建一个单文档应用程序Student。在第六步中将视图类的基类选择为CtreeView。
2.用MFC ClaWizard添加一个新的ClistView的派生类ClistView。打开CmainFrame.h文件,为CmainFrame类添加下列变量成员: public: CSplitterWnd m_wndSplitter;3.用MFC ClaWizard为CMainFrame类添加OnCreatClient函数,并添加下列代码:
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext){ m_wndSplitter.CreateStatic(this,1,2);m_wndSplitter.SetRowInfo(0,400,0);m_wndSplitter.CreateView(0,0,RUNTIME_CLASS(CStudentView),CSize(400,600),pContext);m_wndSplitter.CreateView(0,1,RUNTIME_CLASS(ClistView),CSize(400,600),pContext);return TRUE;} 4.在MainFrm.cpp的前面添加下列语句: #include “MainFrm.h” #include “StudentView.h” #include “listView.h” 在StudentView.cpp的前面添加下列下列语句: #include “StudentDoc.h” 4 #include “StudentView.h” #include “listView.h” 在StudentDoc.cpp的前面添加: #include “StudentDoc.h” #include “listView.h” #include “StudentView.h” 打开StudentView.h文件,在cla CStudentView : public CtreeView语句前面添加: cla CStudentDoc;cla CStudentView : public CTreeView 5.为CstudentView添加void类型函数creatTree()函数,并添加下列代码
void CStudentView::creatTree(){ CTreeCtrl& m_TreeCtrl = GetTreeCtrl();m_TreeCtrl.DeleteAllItems();HTREEITEM hRoot,hParent,hChild,pS;hRoot=m_TreeCtrl.InsertItem(“中原工学院”,0,1);hParent=m_TreeCtrl.InsertItem(“计算机学院”,hRoot);m_TreeCtrl.InsertItem(“网络工程”,hParent);
m_TreeCtrl.InsertItem(“软件工程”,hParent);m_TreeCtrl.InsertItem(“计算机科学”,hParent);hParent=m_TreeCtrl.InsertItem(“软件学院”,hRoot);BOOL flag=TRUE;CStudentDoc *doc=GetDocument();int nIndex=doc->m_stuObArray.GetSize();for(int n=0;n
CStudentInfo *stu=(CStudentInfo*)doc->m_stuObArray.GetAt(n);
CString str1=stu->strSpecial;
CString str2=stu->strAcademy;
if(!m_TreeCtrl.ItemHasChildren(hRoot))//判断根节点是否有子节点
{
hParent=m_TreeCtrl.InsertItem(str2,hRoot);
m_TreeCtrl.InsertItem(str1,0,1,hParent);5
} else {
hParent=m_TreeCtrl.GetChildItem(hRoot);
pS=hParent;
for(;pS!=NULL;pS=m_TreeCtrl.GetNextItem(pS,TVGN_NEXT))
{
CString strItem1=m_TreeCtrl.GetItemText(pS);
if(strItem1==str2)flag=FALSE;
}
if(flag)
{
hParent=m_TreeCtrl.InsertItem(str2,hRoot);
m_TreeCtrl.InsertItem(str1,0,1,hParent);
}
else
{
if(!m_TreeCtrl.ItemHasChildren(hParent))//判断是否有子节点
m_TreeCtrl.InsertItem(str1,0,1,hParent);
else
{
hChild=m_TreeCtrl.GetChildItem(hParent);
for(;hChild!=NULL;hChild=m_TreeCtrl.GetNextItem(hChild,TVGN_NEXT))
{
CString strItem2=m_TreeCtrl.GetItemText(hChild);
if(strItem2==str1)flag=TRUE;
}
if(!flag)m_TreeCtrl.InsertItem(str1,0,1,hParent);
}
} } } SetCtrlStyle(m_TreeCtrl.GetSafeHwnd(),TVS_HASLINES|TVS_LINESATROOT|TVS_HASBUTTONS);} 6.为CstudentView添加SetCtrlStyle(HWND hWnd, DWORD dwNewStyle)函数,并写入下列代码:
void CStudentView::SetCtrlStyle(HWND hWnd, DWORD dwNewStyle){ LONG lStyle;lStyle = GetWindowLong(hWnd, GWL_STYLE);//获取当前窗口style
lStyle &= ~LVS_TYPEMASK;//清除显示方式位
lStyle |= dwNewStyle;//设置style
SetWindowLong(hWnd, GWL_STYLE, lStyle);//设置style } 7.在CStudentView::OnInitialUpdate中添加一些初始化代码: void CStudentView::OnInitialUpdate(){ CTreeView::OnInitialUpdate();CTreeCtrl& m_TreeCtrl = GetTreeCtrl();m_TreeCtrl.DeleteAllItems();creatTree();} 8.为ClistView类添加print(CStudentInfo *dlg)和SetCtrlStyle(HWND hWnd,DWORD dwNewStyle)函数,并添入下列代码: void SetCtrlStyle(HWND hWnd,DWORD dwNewStyle){
DWORD dwOldStyle;
dwOldStyle=GetWindowLong(hWnd,GWL_STYLE);
if((dwOldStyle&LVS_TYPEMASK)!=dwNewStyle)
{
dwOldStyle&=~LVS_TYPEMASK;
dwNewStyle|=dwOldStyle;
SetWindowLong(hWnd,GWL_STYLE,dwNewStyle);
} } 7 void ClistView::print(CStudentInfo *dlg){ CListCtrl&m_listctrl=GetListCtrl();m_listctrl.InsertItem(0,dlg->strName);m_listctrl.SetItemText(0,1,dlg->strNO);if(dlg->bMale)
m_listctrl.SetItemText(0,2,“男”);else
m_listctrl.SetItemText(0,2,“女”);m_listctrl.SetItemText(0,3,dlg->tBirth.Format(“%Y-%m-%d”));m_listctrl.SetItemText(0,4,dlg->strAcademy);m_listctrl.SetItemText(0,5,dlg->strSpecial);CString str;str.Format(“%d”,dlg->English);m_listctrl.SetItemText(0,6,str);str.Format(“%d”,dlg->Computer);m_listctrl.SetItemText(0,7,str);} 9.在ClistView::OnInitialUpdate中添加初始化代码: void ClistView::OnInitialUpdate(){ CListView::OnInitialUpdate();
CListCtrl&m_listctrl=GetListCtrl();SetCtrlStyle(m_listctrl.m_hWnd,LVS_REPORT);for(int n=0;n
m_listctrl.DeleteColumn(0);CString strHeader[8]={“姓名”,“学号”,“性别”,“出生年月”,“学院”,“专业”,“英语”,“计算机”};int nWidth[8]={100,80,60,100,150,150,80,80};for(int nCol=0;nCol
m_listctrl.InsertColumn(nCol,strHeader[nCol],LVCFMT_LEFT,nWidth[nCol]);} 8 4.2文档序列化——建立可序列化类
1.选择“文件”→“新建”菜单命令,显示出“新建”对话框。单击“文件”标签在左边的列表框中选择C/C++ Header File项,在右边的“文件”下的编辑框中键入StudentInfo.h,单击“确定”按钮。
2.在文档窗口中输入下列代码: cla CStudentInfo:public CObject { public: CString strName;
//姓名
CString strNO;
//学号
BOOL
bMale;//性别,是否为男
CTime
tBirth;//出生年月
CString strAcademy;//学院
CString strSpecial;//专业
int English;
//英语成绩
int Computer;//计算机成绩
DECLARE_SERIAL(CStudentInfo)//序列化声明
public:
CStudentInfo(){};CStudentInfo(CString name,CString id,BOOL male,CTime birth,CString academy,CString special,int eng,int comp);void Serialize(CArchive &ar);};3.再次选择““文件”→“新建”菜单命令,显示出“新建”对话框。单击“文件”标签在左边的列表框中选择C++ Source File项,在右边的“文件”下的编辑框中键入StudentInfo.cpp,单击“确定”按钮。
4.在文档窗口中输入下面的代码: #include “stdafx.h” #include “StudentInfo.h” CStudentInfo::CStudentInfo(CString name,CString id,BOOL male,CTime birth,CString academy,CString special,int eng,int comp)9 { strName=name;strNO=id;bMale=male;tBirth=birth;strAcademy=academy;strSpecial=special;English=eng;Computer=comp;} IMPLEMENT_SERIAL(CStudentInfo,CObject,1)void CStudentInfo::Serialize(CArchive &ar){ if(ar.IsStoring())ar
ar>>strName>>strNO>>bMale>>tBirth>>strAcademy>>strSpecial>>English>>Computer;} 4.3添加并处理对话框
1.向应用程序中添加一个对话框资源,标题定为“学生信息管理”,创建此对话框为CstuInfoDlg。
2.打开对话框网格,参照下图为对话框添加控件:
3.为各个控件增加成员变量:
4.为CstuInfoDlg类添加一个BOOL型成员变量m_bMale,并设置该变量的初始值:
CStuInfoDlg::CStuInfoDlg(CWnd* pParent /*=NULL*/): CDialog(CStuInfoDlg::IDD, pParent){ 11 m_bMale=FALSE;//{{AFX_DATA_INIT(CStuInfoDlg)„„
//}}AFX_DATA_INIT } 5.用MFC ClaWizard为单选按钮IDC_RADIO_MALE添加BN_CLICKED的消息映射,并增加下列代码: void CstuInfoDlg::OnRadioMale(){
m_bMale=TRUE; }
6.用MFC ClaWizard为单选按钮IDC_RADIO_FEMALE添加BN_CLICKED的消息映射,并增加下列代码: void CstuInfoDlg::OnRadioFemale(){
m_bMale=FALSE; }
7.再添加一个对话框资源,标题认定为“学生信息管理”,创建此对话框类为CstuDlg。按下图添加控件,并为控件增加成员变量:
4.4添加并处理菜单项
1.在StudentDoc.cpp文件的开始处,添加包含CStuInfoDlg和CstuDlg类的头文件包含。
#include “StudentDoc.h” #include “StuInfoDlg.h” #include “StuDlg.h” 2.在菜单资源的主菜单中添加顶层菜单项“学生信息管理(&M)”,在该顶层菜单项中添加子菜单“添加(&A)”(ID_STUINFO_ADD),“修改(&X)”(ID_STUINFO_MODIFY),“删除(&D)”(ID_STUINFO_DELETE),“查询(&Q)”(ID_STUINFO_QUERY)。
3.用MFC ClaWizard为CstudentDoc添加处理菜单项ID_STUINFO_ADD,ID_STUINFO_MODIFY,ID_STUINFO_DELETE和ID_STUINFO_QUERY的 COMMAND的消息,并添加下列代码: void CStudentDoc::OnStuinfoAdd()//添加 { CStuInfoDlg dlg;BOOL flag=TRUE;//根据姓名判断是否已存在if(dlg.DoModal()!=IDOK)return;int nIndex=m_stuObArray.GetSize();for(int n=0;n
CStudentInfo *stu=(CStudentInfo*)m_stuObArray.GetAt(n);
if(stu->strName==dlg.m_strName)
{
AfxMeageBox(“此学生信息已存在”);
flag=FALSE;
} } 13 if(flag){
CStudentInfo*pStudent=new CStudentInfo(dlg.m_strName,dlg.m_strNO,dlg.m_bMale,dlg.m_tBirth,dlg.m_strAcademy,dlg.m_strSpecial,dlg.m_english,dlg.m_computer);
m_stuObArray.Add(pStudent);
POSITION pos=GetFirstViewPosition();
CStudentView *stu=(CStudentView*)GetNextView(pos);
stu->creatTree();
SetModifiedFlag();//设置文档更改标志
UpdateAllViews(NULL);//更新视图
} }
void CStudentDoc::OnStuinfoQuery()//查询 { CStuDlg dlg;BOOL flag=TRUE;if(dlg.DoModal()!=IDOK)return;int nIndex=m_stuObArray.GetSize();if(nIndex==0)
AfxMeageBox(“无学生信息”);else {
for(int n=0;n
{
CStudentInfo *stu=(CStudentInfo*)m_stuObArray.GetAt(n);
if(stu->strName==dlg.m_strNm)
{
POSITION pos=GetFirstViewPosition();
GetNextView(pos);
ClistView *list=(ClistView*)GetNextView(pos);
CListCtrl&m_list=list->GetListCtrl();
m_list.DeleteAllItems();14
list->print(stu);
flag=FALSE;
}
}
//判断是否找到
if(flag)
AfxMeageBox(“无此学生信息”);} } void CStudentDoc::OnStuinfoDelete()//删除 { CStuDlg dlg;BOOL flag=TRUE;if(dlg.DoModal()!=IDOK)return;int nIndex=m_stuObArray.GetSize();if(nIndex==0)
AfxMeageBox(“无学生信息”);else {
for(int n=0;n
{
CStudentInfo *stu=(CStudentInfo*)m_stuObArray.GetAt(n);
if(stu->strName==dlg.m_strNm)
{
CStudentInfo *stu=(CStudentInfo*)m_stuObArray.GetAt(n);
delete m_stuObArray.GetAt(n);
m_stuObArray.RemoveAt(n,1);
AfxMeageBox(“已删除”);
flag=FALSE;
break;
SetModifiedFlag();//设置文档更改标志
UpdateAllViews(NULL);//更新视图
}
} 15
//判断是否找到
if(flag)
AfxMeageBox(“无此学生信息”);} }
void CStudentDoc::OnStuinfoModify()//修改 { CStuDlg dlg;CStuInfoDlg dl;BOOL flag=TRUE;if(dlg.DoModal()!=IDOK)return;int nIndex=m_stuObArray.GetSize();if(nIndex==0)
AfxMeageBox(“无学生信息”);else {
for(int n=0;n
{
CStudentInfo *stu=(CStudentInfo*)m_stuObArray.GetAt(n);
if(stu->strName==dlg.m_strNm)
{
dl.m_strName=stu->strName;
dl.m_strNO=stu->strNO;
dl.m_bMale=stu->bMale;
dl.m_tBirth=stu->tBirth;
dl.m_strAcademy=stu->strAcademy;
dl.m_strSpecial=stu->strSpecial;
dl.m_english=stu->English;
dl.m_computer=stu->Computer;
dl.DoModal();
stu->strName=dl.m_strName;
stu->strNO=dl.m_strNO;
stu->bMale=dl.m_bMale;16
}
}
stu->tBirth=dl.m_tBirth;
stu->strAcademy=dl.m_strAcademy;
stu->strSpecial=dl.m_strSpecial;
stu->English=dl.m_english;
stu->Computer=dl.m_computer;
SetModifiedFlag();//设置文档更改标志
UpdateAllViews(NULL);//更新视图
flag=FALSE;} } //判断是否找到 if(flag)AfxMeageBox(“无此学生信息”);4.5修改CStudentDoc类代码
1.在StudentDoc.h文件的cla CStudentDoc : public Cdocument前面,添加包含CStudentInfo类的头文件。
#include “StudentInfo.h” 2.为CStudentDoc类添加成员变量: public: CObArray m_stuObArray;3.在CstudentDoc类析构函数CStudentDoc::~CstudentDoc中添加下列代码: CStudentDoc::~CStudentDoc(){ int nIndex=m_stuObArray.GetSize();while(nIndex--)
delete m_stuObArray.GetAt(nIndex);m_stuObArray.RemoveAll();} 4.在CStudentDoc::Serialize(CArchive& ar)函数中添加下列代码:void CStudentDoc::Serialize(CArchive& ar){ if(ar.IsStoring()){
m_stuObArray.Serialize(ar);} else {
m_stuObArray.Serialize(ar);} } 5.编译。心得体会及参考文献
1、心得体会
两周的课程设计结束了,在这次的课程设计中不仅检验了我所学习的知识,也培养了我如何去把握一件事情,如何去做一件事情,又如何完成一件事情。在设计过程中,与同学不断的探讨、学习、推敲,无形中提高了对Visual C++的理解。
课程设计是对我们这门课程知识综合应用的实践训练,考察了我们对所学知识的掌握与应用程度。对于编程,老师一直强调要注重上机操作,通过课程设计,我对此有了更深的理解。书本上的理论知识固然重要,但从真正的操作中我们会收获更多,有时自认为很合理的逻辑,到了电脑上就是不能运行,或得到的并不是自己想要的结果,这就需要我们不断的调试,纠错,找出问题所在并一一解答,从中我们受益匪浅。
如今我不再认为编程枯燥反而觉得它很有趣,而且越来越觉得这门学问很精深,当自己的程序很好的运行时,会有一种小小的成就感。在以后的学习中我一定会再接再厉,不断提高自己。感谢老师这一学期的辛勤教导。
2、参考文献
高等院校程序设计规划教材 《Visual C++ 教程》郑阿奇 主编 丁有和 编著。