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

📄 inputdata.cpp

📁 一个详细的学籍管理系统源码,支持数据查找、排序、导入、导出
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// InputData.cpp : 实现文件
//

#include "stdafx.h"
#include "StudentRecords.h"
#include "InputData.h"


// CInputData 对话框

IMPLEMENT_DYNAMIC(CInputData, CDialog)

CInputData::CInputData(CWnd* pParent /*=NULL*/)
	: CDialog(CInputData::IDD, pParent)
{

}

CInputData::~CInputData()
{
}

void CInputData::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST1, m_score);
	DDX_Control(pDX, IDC_CLASS, m_class);
	DDX_Control(pDX, IDC_SEX, m_sex);
	DDX_Control(pDX, IDC_SCORESEG, m_scoreseg);
	DDX_Control(pDX, IDC_XUEKE, m_xueke);
	DDX_Control(pDX, IDC_READEM, m_readem);
}


BEGIN_MESSAGE_MAP(CInputData, CDialog)
	ON_BN_CLICKED(IDC_ADD, &CInputData::OnBnClickedAdd)
	ON_BN_CLICKED(IDC_DEL, &CInputData::OnBnClickedDel)
	ON_BN_CLICKED(IDC_DELALL, &CInputData::OnBnClickedDelall)
	ON_BN_CLICKED(IDC_SAVETXT, &CInputData::OnBnClickedSavetxt)
	ON_WM_SHOWWINDOW()
	ON_WM_MOUSEWHEEL()
	ON_BN_CLICKED(IDC_NEWVIEW, &CInputData::OnBnClickedNewview)
	ON_CBN_SELCHANGE(IDC_CLASS, &CInputData::OnCbnSelchangeClass)
END_MESSAGE_MAP()


// CInputData 消息处理程序

BOOL CInputData::PreTranslateMessage(MSG* pMsg)
{
	if(pMsg->message==WM_KEYDOWN && pMsg->wParam==VK_RETURN)
	{
		OnBnClickedAdd();
		return   true;
	}
	if(pMsg->message==WM_KEYDOWN && pMsg->wParam==VK_ESCAPE)
		return   true;

	return CDialog::PreTranslateMessage(pMsg);
}

BOOL CInputData::OnInitDialog()
{
	CDialog::OnInitDialog();

	m_sex.AddString("男");
	m_sex.AddString("女");
	m_readem.AddString("无");
	m_readem.AddString("缺考");
	m_readem.AddString("舞弊");
	
	UpdateFlexGrid(3,9,2,1);

	return TRUE;
}

void CInputData::UpdateFlexGrid(int m,int n,int fm,int fn)
{
	m_score.Clear();
	m_score.SetRows(m);
	m_score.SetCols(n);
	m_score.SetFixedRows(fm);
	m_score.SetFixedCols(fn);
	m_score.SetSelectionMode(0); 
	m_score.SetMergeCells(3);//此风格的设定与上一句联合使用,后面用m_score.SetMergeCol(x,TRUE);可合并单元格
	m_score.SetRowHeightMin(260);
	int i,j;
	for(i=0;i<m;i++)
	{
		for(j=0;j<n;j++)
		{
			m_score.SetRow(long(i));
			m_score.SetCol(long(j));
			m_score.SetCellAlignment(4);
		}
	}
	CString str;
	str="序号";
	m_score.SetTextMatrix(0,0,str);
	m_score.SetTextMatrix(1,0,str);
	m_score.SetColWidth(0,480);
	str="学        校";
	m_score.SetTextMatrix(0,1,str);
	m_score.SetTextMatrix(1,1,str);
	m_score.SetColWidth(1,2200);
	str="年    级";
	m_score.SetTextMatrix(0,2,str);
	m_score.SetTextMatrix(1,2,str);
	m_score.SetColWidth(2,1200);
	str="班";
	m_score.SetTextMatrix(0,3,str);
	m_score.SetTextMatrix(1,3,str);
	m_score.SetColWidth(3,720);
	str="系      别";
	m_score.SetTextMatrix(0,4,str);
	m_score.SetTextMatrix(1,4,str);
	m_score.SetColWidth(4,1600);
	str="学  籍  号";
	m_score.SetTextMatrix(0,5,str);
	m_score.SetTextMatrix(1,5,str);
	m_score.SetColWidth(5,1400);
	str="姓    名";
	m_score.SetTextMatrix(0,6,str);
	m_score.SetTextMatrix(1,6,str);
	m_score.SetColWidth(6,800);
	str="性别";
	m_score.SetTextMatrix(0,7,str);
	m_score.SetTextMatrix(1,7,str);
	m_score.SetColWidth(7,540);
	str="年龄";
	m_score.SetTextMatrix(0,8,str);
	m_score.SetTextMatrix(1,8,str);
	m_score.SetColWidth(8,540);
	m_score.SetRowHeight(m_score.GetRows()-1,320);
	for(int c=0;c<n;c++)
		m_score.SetMergeCol(c,TRUE);
}

BOOL CInputData::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
	if(m_score.GetRows()>3)
	{
		int nPos = m_score.GetScrollPos(SB_VERT);         //获得当前垂直滚动条位置
		int nMax = m_score.GetScrollLimit(SB_VERT);      //获得当前垂直滚动条位置范围的最大值
		if(zDelta < 0)         //判断滚动方向为向下滚动
		{
			nPos += 3;
			(nPos >= nMax) ? (nPos = nMax-1) : NULL;
		}
		else
		{
			nPos -= 3;
			(nPos <= 1) ? (nPos = 1) : NULL;
		}
		m_score.SetTopRow(nPos+1);//固一行为2,所以加1
	}
	return CDialog::OnMouseWheel(nFlags, zDelta, pt);
}

void CInputData::OnCbnSelchangeClass()
{
	if(!(m_score.GetTextMatrix(2,1).IsEmpty()))
		if(MessageBox("在重新选择班级之前请确认数据是否保存!","提示",MB_YESNO|MB_ICONASTERISK|MB_DEFBUTTON2)==IDNO)
		{
			m_class.SetCurSel(m_class.FindString(0,m_score.GetTextMatrix(2,2)+m_score.GetTextMatrix(2,3)));//还原班级
			return;
		}
	if(theApp.bExist)
	{
		CString temp,gradename,xiname,classname;
		if(m_class.GetCurSel()>=0)//如果选择了具体的项则分别构造年级和班学符串并进行一系列初始化
		{
			SetDlgItemText(IDC_NUMBER,"");
			SetDlgItemText(IDC_NAME,"");
			SetDlgItemText(IDC_SEG,"");
			m_scoreseg.ResetContent();
			m_xueke.ResetContent();
			m_sex.SetCurSel(-1);
			m_readem.SetCurSel(-1);
			SetDlgItemText(IDC_SCORE,"");

			m_class.GetLBText(m_class.GetCurSel(),temp);
			gradename=temp.Left(temp.Find("级")+2);
			classname=temp.Right(temp.GetLength()-temp.Find("级")-2);
			int rcount;
			theApp.IniFile.GetItemInt("SCHOOL","row",rcount);//获到行数
			CString strvar,stritem;//strvar为字符串变量,stritem为字符串变量的值
			for(int r=1;r<=rcount;r++)
			{
				strvar.Format("var%d",r);//构造列号
				theApp.IniFile.GetItemString("CLASS",strvar,stritem);//获取字符串变量的值
				if(stritem.Find(gradename)>0 && stritem.Find(classname)>0)
				//找到时值返回值大于或等于0,但在具体设计文件时这两项不可能出现在首部,所以取大于0
				//找到后便获取该行的系别及学科字串,学科字串必须分解,因为是以逗号分隔的。
				//再根据学科数在网格控件中增加列并对其进行格式初始化
				{
					//获得“级”字串与“班”字串之间的字串
					temp=stritem.Mid(stritem.Find("级",0),stritem.Find("班",0)-stritem.Find("级",0));
					//根据temp字串得到系别
					xiname=temp.Mid(temp.Find(";",0)+1,temp.ReverseFind(';')-temp.Find(";",0)-1);
					if(!xiname.IsEmpty())
						m_scoreseg.AddString(xiname);
					else
						m_scoreseg.AddString("无");//没有分系则为“无”
					m_scoreseg.SetCurSel(0);
					//系别填写完毕,开始填写学科
					//截取stritem中“班”字串后面的字串
					temp=stritem.Right(stritem.GetLength()-stritem.Find("班",0)-3);
					//截取学科字串
					temp=temp.Left(temp.Find(";",0));
					//截取完毕,开始拆分字串以构造各个学科的字串并填入学科下拉框
					if(!temp.IsEmpty())//开始从已获取的字符串变量(temp)中获取单个以分号分隔的子串填充相应单元格
					{
						CString strleft;
						while(temp.Find(",",0)>=0)
						{
							if(temp.Find(",",0)>0)//找到分隔符并且不位于首端
							{
								strleft=temp.Left(temp.Find(",",0)+1);//截取左子串(包含找到的分隔符)
								m_xueke.AddString(strleft.Left(strleft.Find(",",0)));//用左子串填充分下拉框(但不含逗号分隔符)
								temp=temp.Right(temp.GetLength()-strleft.GetLength());//迭代改变本身值为右串
							}
							else
								if(temp.Find(",",0)==0)
									temp=temp.Right(temp.GetLength()-1);//分隔符位于首端时不截取左串,迭代改变本身值为去除首端分隔符后的右串,此时不对学科下拉框填充。
						}
						m_xueke.AddString(temp);//此时没的temp已经被迭代以逗号分隔的最末个子串了,所以对学科下拉框直拉用其填充之
						//获取学科数以便对网格初始化
						int c=m_xueke.GetCount();
						//清空表格内数据,但不包含第1行
						for(int r=1;r<m_score.GetRows();r++)
							for(int c=0;c<m_score.GetCols();c++)
								m_score.SetTextMatrix(r,c,"");
						UpdateFlexGrid(3,9,2,1);//还原网格为初始状态
						m_score.SetCols(m_score.GetCols()+c+1);//设置为初始列加c个学科列及1个备注列
						//对新增单元格进行初始化
						for(int r=0;r<m_score.GetRows();r++)
						{
							for(int c=9;c<m_score.GetCols();c++)
							{
								m_score.SetRow(long(r));
								m_score.SetCol(long(c));
								m_score.SetCellAlignment(4);
							}
						}
						//设定第一列可见
						m_score.SetLeftCol(1);
						//补充表头
						int n,x=0;
						for(n=9;n<c+10;n++)
						{
							if(n<c+9)
							{
								m_score.SetTextMatrix(0,n,"学        科");
								m_xueke.GetLBText(x,temp);
								m_score.SetTextMatrix(1,n,temp);
								x++;
							}
							else
							{
								m_score.SetTextMatrix(0,n,"备注");
								m_score.SetTextMatrix(1,n,"备注");
							}
						}
						m_score.SetMergeCol(n-1,TRUE);//此时的变量n在上面循环结束时也自加1,故需减1
						m_score.SetMergeRow(0,TRUE);
					}
				}
			}
		}
	}
}

void CInputData::OnShowWindow(BOOL bShow, UINT nStatus)
{
	CDialog::OnShowWindow(bShow, nStatus);

	if(bShow)
	{
		////从ini文件中构造班级字串////
		if(theApp.bExist)
		{
			int rcount,ccount;
			theApp.IniFile.GetItemInt("SCHOOL","row",rcount);//获到行数
			theApp.IniFile.GetItemInt("SCHOOL","col",ccount);//获取列数,不含序号列
			CString strvar,stritem,strleft,temp;//sstrvar为字符串变量,stritem为字符串变量的值,strleft为待截取的左子串
			int f;//记录分号出现的次数
			for(int r=1;r<=rcount;r++)
			{
				f=0;//每次查找之前将其置为0
				strvar.Format("var%d",r);//构造列号
				theApp.IniFile.GetItemString("CLASS",strvar,stritem);//获取字符串变量的值
				for(int c=0;c<ccount;c++)
				{
					if(stritem.Find(";")>=0)
					{
						f++;//找到时将出现次数自加1.
						if(stritem.Find(";",0)>0)//找到分隔符并且不位于首端
						{
							strleft=stritem.Left(stritem.Find(";",0)+1);//截取左子串(包含找到的分隔符)
							if(f==2)
								temp=strleft.Left(strleft.GetLength()-1);
							if(f==4)
								m_class.AddString(temp+strleft.Left(strleft.GetLength()-1));//此时构造完一个班级字符串,将其添加进班级下拉框
							stritem=stritem.Right(stritem.GetLength()-strleft.GetLength());//迭代改变本身值为右串
						}
						else
							if(stritem.Find(";",0)==0)
							stritem=stritem.Right(stritem.GetLength()-1);//分隔符位于首端时不截取左串,迭代改变本身值为去除首端分隔符后的右串。
					}
				}
			}
		}
		////构造完毕////
	}
	else
	{
		m_class.ResetContent();
		SetDlgItemText(IDC_NUMBER,"");
		SetDlgItemText(IDC_NAME,"");
		SetDlgItemText(IDC_SEG,"");
		m_scoreseg.ResetContent();
		m_xueke.ResetContent();
		m_sex.SetCurSel(-1);
		m_readem.SetCurSel(-1);
		SetDlgItemText(IDC_SCORE,"");
		for(int r=m_score.GetRows()-1;r>1;r--)//表头不清除
		{
			for(int c=0;c<m_score.GetCols();c++)
				m_score.SetTextMatrix(r,c,"");
			if(r>2)
				m_score.RemoveItem(r);
		}
	}
}

void CInputData::OnBnClickedAdd()
{
	if(theApp.bExist)
	{
		CString temp;
		CStringArray itemstr;
		theApp.IniFile.GetItemString("SCHOOL","name",temp);//从INI文件中获取学校名称
		itemstr.Add(temp);
		if(m_class.GetCurSel()>=0)//构造年级及班字串
		{
			m_class.GetLBText(m_class.GetCurSel(),temp);
			itemstr.Add(temp.Left(temp.Find("级",0)+2));//一个汉字占2字节,找到的是所处的起始位置,所以加2
			itemstr.Add(temp.Right(temp.GetLength()-temp.Find("级",0)-2));
		}
		else
		{
			GotoDlgCtrl(GetDlgItem(IDC_CLASS));
			MessageBox("请选择班级!","错误",MB_OK|MB_ICONERROR);
			itemstr.RemoveAll();
			return;
		}
		if(m_scoreseg.GetCurSel()>=0)
		{
			m_scoreseg.GetLBText(m_scoreseg.GetCurSel(),temp);
			itemstr.Add(temp);
		}
		else
		{
			GotoDlgCtrl(GetDlgItem(IDC_SCORESEG));
			MessageBox("请选择系别!","错误",MB_OK|MB_ICONERROR);
			itemstr.RemoveAll();
			return;
		}
		GetDlgItemText(IDC_NUMBER,temp);
		if(temp.IsEmpty())
		{
			GotoDlgCtrl(GetDlgItem(IDC_NUMBER));
			MessageBox("请填写学籍号!","错误",MB_OK|MB_ICONERROR);
			itemstr.RemoveAll();
			return;
		}
		if(!ExamNumber(temp))//检验学籍号的有效性
		{
			GotoDlgCtrl(GetDlgItem(IDC_NUMBER));
			MessageBox("非法学籍号,请检查!","错误",MB_OK|MB_ICONERROR);
			itemstr.RemoveAll();
			return;
		}
		////对学籍号添加前缀
		if(theApp.bExist)
		{
			int row;
			theApp.IniFile.GetItemInt("SCHOOL","row",row);//获取班级行数
			CString str,strnumber;
			BOOL per=FALSE;//是否找到对应班级的标志
			int r=1;
			CString ttemp=temp;//暂存学籍号
			while(r<=row && !per)//当未查找完并且没找到对应班级时继续,否则退出
			{
				str.Format("var%d",r);
				r++;
				theApp.IniFile.GetItemString("CLASS",str,temp);
				if(temp.Find(itemstr[1],0)>=0 && temp.Find(itemstr[2],0)>=0)//找到年级及班级时取出学籍号前缀
				{
					per=TRUE;
					temp=temp.Left(temp.ReverseFind(';'));
					temp=temp.Right(temp.GetLength()-temp.ReverseFind(';')-1);
					temp=temp+ttemp;//将取得的前缀与学籍号相连
				}
			}
		}
		////添加前缀完毕
		itemstr.Add(temp);
		GetDlgItemText(IDC_NAME,temp);
		if(temp.IsEmpty() || temp.Find(" ",0)>=0)
		{
			GotoDlgCtrl(GetDlgItem(IDC_NAME));
			MessageBox("请如实填写学生姓名!","错误",MB_OK|MB_ICONERROR);
			itemstr.RemoveAll();
			return;
		}
		else
			itemstr.Add(temp);
		if(m_sex.GetCurSel()>=0)
		{
			m_sex.GetLBText(m_sex.GetCurSel(),temp);
			itemstr.Add(temp);
		}
		else

⌨️ 快捷键说明

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