⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 studentmisdlg.cpp

📁 使用vc++的mfc编写的简单学生管理系统源码
💻 CPP
字号:
// StudentMISDlg.cpp : implementation file
//

#include "stdafx.h"
#include "StudentMIS.h"
#include "StudentMISDlg.h"
#include "addstudent.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CStudentMISDlg dialog

CStudentMISDlg::CStudentMISDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CStudentMISDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CStudentMISDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CStudentMISDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CStudentMISDlg)
	DDX_Control(pDX, IDC_COMTYPE, m_type);
	DDX_Control(pDX, IDC_LISTINFO, m_list);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CStudentMISDlg, CDialog)
	//{{AFX_MSG_MAP(CStudentMISDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_ADD, OnAdd)
	ON_CBN_SELCHANGE(IDC_COMTYPE, OnSelchangeComtype)
	ON_BN_CLICKED(IDC_EDIT, OnEdit)
	ON_BN_CLICKED(IDC_DEL, OnDel)
	ON_BN_CLICKED(IDC_SAVE, OnSave)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CStudentMISDlg message handlers

/*窗口初始化*/
BOOL CStudentMISDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	CStudentMISApp *pApp = (CStudentMISApp *)AfxGetApp();
	m_type.SetCurSel(0);
	
	m_pImageList = new CImageList();
	ASSERT(m_pImageList != NULL);    
	m_pImageList->Create(16,16,ILC_COLOR32,0,10);
	m_pImageList->Add(pApp->LoadIcon(IDI_ICON1));
	m_pImageList->Add(pApp->LoadIcon(IDI_ICON2));
	m_pImageList->Add(pApp->LoadIcon(IDI_ICON3));
	m_list.SetImageList(m_pImageList,LVSIL_SMALL);

	DWORD dwStyle=m_list.GetExtendedStyle();
	m_list.SetExtendedStyle(dwStyle|LVS_EX_FULLROWSELECT);
	//设置列名
    m_list.InsertColumn(0,"姓名",LVCFMT_CENTER,60,-1);
	m_list.InsertColumn(1,"学号",LVCFMT_CENTER,60,0);
	m_list.InsertColumn(2,"年龄",LVCFMT_CENTER,60,1);
	m_list.InsertColumn(3,"学院",LVCFMT_CENTER,60,2);
	m_list.InsertColumn(4,"实验室",LVCFMT_CENTER,60,3);
	m_list.InsertColumn(5,"发表文章",LVCFMT_CENTER,60,4);
	

	//打开文件
	ifstream infile("学生信息.txt",ios::nocreate);
	if (infile.fail())
		MessageBox("该文件不存在!","警告",MB_OK);

	//读列信息数据
	char info[1023];
	infile.getline(info,sizeof(info));
	while(!infile.eof())
    {
		infile>>info;
		//博士生
		if (strcmp(info,"D")==0)
		{
			//存储数据
			CDoctorStudent dstu;
			infile>>info;
			dstu.m_strName =info;
			infile>>info;
			dstu.m_strID = info;
			infile>>info;
			dstu.m_nAge = atoi(info);
			infile>>info;
			dstu.m_strDepartment = info;
			infile>>info;
			dstu.m_strLab = info;
			infile>>info;
			dstu.m_nPaper = atoi(info);

			Darray.Add(dstu);
			dstu.Display(&m_list,2);
		}
		//硕士生
		if (strcmp(info,"M")==0)
		{
			
			//存储数据
			CMasterStudent mstu;
			infile>>info;
			mstu.m_strName =info;
			infile>>info;
			mstu.m_strID = info;
			infile>>info;
			mstu.m_nAge = atoi(info);
			infile>>info;
			mstu.m_strDepartment = info;
			infile>>info;
			mstu.m_strLab = info;
			
			Marray.Add(mstu);
			mstu.Display(&m_list,1);
		}
		//本科生
		if (strcmp(info,"B")==0)
		{
		
			//存储数据
			CStudent bstu;
			infile>>info;
			bstu.m_strName =info;
			infile>>info;
			bstu.m_strID = info;
			infile>>info;
			bstu.m_nAge = atoi(info);
			infile>>info;
			bstu.m_strDepartment = info;
					
			Barray.Add(bstu);
			bstu.Display(&m_list,0);
		}
	}
	infile.close();

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CStudentMISDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CStudentMISDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		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;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CStudentMISDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

/*按照学生类别分别显示学生信息*/
void CStudentMISDlg::OnSelchangeComtype() 
{
	//清除列表框
	m_list.DeleteAllItems();

	//只显示本科生或全部
	if (m_type.GetCurSel()==1 || m_type.GetCurSel()==0)
	{
		for(int i=0;i<Barray.GetSize();i++)
			((CStudent)Barray.GetAt(i)).Display(&m_list,0);
	}

	//只显示硕士生或全部
	if (m_type.GetCurSel()==2 || m_type.GetCurSel()==0)
	{
		for(int i=0;i<Marray.GetSize();i++)
			((CMasterStudent)Marray.GetAt(i)).Display(&m_list,1);
	}

	//只显示博士生或全部
	if (m_type.GetCurSel()==3 || m_type.GetCurSel()==0)
	{
		for(int i=0;i<Darray.GetSize();i++)
			((CDoctorStudent)Darray.GetAt(i)).Display(&m_list,2);
	}
}

/*调用添加学生的对话框*/
void CStudentMISDlg::OnAdd() 
{

	//创建增加学生信息窗口
	CAddStudent addStu;
	addStu.m_bIsNew = true;
	//CString strDepartment;
	
	//增加学生信息,并添加到数组中
	if (addStu.DoModal()==IDOK)
	{
		if (addStu.m_nBachelor == 0)
		{
			Barray.Add(CStudent(addStu.m_strName,addStu.m_strID,
								atoi(addStu.m_strAge),addStu.m_strDepartment));
		}
		if (addStu.m_nMaster == 0)
		{
			Marray.Add(CMasterStudent(addStu.m_strName,addStu.m_strID,
								atoi(addStu.m_strAge),addStu.m_strDepartment,
								addStu.m_strLab));
		}
		if (addStu.m_nDoctor == 0)
		{
			Darray.Add(CDoctorStudent(addStu.m_strName,addStu.m_strID,
								atoi(addStu.m_strAge),addStu.m_strDepartment,
								addStu.m_strLab,atoi(addStu.m_strPaper)));
		}
		GetDlgItem(IDC_SAVE)->EnableWindow(true);
	}
	//更新显示信息
	this->OnSelchangeComtype();
	
}

/*调用编辑学生信息的对话框*/
void CStudentMISDlg::OnEdit() 
{
	//获取选择的行
	POSITION pos = m_list.GetFirstSelectedItemPosition();
	if (pos == NULL)
		return;
	int nItem = m_list.GetNextSelectedItem(pos);
	
	//创建编辑窗口的实例,并附初值
	CAddStudent editStu;
	editStu.m_bIsNew = false;
	editStu.m_strName = m_list.GetItemText(nItem,0);
	editStu.m_strID = m_list.GetItemText(nItem,1);
	editStu.m_strAge = m_list.GetItemText(nItem,2);
	editStu.m_strDepartment = m_list.GetItemText(nItem,3);
	editStu.m_strLab = m_list.GetItemText(nItem,4);
	editStu.m_strPaper = m_list.GetItemText(nItem,5);
	
	//获取该行使用的图片号,以确定学生类别
	LVITEM item;
	item.mask =LVIF_IMAGE ;
	item.iItem = nItem; 
	item.iSubItem = 0;
	m_list.GetItem(&item);
	editStu.m_nBachelor =-1;
	editStu.m_nMaster =-1;
	editStu.m_nDoctor =-1;
	switch(item.iImage)
	{
		case 0: editStu.m_nBachelor =0;
			    break;
		case 1: editStu.m_nMaster =0;
			    break;
		case 2: editStu.m_nDoctor =0;
			    break;
	}

	//如果学生信息更新,则删除原有学生类,增加更新后的学生类
	if (editStu.DoModal()==IDOK)
	{
		RemoveStudent(m_list.GetItemText(nItem,1)); 
		if (editStu.m_nBachelor == 0)
		{
			Barray.Add(CStudent(editStu.m_strName,editStu.m_strID,
								atoi(editStu.m_strAge),editStu.m_strDepartment));
		}
		if (editStu.m_nMaster == 0)
		{
			Marray.Add(CMasterStudent(editStu.m_strName,editStu.m_strID,
								atoi(editStu.m_strAge),editStu.m_strDepartment,
								editStu.m_strLab));
		}
		if (editStu.m_nDoctor == 0)
		{
			Darray.Add(CDoctorStudent(editStu.m_strName,editStu.m_strID,
								atoi(editStu.m_strAge),editStu.m_strDepartment,
								editStu.m_strLab,atoi(editStu.m_strPaper)));
		}
		GetDlgItem(IDC_SAVE)->EnableWindow(true);
	}

	//更新显示信息
	this->OnSelchangeComtype();

}

/*删除指定学生信息*/
void CStudentMISDlg::OnDel() 
{
	//获取选择的行
	POSITION pos = m_list.GetFirstSelectedItemPosition();
	if (pos == NULL)
	{
		AfxMessageBox("请选择要删除的纪录!");
		return;
	}
	int nItem = m_list.GetNextSelectedItem(pos);
	
	//删除该行对应的类
	RemoveStudent(m_list.GetItemText(nItem,1)); 

	//更新显示窗口信息
	this->OnSelchangeComtype();
	GetDlgItem(IDC_SAVE)->EnableWindow(true);
}

/*保存学生信息*/
void CStudentMISDlg::OnSave() 
{
	//保存学生信
	ofstream outfile("学生信息.txt");
    if (outfile.fail())
        MessageBox("error");
	//写入列名称
	outfile<<"类别\t"<<"姓名\t"<<"学号\t"<<"年龄\t"
		<<"系别\t"<<"实验室\t"<<"发表文章\t"<<endl;
	
	//保存本科生信息
	for (int i=0;i<Barray.GetSize();i++)
	{
		outfile<<"B\t";
		CStudent bstr = Barray.GetAt(i);
		outfile<<bstr.m_strName<<"\t"<<bstr.m_strID<<"\t"<<bstr.m_nAge
			<<"\t"<<bstr.m_strDepartment<<endl;
	}

	//保存硕士生信息
	for (i=0;i<Marray.GetSize();i++)
	{
		outfile<<"M\t";
		CMasterStudent mstr = Marray.GetAt(i);
		outfile<<mstr.m_strName<<"\t"<<mstr.m_strID<<"\t"<<mstr.m_nAge<<"\t"
			<<mstr.m_strDepartment<<"\t"<<mstr.m_strLab<<"\t"<<endl;
	}

	//保存博士生信息
	for (i=0;i<Darray.GetSize();i++)
	{
		outfile<<"D\t";
		CDoctorStudent dstr = Darray.GetAt(i);
		outfile<<dstr.m_strName<<"\t"<<dstr.m_strID<<"\t"<<dstr.m_nAge<<"\t"
			<<dstr.m_strDepartment<<"\t"<<dstr.m_strLab<<"\t"<<dstr.m_nPaper<<endl;
	}

	//关闭文件
	outfile.close();
	GetDlgItem(IDC_SAVE)->EnableWindow(false);
}

//从数组中删除指定学生实例
void CStudentMISDlg::RemoveStudent(CString strID)
{
	//从本科生信息中查找
	for (int i=0;i<Barray.GetSize();i++)
	{
		if (strID.CompareNoCase(((CStudent)Barray.GetAt(i)).m_strID)==0)
			Barray.RemoveAt(i);
	}

	//从硕士生信息中查找
	for (i=0;i<Marray.GetSize();i++)
	{
		if (strID.CompareNoCase(((CStudent)Marray.GetAt(i)).m_strID)==0)
			Marray.RemoveAt(i);
	}

	//从博士生信息中查找
	for (i=0;i<Darray.GetSize();i++)
	{
		if (strID.CompareNoCase(((CStudent)Darray.GetAt(i)).m_strID)==0)
			Darray.RemoveAt(i);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -