今热点:基于MFC的学生成绩管理系统

2023-01-06 17:10:55 来源:51CTO博客


(资料图片)

1.基本功能演示​

1.1软件登陆界面​

为实现多账户的登陆方式,故采用了标签页组合的方式,每个分页面指向一个登录窗口。​

1.2学生界面​

学生界面所需要的功能为课程分数查询,功能较为简单,采用了下拉框+编辑框+按钮的方式实现,需要的特殊功能为学分预警。当科目分数小于60时,弹出弹框,并提示,当前科目分数小于60,要加油啦。​

1.3教师界面​

教师界面相对于学生界面略显复杂,采用了列表+下拉框+编辑框的方式,首先通过下拉框的方式选择姓名,随之与其对应的其他几项也会出现,可以根据科目下拉框选择需要修改的科目,写好后点击修改​

1.4教务界面​

教务界面相较于教师界面包含了成绩修改的功能,同时增加了对学生的增加​

2.部分代码展示​

2.1标签页部分代码​

BOOL CMySystemDlg::OnInitDialog()​{​ CDialogEx::OnInitDialog();​ // 将“关于...”菜单项添加到系统菜单中。​ // IDM_ABOUTBOX 必须在系统命令范围内。​ ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);​ ASSERT(IDM_ABOUTBOX < 0xF000);​ CMenu* pSysMenu = GetSystemMenu(FALSE);​ if (pSysMenu != nullptr)​ {​  BOOL bNameValid;​  CString strAboutMenu;​  bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);​  ASSERT(bNameValid);​  if (!strAboutMenu.IsEmpty())​  {​   pSysMenu->AppendMenu(MF_SEPARATOR);​   pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);​  }​ }​ // 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动​ // 执行此操作​ SetIcon(m_hIcon, TRUE);   // 设置大图标​ SetIcon(m_hIcon, FALSE);  // 设置小图标​ // TODO: 在此添加额外的初始化代码​ theApp.Mymanager.name = TEXT("教务");​ theApp.Mymanager.password = "12345678";​ theApp.Mystudent.name = TEXT("宋同学");​ theApp.Mystudent.password = "12345678";​ theApp.Myteacher.name = TEXT("大卫老师");​ theApp.Myteacher.password = "12345678";​ CRect tabRect; // 标签控件客户区的Rect ​ // 获取标签控件客户区Rect,并对其调整,以适合放置标签页 ​ m_Tab.GetClientRect(&tabRect);​ tabRect.left += 1;​ tabRect.right -= 1;​ tabRect.top += 25;​ tabRect.bottom -= 1;​ m_Tab.InsertItem(0, _T("学生登录")); // 插入第一个标签​ m_Tab.InsertItem(1, _T("教师登录")); // 插入第二个标签​ m_Tab.InsertItem(2, _T("教务登录")); //第三个标签​ m_student.Create(IDD_DIALOG1, &m_Tab); // 创建第一个标签页 ​ m_teacher.Create(IDD_DIALOG2, &m_Tab); // 创建第二个标签页 ​ m_manager.Create(IDD_DIALOG3, &m_Tab);//创建第三个标签页​ // 根据调整好的tabRect放置t1子对话框,并设置为显示 ​ m_student.SetWindowPos(NULL,tabRect.left, tabRect.top, tabRect.Width(), tabRect.Height(), SWP_SHOWWINDOW);​ // 根据调整好的tabRect放置t2子对话框,并设置为隐藏 ​ m_teacher.SetWindowPos(NULL, tabRect.left, tabRect.top, tabRect.Width(), tabRect.Height(), SWP_HIDEWINDOW);​ // 根据调整好的tabRect放置t3子对话框,并设置为隐藏 ​ m_manager.SetWindowPos(NULL, tabRect.left, tabRect.top, tabRect.Width(), tabRect.Height(), SWP_HIDEWINDOW);​ return TRUE; // 除非将焦点设置到控件,否则返回 TRUE​}​void CMySystemDlg::OnSysCommand(UINT nID, LPARAM lParam)​{​ if ((nID & 0xFFF0) == IDM_ABOUTBOX)​ {​  CAboutDlg dlgAbout;​  dlgAbout.DoModal();​ }​ else​ {​  CDialogEx::OnSysCommand(nID, lParam);​ }​}​//当用户拖动最小化窗口时系统调用此函数取得光标​//显示。​HCURSOR CMySystemDlg::OnQueryDragIcon()​{ return static_cast(m_hIcon);​}void CMySystemDlg::OnTcnSelchangeTab1(NMHDR* pNMHDR, LRESULT* pResult)​{ // TODO: 在此添加控件通知处理程序代码 CRect tabRect; // 标签控件客户区的Rect  // 获取标签控件客户区Rect,并对其调整,以适合放置标签页 ​ m_Tab.GetClientRect(&tabRect); tabRect.left += 1;​ tabRect.right -= 1;​ tabRect.top += 25;​ tabRect.bottom -= 1;​ switch (m_Tab.GetCurSel()) //返回组合框中列表框中当前选中的项的下标。如果没有选中项,则返回CB_ERR。 ​ {​  // 如果标签控件当前选择标签为“测试1”​ case 0:​  m_student.SetWindowPos(NULL, tabRect.left, tabRect.top, tabRect.Width(), tabRect.Height(), SWP_SHOWWINDOW);​  m_teacher.SetWindowPos(NULL, tabRect.left, tabRect.top, tabRect.Width(), tabRect.Height(), SWP_HIDEWINDOW);​  m_manager.SetWindowPos(NULL, tabRect.left, tabRect.top, tabRect.Width(), tabRect.Height(), SWP_HIDEWINDOW);​  break;​  // 如果标签控件当前选择标签为“测试2”​ case 1:​  m_student.SetWindowPos(NULL, tabRect.left, tabRect.top, tabRect.Width(), tabRect.Height(), SWP_HIDEWINDOW);​  m_teacher.SetWindowPos(NULL, tabRect.left, tabRect.top, tabRect.Width(), tabRect.Height(), SWP_SHOWWINDOW);​  m_manager.SetWindowPos(NULL, tabRect.left, tabRect.top, tabRect.Width(), tabRect.Height(), SWP_HIDEWINDOW);​  break;​ case 2:​  m_student.SetWindowPos(NULL, tabRect.left, tabRect.top, tabRect.Width(), tabRect.Height(), SWP_HIDEWINDOW);​  m_teacher.SetWindowPos(NULL, tabRect.left, tabRect.top, tabRect.Width(), tabRect.Height(), SWP_HIDEWINDOW);​  m_manager.SetWindowPos(NULL, tabRect.left, tabRect.top, tabRect.Width(), tabRect.Height(), SWP_SHOWWINDOW);​  break;​ default:​  break;​ }​ *pResult = 0;​}

2.2学生登录页代码​

// StudentDlg 对话框​IMPLEMENT_DYNAMIC(StudentDlg, CDialogEx)​StudentDlg::StudentDlg(CWnd* pParent /*=nullptr*/)​ : CDialogEx(IDD_DIALOG1, pParent)​ , m_name(_T(""))​ , m_password(_T(""))​{​}​StudentDlg::~StudentDlg()​{​}​void StudentDlg::DoDataExchange(CDataExchange* pDX)​{​ CDialogEx::DoDataExchange(pDX);​ DDX_Text(pDX, IDC_EDIT1, m_name);​ DDX_Text(pDX, IDC_EDIT2, m_password);​}​BEGIN_MESSAGE_MAP(StudentDlg, CDialogEx)​ ON_BN_CLICKED(IDC_BUTTON1, &StudentDlg::OnBnClickedButton1)​ ON_BN_CLICKED(IDC_BUTTON2, &StudentDlg::OnBnClickedButton2)​END_MESSAGE_MAP()​// StudentDlg 消息处理程序​//学生登录​void StudentDlg::OnBnClickedButton1()​{​ // TODO: 在此添加控件通知处理程序代码​ UpdateData(TRUE);​ if (m_name == theApp.Mystudent.name && m_password == theApp.Mystudent.password)​ {​  StuManDlg dlg;​  dlg.DoModal();​  return;​ }​ MessageBox(TEXT("账号或密码输入错误,请重新输入"));​}​//学生返回​void StudentDlg::OnBnClickedButton2()​{​ // TODO: 在此添加控件通知处理程序代码​ exit(1);​}​void StudentDlg::OnOK()​{​ // TODO: 在此添加专用代码和/或调用基类​ if (GetFocus() == GetDlgItem(IDC_EDIT1))​  GetDlgItem(IDC_EDIT2)->SetFocus();​ else if (GetFocus() == GetDlgItem(IDC_EDIT2))​  return OnBnClickedButton1();​ //CDialogEx::OnOK();​}​BOOL StudentDlg::OnInitDialog()​{​ CDialogEx::OnInitDialog();​ m_name = TEXT("宋同学"); //用户名​ m_password = TEXT("12345678");//密码​ UpdateData(FALSE); //内容更新到对应的控件​ // TODO: 在此添加额外的初始化​ GetDlgItem(IDC_EDIT1)->SetFocus();​ return TRUE; // return TRUE unless you set the focus to a control​     // 异常: OCX 属性页应返回 FALSE​}

2.3教师登录页代码​

// TeacherDlg 对话框​IMPLEMENT_DYNAMIC(TeacherDlg, CDialogEx)​TeacherDlg::TeacherDlg(CWnd* pParent /*=nullptr*/)​ : CDialogEx(IDD_DIALOG2, pParent)​ , m_name(_T(""))​ , m_password(_T(""))​{​}​TeacherDlg::~TeacherDlg()​{​}​void TeacherDlg::DoDataExchange(CDataExchange* pDX)​{​ CDialogEx::DoDataExchange(pDX);​ DDX_Text(pDX, IDC_EDIT1, m_name);​ DDX_Text(pDX, IDC_EDIT2, m_password);​}​BEGIN_MESSAGE_MAP(TeacherDlg, CDialogEx)​ ON_BN_CLICKED(IDC_BUTTON2, &TeacherDlg::OnBnClickedButton2)​ ON_BN_CLICKED(IDC_BUTTON1, &TeacherDlg::OnBnClickedButton1)​END_MESSAGE_MAP()​// TeacherDlg 消息处理程序​//教师登录​void TeacherDlg::OnBnClickedButton2()​{​ // TODO: 在此添加控件通知处理程序代码​ UpdateData(TRUE);​ if (m_name == theApp.Myteacher.name && m_password == theApp.Myteacher.password)​ {​  TeaManDlg dlg;​  dlg.DoModal();​  return;​ }​ MessageBox(TEXT("账号或密码输入错误,请重新输入"));​}​//教师返回​void TeacherDlg::OnBnClickedButton1()​{​ // TODO: 在此添加控件通知处理程序代码​ exit(1);​}​void TeacherDlg::OnOK()​{​ // TODO: 在此添加专用代码和/或调用基类​ if (GetFocus() == GetDlgItem(IDC_EDIT1))​  GetDlgItem(IDC_EDIT2)->SetFocus();​ else if (GetFocus() == GetDlgItem(IDC_EDIT2))​  return OnBnClickedButton2();​ //CDialogEx::OnOK();​}​BOOL TeacherDlg::OnInitDialog()​{​ CDialogEx::OnInitDialog();​ m_name = TEXT("大卫老师"); //用户名​ m_password = TEXT("12345678");//密码​ UpdateData(FALSE); //内容更新到对应的控件​ // TODO: 在此添加额外的初始化​ GetDlgItem(IDC_EDIT1)->SetFocus();​ return TRUE; // return TRUE unless you set the focus to a control​     // 异常: OCX 属性页应返回 FALSE​}

2.4教务登录页代码​

// ManagerDlg 对话框​IMPLEMENT_DYNAMIC(ManagerDlg, CDialogEx)​ManagerDlg::ManagerDlg(CWnd* pParent /*=nullptr*/)​ : CDialogEx(IDD_DIALOG3, pParent)​ , m_name(_T(""))​ , m_password(_T(""))​{​}​ManagerDlg::~ManagerDlg()​{​}​void ManagerDlg::DoDataExchange(CDataExchange* pDX)​{​ CDialogEx::DoDataExchange(pDX);​ DDX_Text(pDX, IDC_EDIT1, m_name);​ DDX_Text(pDX, IDC_EDIT2, m_password);​}​BEGIN_MESSAGE_MAP(ManagerDlg, CDialogEx)​ ON_EN_CHANGE(IDC_EDIT1, &ManagerDlg::OnEnChangeEdit1)​ ON_BN_CLICKED(IDC_BUTTON1, &ManagerDlg::OnBnClickedButton1)​ ON_BN_CLICKED(IDC_BUTTON2, &ManagerDlg::OnBnClickedButton2)​ ON_WM_CTLCOLOR()​END_MESSAGE_MAP()​// ManagerDlg 消息处理程序​BOOL ManagerDlg::OnInitDialog()​{​ CDialogEx::OnInitDialog();​ m_name = TEXT("教务"); //用户名​ m_password = TEXT("12345678");//密码​ UpdateData(FALSE); //内容更新到对应的控件​ // TODO: 在此添加额外的初始化​ GetDlgItem(IDC_EDIT1)->SetFocus();​ return TRUE; // return TRUE unless you set the focus to a control​     // 异常: OCX 属性页应返回 FALSE​}​void ManagerDlg::OnEnChangeEdit1()​{​ // TODO: 如果该控件是 RICHEDIT 控件,它将不​ // 发送此通知,除非重写 CDialogEx::OnInitDialog()​ // 函数并调用 CRichEditCtrl().SetEventMask(),​ // 同时将 ENM_CHANGE 标志“或”运算到掩码中。​}​//教务登录​void ManagerDlg::OnBnClickedButton1()​{​ // TODO: 在此添加控件通知处理程序代码​ UpdateData(TRUE);​ if (m_name == theApp.Mymanager.name && m_password == theApp.Mymanager.password)​ {​  MANANGER dlg;​  dlg.DoModal();​  return;​ }​ MessageBox(TEXT("账号或密码输入错误,请重新输入"));​}​//教务退出​void ManagerDlg::OnBnClickedButton2()​{​ // TODO: 在此添加控件通知处理程序代码​ exit(1);​}​void ManagerDlg::OnOK()​{​ // TODO: 在此添加专用代码和/或调用基类​ if (GetFocus() == GetDlgItem(IDC_EDIT1))​  GetDlgItem(IDC_EDIT2)->SetFocus();​ else if (GetFocus() == GetDlgItem(IDC_EDIT2))​  return OnBnClickedButton1();​ //CDialogEx::OnOK();​}​

2.5学生部分​

// StuManDlg 对话框​IMPLEMENT_DYNAMIC(StuManDlg, CDialogEx)​StuManDlg::StuManDlg(CWnd* pParent /*=nullptr*/)​ : CDialogEx(IDD_STUDENT, pParent)​ , m_grade(0)​ , m_num(0)​ , m_war(_T(""))​{​}​StuManDlg::~StuManDlg()​{​}​void StuManDlg::DoDataExchange(CDataExchange* pDX)​{​ CDialogEx::DoDataExchange(pDX);​ DDX_Control(pDX, IDC_COMBO1, m_combo);​ DDX_Text(pDX, IDC_EDIT2, m_grade);​ DDX_Text(pDX, IDC_EDIT1, m_num);​ DDX_Text(pDX, IDC_EDIT3, m_war);​}​BEGIN_MESSAGE_MAP(StuManDlg, CDialogEx)​ ON_BN_CLICKED(IDC_BUTTON1, &StuManDlg::OnBnClickedButton1)​ ON_CBN_SELCHANGE(IDC_COMBO1, &StuManDlg::OnCbnSelchangeCombo1)​END_MESSAGE_MAP()​BOOL StuManDlg::OnInitDialog()​{​ CDialogEx::OnInitDialog();​ //读取文件,获取课程名称,给组合框添加字符串​ CRect temprect(0, 0, 500, 400);​ CWnd::SetWindowPos(NULL, 0, 0, temprect.Width(), temprect.Height(), SWP_NOZORDER | SWP_NOMOVE);​ CInfoFile file;​ file.ReadDocline_one(); //读取学生信息​ for (list::iterator it = file.ls_one.begin(); it != file.ls_one.end(); it++)​ {​  m_combo.AddString((CString)it->sub.c_str());​ }​ file.ls_one.clear(); //清空list的内容​ return TRUE; // return TRUE unless you set the focus to a control​     // 异常: OCX 属性页应返回 FALSE​}​void StuManDlg::OnBnClickedButton1()​{​ // TODO: 在此添加控件通知处理程序代码​ //获取控件上的内容,更新到对应关联的变量中​ UpdateData(TRUE);​ CString type;​ //获取当前选中项​ int index = m_combo.GetCurSel();​ //获取组合框当前内容​ m_combo.GetLBText(index, type);​ if (m_grade <= 60)​ {​  CString str;​  str.Format(_T("科目:%s \r\n当前成绩小于60分,要好好学习啦!"), type);​  m_war = str; //销售信息​  UpdateData(FALSE);​  MessageBox(str);​ }​ CInfoFile file;​ file.ReadDocline(); //读取商品信息​ for (list::iterator it = file.ls_one.begin(); it != file.ls_one.end(); it++)​ {​  if (type == it->sub.c_str())​  {​  }​ }​ file.ls.clear(); //清空list的内容​ UpdateData(FALSE); //更新到对应的控件​}​void StuManDlg::OnCbnSelchangeCombo1()​{​ // TODO: 在此添加控件通知处理程序代码​ CString text;​ //获取当前选中项​ int index = m_combo.GetCurSel();​ //获取当前内容​ m_combo.GetLBText(index, text);​ CInfoFile file;     //需要包含#include "InfoFile.h"​ file.ReadDocline_one();   //读取科目信息​ for (list::iterator it = file.ls_one.begin(); it != file.ls_one.end(); it++)​ {​  if (text == it->sub.c_str())​  {​   m_grade = it->grade;​   m_num = it->numb;​   UpdateData(FALSE); //内容更新到对应的控件​  }​ }​ file.ls_one.clear(); //清空list的内容​}

2.6教师界面实现​

// TeaManDlg 对话框​IMPLEMENT_DYNAMIC(TeaManDlg, CDialogEx)​TeaManDlg::TeaManDlg(CWnd* pParent /*=nullptr*/)​ : CDialogEx(IDD_TEACHER, pParent)​ , _age(0)​ , _grade(0)​ , _id(0)​{​}​TeaManDlg::~TeaManDlg()​{​}​BOOL TeaManDlg::OnInitDialog()​{​ CDialogEx::OnInitDialog();​ CRect temprect(0, 0, 720, 670);​ CWnd::SetWindowPos(NULL, 0, 0, temprect.Width(), temprect.Height(), SWP_NOZORDER | SWP_NOMOVE);​ _list.SetExtendedStyle(_list.GetExtendedStyle() | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);​ CString str[] = { TEXT("学号"),TEXT("姓名"),TEXT("年龄"),TEXT("VC++"),TEXT("单片机")};​ for (int i = 0; i < 5; i++)​ {​  _list.InsertColumn(i, str[i], LVCFMT_LEFT, 80);​ }​ CInfoFile file;​ file.ReadDocline();​ int i = 0;​ for (list::iterator it = file.ls.begin(); it != file.ls.end(); it++)​ {​  CString str;​  str.Format(_T("%d"), (it->id));​  _list.InsertItem(i, str);​  str = CString(it->name.c_str());​  _list.SetItemText(i, 1, str);​  str.Format(_T("%d"), it->age);​  _list.SetItemText(i, 2, str);​  str.Format(_T("%d"), it->grade_vc);​  _list.SetItemText(i, 3, str);​  str.Format(_T("%d"), it->grade_mcu);​  _list.SetItemText(i, 4, str);​  i++;​  //选项的下拉框中姓名部分代码​  _combo.AddString((CString)it->name.c_str());​ }​ return TRUE; // return TRUE unless you set the focus to a control​     // 异常: OCX 属性页应返回 FALSE​}​void TeaManDlg::DoDataExchange(CDataExchange* pDX)​{​ CDialogEx::DoDataExchange(pDX);​ DDX_Control(pDX, IDC_LIST1, _list);​ DDX_Control(pDX, IDC_COMBO4, _combo);​ DDX_Text(pDX, IDC_EDIT6, _age);​ DDX_Text(pDX, IDC_EDIT7, _grade);​ DDX_Text(pDX, IDC_EDIT2, _id);​ DDX_Control(pDX, IDC_COMBO1, _combo_sub);​}​BEGIN_MESSAGE_MAP(TeaManDlg, CDialogEx)​ ON_CBN_SELCHANGE(IDC_COMBO4, &TeaManDlg::OnCbnSelchangeCombo4)​ ON_BN_CLICKED(IDC_BUTTON1, &TeaManDlg::OnBnClickedButton1)​ ON_CBN_SELCHANGE(IDC_COMBO1, &TeaManDlg::OnCbnSelchangeCombo1)​ ON_BN_CLICKED(IDC_BUTTON2, &TeaManDlg::OnBnClickedButton2)​END_MESSAGE_MAP()​// TeaManDlg 消息处理程序​void TeaManDlg::OnCbnSelchangeCombo4()​{​ _grade = 0; //每次切换完之后将成绩框归零​ CString text;​ int index = _combo.GetCurSel();  //获取当前选中项​ _combo.GetLBText(index, text);  //获取当前内容​ CInfoFile file;    //需要包含#include "InfoFile.h"​ file.ReadDocline();   //读取学生信息​ for (list::iterator it = file.ls.begin(); it != file.ls.end(); it++)​ {​  if (text == it->name.c_str())​  {​   _age = it->age; ​   _id = it->id;​   UpdateData(FALSE); //内容更新到对应的控件​   _combo_sub.ResetContent();​   _combo_sub.AddString(TEXT("VC++")); //AddString为追加选项​   _combo_sub.AddString(TEXT("单片机"));​  }​ }​ file.ls.clear(); //清空list的内容​}​void TeaManDlg::OnBnClickedButton1()​{​ // TODO: 在此添加控件通知处理程序代码​ UpdateData(TRUE);​ CString type;​ //获取当前选中项​ int index = _combo.GetCurSel();​ //获取组合框当前内容​ _combo.GetLBText(index, type);​ //需要包含#include "InfoFile.h"​ CInfoFile file;​ file.ReadDocline(); //读取学生信息​ for (list::iterator it = file.ls.begin(); it != file.ls.end(); it++)​ {​  if (type == it->name.c_str())​  {​   it->age = _age;​   it->id = _id;​   CString text;​   int index = _combo_sub.GetCurSel();//获取当前下拉框文本内容​   _combo_sub.GetLBText(index, text);​   CInfoFile file;    //需要包含#include "InfoFile.h"​   file.ReadDocline();   //读取学生信息​   if (text == CString(TEXT("VC++")))​   {​    it->grade_vc = _grade;​    UpdateData(FALSE);  //内容更新到对应的控件​   }​   if (text == CString(TEXT("单片机")))​   {​    it->grade_mcu = _grade;​    UpdateData(FALSE);  //内容更新到对应的控件​   }​  }​ }​ file.WriteDocline();  //更新文件内容​ file.ls.clear();   //清空list的内容​ UpdateData(FALSE);   //更新到对应的控件​ MessageBox(TEXT("修改成功"));​ CDialog::OnCancel();   //关闭当前窗口​ TeaManDlg dlg;     //重新打开并刷新​ dlg.DoModal();​}​void TeaManDlg::OnCbnSelchangeCombo1()​{​ // TODO: 在此添加控件通知处理程序代码​ CString text_all;​ int index = _combo.GetCurSel();  //获取当前内容​ _combo.GetLBText(index, text_all);​ CInfoFile file;      //需要包含#include "InfoFile.h"​ file.ReadDocline();     //读取学生信息​ for (list::iterator it = file.ls.begin(); it != file.ls.end(); it++)​ {​  if (text_all == it->name.c_str())​  {​   CString text;​   int index = _combo_sub.GetCurSel();//获取当前下拉框文本内容​   _combo_sub.GetLBText(index, text);​   CInfoFile file;    //需要包含#include "InfoFile.h"​   file.ReadDocline();   //读取学生信息​   if (text == CString(TEXT("VC++")))​   {​    _grade = it->grade_vc;​    UpdateData(FALSE);  //内容更新到对应的控件​   }​   if (text == CString(TEXT("单片机")))​   {​    _grade = it->grade_mcu;​    UpdateData(FALSE);  //内容更新到对应的控件​   }​  }​ }​ file.ls.clear(); //清空list的内容​}​//取消修改​void TeaManDlg::OnBnClickedButton2()​{​ // TODO: 在此添加控件通知处理程序代码​ _id = 0;​ _age = 0;​ _grade = 0;​ UpdateData(FALSE);​}

2.7教务界面实现​

// MANANGER 对话框​IMPLEMENT_DYNAMIC(MANANGER, CDialogEx)​MANANGER::MANANGER(CWnd* pParent /*=nullptr*/)​ : CDialogEx(IDD_DIALOG4, pParent)​ , _id(0)​ , _age(0)​ , _grade(0)​ , _name(_T(""))​ , _Addsub(_T(""))​{​}​MANANGER::~MANANGER()​{​}​void MANANGER::DoDataExchange(CDataExchange* pDX)​{​ CDialogEx::DoDataExchange(pDX);​ DDX_Control(pDX, IDC_LIST2, _list);​ DDX_Text(pDX, IDC_EDIT1, _id);​ DDX_Text(pDX, IDC_EDIT2, _age);​ DDX_Text(pDX, IDC_EDIT7, _grade);​ DDX_Control(pDX, IDC_COMBO5, _combo);​ DDX_Text(pDX, IDC_EDIT5, _name);​ DDX_Control(pDX, IDC_COMBO1, _combo_sub);​ DDX_Control(pDX, IDC_COMBO2, _combo_sub2);​ DDX_Text(pDX, IDC_EDIT3, _Addsub);​}​BEGIN_MESSAGE_MAP(MANANGER, CDialogEx)​ ON_BN_CLICKED(IDC_BUTTON1, &MANANGER::OnBnClickedButton1)​ ON_BN_CLICKED(IDC_BUTTON9, &MANANGER::OnBnClickedButton9)​ ON_CBN_SELCHANGE(IDC_COMBO5, &MANANGER::OnCbnSelchangeCombo5)​ ON_BN_CLICKED(IDC_BUTTON2, &MANANGER::OnBnClickedButton2)​ ON_BN_CLICKED(IDC_BUTTON6, &MANANGER::OnBnClickedButton6)​ ON_CBN_SELCHANGE(IDC_COMBO2, &MANANGER::OnCbnSelchangeCombo2)​ ON_BN_CLICKED(IDC_BUTTON5, &MANANGER::OnBnClickedButton5)​END_MESSAGE_MAP()​// 添加学生信息​void MANANGER::OnBnClickedButton1()​{​ // TODO: 在此添加控件通知处理程序代码​ UpdateData(TRUE); //获取控件内容​ if (_age <= 0 || _name.IsEmpty())​ {​  MessageBox(TEXT("输入信息有误"));​  return;​ }​ CInfoFile file;     //需要包含#include "InfoFile.h"​ file.ReadDocline();    //读取学生信息​ file.Addline(_id,_name,_age); //添加学生​ file.WriteDocline(); //写文件​ file.ls.clear();  //清空list的内容​ MessageBox(_T("添加成功"));​ _name.Empty();​ _age = 0;​ _grade = 0;​ CDialog::OnCancel();   //关闭当前窗口​ MANANGER dlg;     //重新打开并刷新​ dlg.DoModal();​ UpdateData(FALSE);​}​//初始化​BOOL MANANGER::OnInitDialog()​{​ CDialogEx::OnInitDialog();​ CRect temprect(0, 0, 870, 650);​ CWnd::SetWindowPos(NULL, 0, 0, temprect.Width(), temprect.Height(), SWP_NOZORDER | SWP_NOMOVE);​ _list.SetExtendedStyle(_list.GetExtendedStyle() | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);​ CString str[] = { TEXT("学号"),TEXT("姓名"),TEXT("年龄"),TEXT("VC++") ,TEXT("单片机") };​ for (int i = 0; i < 5; i++)​ {​  _list.InsertColumn(i, str[i], LVCFMT_LEFT, 80);​ }​ CInfoFile file;​ file.ReadDocline();​ int i = 0;​ for (list::iterator it = file.ls.begin(); it != file.ls.end(); it++)​ {​  CString str;​  str.Format(_T("%d"), (it->id));​  _list.InsertItem(i, str); //添加列​  str = CString(it->name.c_str());​  _list.SetItemText(i, 1, str);//添加文本内容​  str.Format(_T("%d"), it->age);​  _list.SetItemText(i, 2, str);​  str.Format(_T("%d"), it->grade_vc);​  _list.SetItemText(i, 3, str);​  str.Format(_T("%d"), it->grade_mcu);​  _list.SetItemText(i, 4, str);​  i++;​  _combo.AddString((CString)it->name.c_str());​ }​ _combo_sub.AddString(TEXT("VC++")); //AddString为追加选项​ _combo_sub.AddString(TEXT("单片机"));​ return TRUE; // return TRUE unless you set the focus to a control​     // 异常: OCX 属性页应返回 FALSE​}​//删除学生按钮部分​void MANANGER::OnBnClickedButton9() ​{​ // TODO: 在此添加控件通知处理程序代码​ //获取控件上的内容,更新到对应关联的变量中​ UpdateData(TRUE);​ int index = _combo.GetCurSel();​ if (index == -1)​ {​  MessageBox(TEXT("删除失败,您未选中任何人"));​  return;​ }​ CString type;    ​ _combo.GetLBText(index, type);//获取组合框当前内容​ CInfoFile file;    //需要包含#include "InfoFile.h"​ file.ReadDocline();   //读取学生信息​ UpdateData(TRUE);​ for (list::iterator it = file.ls.begin(); it != file.ls.end(); it++)​ {​  CString str;​  str = CString(it->name.c_str());​  if (type == str)​  {​   file.ls.remove(*it);​   _id = 0;​   _name = "";​   _age = 0;​   _grade = 0;​   _combo.DeleteString(_combo.GetCurSel());​   _combo.SetCurSel(-1);​   break;​  }​ }​ file.WriteDocline(); //更新文件内容​ UpdateData(FALSE); //更新到对应的控件​ MessageBox(TEXT("删除成功"));​ CDialog::OnCancel();   //关闭当前窗口​ MANANGER dlg;     //重新打开并刷新​ dlg.DoModal();​}​void MANANGER::OnCbnSelchangeCombo5()​{​ // TODO: 在此添加控件通知处理程序代码​ _grade = 0; //每次切换完之后将成绩框归零​ CString text;​ int index = _combo.GetCurSel(); //获取当前选中项​ _combo.GetLBText(index, text); //获取当前内容​ CInfoFile file;     //需要包含#include "InfoFile.h"​ file.ReadDocline(); //读取学生信息​ for (list::iterator it = file.ls.begin(); it != file.ls.end(); it++)​ {​  if (text == it->name.c_str()) //姓名比对​  {​   _age = it->age;​   _id = it->id;​   UpdateData(FALSE); //内容更新到对应的控件​   _combo_sub2.ResetContent();​   _combo_sub2.AddString(TEXT("VC++")); //AddString为追加选项​   _combo_sub2.AddString(TEXT("单片机"));​  }​ }​}​//修改学生信息​void MANANGER::OnBnClickedButton2()​{​ UpdateData(TRUE);​ CString type;​ //获取当前选中项​ int index = _combo.GetCurSel();​ //获取组合框当前内容​ _combo.GetLBText(index, type);​ //需要包含#include "InfoFile.h"​ CInfoFile file;​ file.ReadDocline(); //读取学生信息​ for (list::iterator it = file.ls.begin(); it != file.ls.end(); it++)​ {​  if (type == it->name.c_str())​  {​   if (!(_name.IsEmpty()))​   {​    CStringA s_name;​    s_name = _name;​    it->name = s_name.GetBuffer();​   }​   it->age = _age;​   it->id = _id;​   CString text;​   int index = _combo_sub2.GetCurSel();//获取当前下拉框文本内容​   _combo_sub2.GetLBText(index, text);​   CInfoFile file;    //需要包含#include "InfoFile.h"​   file.ReadDocline();   //读取学生信息​   if (text == CString(TEXT("VC++")))​   {​    it->grade_vc = _grade;​    UpdateData(FALSE);  //内容更新到对应的控件​   }​   if (text == CString(TEXT("单片机")))​   {​    it->grade_mcu = _grade;​    UpdateData(FALSE);  //内容更新到对应的控件​   }​  }​ }​ file.WriteDocline();  //更新文件内容​ file.ls.clear();   //清空list的内容​ MessageBox(TEXT("修改成功"));​ UpdateData(FALSE);   //更新到对应的控件​ CDialog::OnCancel();   //关闭当前窗口​ MANANGER dlg;     //重新打开并刷新​ dlg.DoModal();​}​//添加课程信息​int i = 6;​void MANANGER::OnBnClickedButton6()​{​ // TODO: 在此添加控件通知处理程序代码​ //获取编辑框内容 ​ UpdateData(TRUE);   //获取内容​ _combo_sub.AddString(_Addsub);​ _list.InsertColumn(i,_Addsub,LVCFMT_LEFT,80); //添加列​ i++;​ UpdateData(FALSE);​}​//添加学生成绩信息​void MANANGER::OnCbnSelchangeCombo2()​{​ // TODO: 在此添加控件通知处理程序代码​ CString text_all;​ int index = _combo.GetCurSel();  //获取当前内容​ _combo.GetLBText(index, text_all);​ CInfoFile file;      //需要包含#include "InfoFile.h"​ file.ReadDocline();     //读取学生信息​ for (list::iterator it = file.ls.begin(); it != file.ls.end(); it++)​ {​  if (text_all == it->name.c_str())​  {​   CString text;​   int index = _combo_sub2.GetCurSel();//获取当前下拉框文本内容​   _combo_sub2.GetLBText(index, text);​   CInfoFile file;    //需要包含#include "InfoFile.h"​   file.ReadDocline();   //读取学生信息​   if (text == CString(TEXT("VC++")))​   {​    _grade = it->grade_vc;​    UpdateData(FALSE);  //内容更新到对应的控件​   }​   if (text == CString(TEXT("单片机")))​   {​    _grade = it->grade_mcu;​    UpdateData(FALSE);  //内容更新到对应的控件​   }​  }​ }​ file.ls.clear(); //清空list的内容​}​void MANANGER::OnBnClickedButton5()​{​ // TODO: 在此添加控件通知处理程序代码​ _combo_sub.DeleteString(_combo_sub.GetCurSel()); //删除当前数据​ //_list.DeleteColumn(6);​}​  }​

标签: 处理程序 消息处理

上一篇:微服务系列 1:服务化框架落地的挑战和核心需求
下一篇:即时焦点:Linux用户及权限详解