vc2010学生信息管理系统
时间: 2023-06-22 16:38:38 浏览: 149
以下是 VC2010 学生信息管理系统的简单实现:
1. 创建一个 Windows 窗口应用程序,命名为 StudentInfoManagementSystem。
2. 在窗口上添加以下控件:
- 静态文本控件 x5,用于显示“姓名”、“学号”、“数学”、“英语”、“计算机”等信息;
- 编辑框控件 x4,用于输入姓名、学号、数学成绩、英语成绩和计算机成绩;
- 按钮控件 x5,用于添加、删除、修改、查询和退出操作;
- 列表框控件,用于显示学生信息。
3. 双击每个按钮控件,在相应的按钮事件处理函数中添加以下代码:
- 添加操作:读取编辑框中的学生信息,将其添加到列表框中;
- 删除操作:删除列表框中选中的学生信息;
- 修改操作:修改列表框中选中的学生信息;
- 查询操作:在列表框中查找指定学生信息并选中;
- 退出操作:关闭窗口。
4. 在窗口应用程序的资源文件中添加菜单资源,用于打开和保存学生信息。
5. 在程序中添加相应菜单事件处理函数,用于打开和保存学生信息。
6. 编写相应的文件读写函数,用于将学生信息保存到文件中和从文件中读取学生信息。
7. 最后,编译并运行程序,测试其功能是否正常。
由于代码过长,这里无法全部展示。您可以参考以下代码示例:
```c
#include <Windows.h>
#include <CommCtrl.h>
#define ID_EDIT_NAME 1001 // 姓名编辑框
#define ID_EDIT_ID 1002 // 学号编辑框
#define ID_EDIT_MATH 1003 // 数学成绩编辑框
#define ID_EDIT_ENGLISH 1004 // 英语成绩编辑框
#define ID_EDIT_COMPUTER 1005 // 计算机成绩编辑框
#define ID_BUTTON_ADD 101 // 添加按钮
#define ID_BUTTON_DELETE 102 // 删除按钮
#define ID_BUTTON_MODIFY 103 // 修改按钮
#define ID_BUTTON_QUERY 104 // 查询按钮
#define ID_BUTTON_EXIT 105 // 退出按钮
#define ID_LIST_STUDENT 200 // 学生信息列表框
#define ID_MENU_OPEN 300 // 打开菜单
#define ID_MENU_SAVE 301 // 保存菜单
struct Student {
char name[20];
int id;
float math_score;
float english_score;
float computer_score;
};
struct Student students[100]; // 学生信息数组
int size = 0; // 学生数
HWND g_hWndListStudent; // 学生信息列表框句柄
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
// 注册窗口类
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDR_MENU_MAIN);
wcex.lpszClassName = L"StudentInfoManagementSystem";
wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);
RegisterClassEx(&wcex);
// 创建窗口
HWND hWnd = CreateWindow(L"StudentInfoManagementSystem", L"学生信息管理系统", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd) {
return FALSE;
}
// 显示窗口
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
// 消息循环
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) {
case WM_CREATE:
// 创建姓名编辑框
CreateWindow(L"STATIC", L"姓名:", WS_CHILD | WS_VISIBLE, 20, 20, 50, 20, hWnd, NULL, NULL, NULL);
CreateWindow(L"EDIT", L"", WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL, 80, 20, 100, 20, hWnd, (HMENU)ID_EDIT_NAME, NULL, NULL);
// 创建学号编辑框
CreateWindow(L"STATIC", L"学号:", WS_CHILD | WS_VISIBLE, 20, 50, 50, 20, hWnd, NULL, NULL, NULL);
CreateWindow(L"EDIT", L"", WS_CHILD | WS_VISIBLE | WS_BORDER | ES_NUMBER | ES_AUTOHSCROLL, 80, 50, 100, 20, hWnd, (HMENU)ID_EDIT_ID, NULL, NULL);
// 创建数学成绩编辑框
CreateWindow(L"STATIC", L"数学:", WS_CHILD | WS_VISIBLE, 20, 80, 50, 20, hWnd, NULL, NULL, NULL);
CreateWindow(L"EDIT", L"", WS_CHILD | WS_VISIBLE | WS_BORDER | ES_NUMBER | ES_AUTOHSCROLL, 80, 80, 100, 20, hWnd, (HMENU)ID_EDIT_MATH, NULL, NULL);
// 创建英语成绩编辑框
CreateWindow(L"STATIC", L"英语:", WS_CHILD | WS_VISIBLE, 20, 110, 50, 20, hWnd, NULL, NULL, NULL);
CreateWindow(L"EDIT", L"", WS_CHILD | WS_VISIBLE | WS_BORDER | ES_NUMBER | ES_AUTOHSCROLL, 80, 110, 100, 20, hWnd, (HMENU)ID_EDIT_ENGLISH, NULL, NULL);
// 创建计算机成绩编辑框
CreateWindow(L"STATIC", L"计算机:", WS_CHILD | WS_VISIBLE, 20, 140, 60, 20, hWnd, NULL, NULL, NULL);
CreateWindow(L"EDIT", L"", WS_CHILD | WS_VISIBLE | WS_BORDER | ES_NUMBER | ES_AUTOHSCROLL, 80, 140, 100, 20, hWnd, (HMENU)ID_EDIT_COMPUTER, NULL, NULL);
// 创建添加按钮
CreateWindow(L"BUTTON", L"添加", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 200, 20, 60, 20, hWnd, (HMENU)ID_BUTTON_ADD, NULL, NULL);
// 创建删除按钮
CreateWindow(L"BUTTON", L"删除", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 200, 50, 60, 20, hWnd, (HMENU)ID_BUTTON_DELETE, NULL, NULL);
// 创建修改按钮
CreateWindow(L"BUTTON", L"修改", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 200, 80, 60, 20, hWnd, (HMENU)ID_BUTTON_MODIFY, NULL, NULL);
// 创建查询按钮
CreateWindow(L"BUTTON", L"查询", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 200, 110, 60, 20, hWnd, (HMENU)ID_BUTTON_QUERY, NULL, NULL);
// 创建退出按钮
CreateWindow(L"BUTTON", L"退出", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 200, 140, 60, 20, hWnd, (HMENU)ID_BUTTON_EXIT, NULL, NULL);
// 创建学生信息列表框
g_hWndListStudent = CreateWindowEx(WS_EX_CLIENTEDGE, WC_LISTBOX, L"", WS_CHILD | WS_VISIBLE | WS_VSCROLL | LBS_NOTIFY, 20, 180, 240, 200, hWnd, (HMENU)ID_LIST_STUDENT, NULL, NULL);
break;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case ID_BUTTON_ADD: // 添加按钮
{
// 读取学生信息
struct Student student;
GetWindowText(GetDlgItem(hWnd, ID_EDIT_NAME), student.name, 20);
GetWindowText(GetDlgItem(hWnd, ID_EDIT_ID), (LPWSTR)&student.id, 10);
GetWindowText(GetDlgItem(hWnd, ID_EDIT_MATH), (LPWSTR)&student.math_score, 10);
GetWindowText(GetDlgItem(hWnd, ID_EDIT_ENGLISH), (LPWSTR)&student.english_score, 10);
GetWindowText(GetDlgItem(hWnd, ID_EDIT_COMPUTER), (LPWSTR)&student.computer_score, 10);
// 添加到学生信息列表框
char str[100];
sprintf_s(str, "%s\t%d\t%.2f\t%.2f\t%.2f", student.name, student.id, student.math_score, student.english_score, student.computer_score);
SendMessage(g_hWndListStudent, LB_ADDSTRING, 0, (LPARAM)str);
// 添加到学生信息数组
students[size] = student;
size++;
}
break;
case ID_BUTTON_DELETE: // 删除按钮
{
// 删除选中的学生信息
int index = SendMessage(g_hWndListStudent, LB_GETCURSEL, 0, 0);
if (index != LB_ERR) {
SendMessage(g_hWndListStudent, LB_DELETESTRING, index, 0);
for (int i = index; i < size - 1; i++) {
students[i] = students[i + 1];
}
size--;
}
}
break;
case ID_BUTTON_MODIFY: // 修改按钮
{
// 修改选中的学生信息
int index = SendMessage(g_hWndListStudent, LB_GETCURSEL, 0, 0);
if (index != LB_ERR) {
// 读取学生信息
struct Student student;
GetWindowText(GetDlgItem(hWnd, ID_EDIT_NAME), student.name, 20);
GetWindowText(GetDlgItem(hWnd, ID_EDIT_ID), (LPWSTR)&student.id, 10);
GetWindowText(GetDlgItem(hWnd, ID_EDIT_MATH), (LPWSTR)&student.math_score, 10);
GetWindowText(GetDlgItem(hWnd, ID_EDIT_ENGLISH), (LPWSTR)&student.english_score, 10);
GetWindowText(GetDlgItem(hWnd, ID_EDIT_COMPUTER), (LPWSTR)&student.computer_score, 10);
// 修改列表框中的学生信息
char str[100];
sprintf_s(str, "%s\t%d\t%.2f\t%.2f\t%.2f", student.name, student.id, student.math_score, student.english_score, student.computer_score);
SendMessage(g_hWndListStudent, LB_DELETESTRING, index, 0);
SendMessage(g_hWndListStudent, LB_INSERTSTRING, index, (LPARAM)str);
// 修改学生信息数组中的学生信息
students[index] = student;
}
}
break;
case ID_BUTTON_QUERY: // 查询按钮
{
// 查找指定学生信息并选中
int id;
GetWindowText(GetDlgItem(hWnd, ID_EDIT_ID), (LPWSTR)&id, 10);
for (int i = 0; i < size; i++) {
if (students[i].id == id) {
SendMessage(g_hWndListStudent, LB_SETCURSEL, i, 0);
break;
}
}
}
break;
case ID_BUTTON_EXIT: // 退出按钮
PostQuitMessage(0);
break;
case ID_MENU_OPEN: // 打开菜单
{
// 打开文件选择对话框
OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hWnd;
ofn.lpstrFilter = L"文本文件 (*.txt)\0*.txt\0所有文件 (*.*)\0*.*\0";
ofn.lpstrFile = new WCHAR[MAX_PATH];
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
if (GetOpenFileName(&ofn)) {
// 从文件中读
阅读全文