mfc学生成绩管理系统c++具有查询录入保存删除修改功能代码
时间: 2023-12-20 20:03:39 浏览: 124
以下是一个简单的 MFC 学生成绩管理系统的代码:
头文件 Student.h:
```c++
#pragma once
#include <string>
class Student
{
public:
Student();
Student(std::string name, int score);
void setName(std::string name);
void setScore(int score);
std::string getName() const;
int getScore() const;
private:
std::string m_name;
int m_score;
};
```
源文件 Student.cpp:
```c++
#include "Student.h"
Student::Student() : m_name(""), m_score(0)
{
}
Student::Student(std::string name, int score) : m_name(name), m_score(score)
{
}
void Student::setName(std::string name)
{
m_name = name;
}
void Student::setScore(int score)
{
m_score = score;
}
std::string Student::getName() const
{
return m_name;
}
int Student::getScore() const
{
return m_score;
}
```
头文件 StudentMgr.h:
```c++
#pragma once
#include <vector>
#include "Student.h"
class StudentMgr
{
public:
void addStudent(Student student);
void removeStudent(int index);
void updateStudent(int index, Student student);
Student getStudent(int index) const;
int getNumStudents() const;
private:
std::vector<Student> m_students;
};
```
源文件 StudentMgr.cpp:
```c++
#include "StudentMgr.h"
void StudentMgr::addStudent(Student student)
{
m_students.push_back(student);
}
void StudentMgr::removeStudent(int index)
{
m_students.erase(m_students.begin() + index);
}
void StudentMgr::updateStudent(int index, Student student)
{
m_students[index] = student;
}
Student StudentMgr::getStudent(int index) const
{
return m_students[index];
}
int StudentMgr::getNumStudents() const
{
return m_students.size();
}
```
对话框类 CMyDlg:
```c++
class CMyDlg : public CDialogEx
{
public:
CMyDlg(CWnd* pParent = nullptr);
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_MY_DIALOG };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX);
protected:
HICON m_hIcon;
CString m_name;
int m_score;
StudentMgr m_studentMgr;
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
private:
void updateListCtrl();
};
CMyDlg::CMyDlg(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_MY_DIALOG, pParent)
, m_name(_T(""))
, m_score(0)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Text(pDX, IDC_NAME, m_name);
DDX_Text(pDX, IDC_SCORE, m_score);
}
BEGIN_MESSAGE_MAP(CMyDlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//{{AFX_MSG_MAP(CMyDlg)
ON_BN_CLICKED(IDC_ADD, &CMyDlg::OnBnClickedAdd)
ON_BN_CLICKED(IDC_DELETE, &CMyDlg::OnBnClickedDelete)
ON_BN_CLICKED(IDC_UPDATE, &CMyDlg::OnBnClickedUpdate)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BOOL CMyDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);
CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST);
pListCtrl->InsertColumn(0, _T("Name"), LVCFMT_LEFT, 100);
pListCtrl->InsertColumn(1, _T("Score"), LVCFMT_LEFT, 100);
return TRUE;
}
void CMyDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this);
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
HCURSOR CMyDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CMyDlg::OnBnClickedAdd()
{
UpdateData();
Student student(CStringA(m_name).GetString(), m_score);
m_studentMgr.addStudent(student);
updateListCtrl();
}
void CMyDlg::OnBnClickedDelete()
{
CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST);
int index = pListCtrl->GetSelectionMark();
if (index != -1)
{
m_studentMgr.removeStudent(index);
updateListCtrl();
}
}
void CMyDlg::OnBnClickedUpdate()
{
CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST);
int index = pListCtrl->GetSelectionMark();
if (index != -1)
{
UpdateData();
Student student(CStringA(m_name).GetString(), m_score);
m_studentMgr.updateStudent(index, student);
updateListCtrl();
}
}
void CMyDlg::updateListCtrl()
{
CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST);
pListCtrl->DeleteAllItems();
for (int i = 0; i < m_studentMgr.getNumStudents(); i++)
{
Student student = m_studentMgr.getStudent(i);
LVITEM lvitem;
lvitem.mask = LVIF_TEXT;
lvitem.iItem = i;
lvitem.iSubItem = 0;
lvitem.pszText = (LPWSTR)student.getName().c_str();
pListCtrl->InsertItem(&lvitem);
lvitem.mask = LVIF_TEXT;
lvitem.iItem = i;
lvitem.iSubItem = 1;
lvitem.pszText = (LPWSTR)std::to_wstring(student.getScore()).c_str();
pListCtrl->SetItem(&lvitem);
}
}
```
其中,IDC_NAME、IDC_SCORE、IDC_LIST 分别是对应的控件 ID。在资源文件中需要添加这些控件,并且对话框类需要添加响应函数 OnBnClickedAdd、OnBnClickedDelete、OnBnClickedUpdate。
阅读全文