📄 querydata.cpp
字号:
}
}
int r=0,row=2;
//待获取列表字符串及数组字符串
CString liststr,arraystr;
BOOL have=FALSE;//表中是否存在该学生信息,若存在则将行坐标定位于此,同时置该标志为真
while(row<m_list1.GetRows() && !have)
{
liststr.Empty();
arraystr.Empty();
for(int n=1;n<6;n++)
{
liststr+=m_list1.GetTextMatrix(row,n);
arraystr+=itemstr[n-1];
}
if(liststr==arraystr)
{
r=row;
have=TRUE;
}
row++;
}
if(have)
{
//填充前8个字段(单元格)
int x;
for(x=0;x<8;x++)
m_list1.SetTextMatrix(r,x+1,itemstr[x]);
//确定成绩所在列号,填充成绩及备注信息
int rc=9,c=0;//rc为列,起始坐标定位为9
while(c==0 && rc<m_list1.GetCols())
{
if(m_list1.GetTextMatrix(1,rc)==itemstr[x])//此时的X已在上面的循环结束后后加1
c=rc;
rc++;
}
m_list1.SetTextMatrix(r,c,itemstr[x+1]);//成绩信息位于科目信息后
m_list1.SetTextMatrix(r,m_list1.GetCols()-1,itemstr[itemstr.GetCount()-1]);//备注信息位于最末
itemstr.RemoveAll();//清空string型数组
//填充序号
int no=0;
for(int m=2;m<m_list1.GetRows();m++)
if(!(m_list1.GetTextMatrix(m,1).IsEmpty()))
{
no++;
if(m_list1.GetTextMatrix(m,0).IsEmpty())
{
temp.Format("%d",no);
m_list1.SetTextMatrix(m,0,temp);
}
}
}
else
MessageBox("在修改之前请选中要修改的记录!","错误",MB_OK|MB_ICONERROR);
GotoDlgCtrl(GetDlgItem(IDC_CLASS));//将光标移动至学籍号处
}
else
MessageBox("配置文件错误!","错误",MB_OK|MB_ICONERROR);
}
BOOL CQueryData::ExamNumber(CString strnumber)//校验学号的有效性,参数为接收到的学号
{
BOOL have=FALSE;//有效性标志,初始为假
//获取年级及班字串,再根据两个字串在配置文件中查找对应的班,然后取学籍号检查
if(m_class.GetCurSel()>=0)//构造年级及班字串
{
CString temp,gradestr,classstr;
m_class.GetLBText(m_class.GetCurSel(),temp);
gradestr=temp.Left(temp.Find("级",0)+2);//一个汉字占2字节,找到的是所处的起始位置,所以加2
classstr=temp.Right(temp.GetLength()-temp.Find("级",0)-2);
////从INI文件读取数据
if(theApp.bExist)
{
int row;
theApp.IniFile.GetItemInt("SCHOOL","row",row);//获到行数
CString strvar,stritem,strleft;//strvar为字符串变量,stritem为字符串变量的值,strleft为待截取的左子串
int r=1;
while(r<=row && !have)
{
strvar.Format("var%d",r);//构造列号
r++;
theApp.IniFile.GetItemString("CLASS",strvar,stritem);//获取字符串变量的值
if(stritem.Find(gradestr,0)>=0 && stritem.Find(classstr,0)>=0)//两者都找到,表示此行数据是该班级的配置数据
{
CString strstart,strend;
strstart=stritem.Mid(stritem.ReverseFind(';')+1,stritem.Find("~",0)-stritem.ReverseFind(';')-1);
strend=stritem.Right(stritem.GetLength()-stritem.Find("~",0)-2);
//学籍号的范围必须在已经设定的域内并且位数也在它们之间
if(atoi(strnumber)>=atoi(strstart) && atoi(strnumber)<=atoi(strend) && strnumber.GetLength()>=strstart.GetLength() && strnumber.GetLength()<=strend.GetLength())
have=TRUE;
}
}
}
}
else
{
GotoDlgCtrl(GetDlgItem(IDC_CLASS));
MessageBox("请先选择班级!","错误",MB_OK|MB_ICONERROR);
}
return have;
}
void CQueryData::OnBnClickedDel()
{
int row=m_list1.GetRow();
if(row>2 && !(m_list1.GetTextMatrix(2,1).IsEmpty()))
{
CString temp,message=_T("将要删除如下记录,是否继续?\r\n");
for(int c=1;c<m_list1.GetCols();c++)
{
temp+=m_list1.GetTextMatrix(row,c);
if(c<m_list1.GetCols()-1)
temp+=",";
}
if(MessageBox(message+temp,"警告",MB_YESNO|MB_ICONASTERISK|MB_DEFBUTTON2)==IDYES)
{
if(!(m_list1.GetTextMatrix(2,0).IsEmpty()) && m_list1.GetRows()==4/*之所以为4,是因为在此时的最末行其实为一高度为0的空行*/ && row==2)//序号不为空并且此行为末行时改为清空数据
{
m_list1.RemoveItem(row+1);//删除其下的空行
for(int c=0;c<m_list1.GetCols();c++)
m_list1.SetTextMatrix(row,c,"");
}
else
{
m_list1.RemoveItem(row);
//m_list1.RemoveItem(row+1);//删除其下的空行
int no=0;
for(int m=2;m<m_list1.GetRows();m++)
{
if(!(m_list1.GetTextMatrix(m,1).IsEmpty()))
{
no++;
temp.Format("%d",no);
m_list1.SetTextMatrix(m,0,temp);
}
}
}
}
}
else
MessageBox("执行删除前请先选择对应数据!","提示",MB_OK|MB_ICONASTERISK);
}
void CQueryData::OnBnClickedSavetxt()
{
//检查表中是否有数据
if(m_list1.GetTextMatrix(2,1).IsEmpty())
{
MessageBox("没有任何数据可保存!","提示",MB_OK|MB_ICONASTERISK);
return;
}
//构造文件名
CString filename,deffilename;
for(int c=1;c<=3;c++)
deffilename+=m_list1.GetTextMatrix(2,c);
deffilename+="学籍.bin";
CFileDialog FileDlg( FALSE, "bin", deffilename, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "BIN Files(*.bin)|*.bin||" );
FileDlg.m_ofn.lpstrTitle="文件另存为";
if( FileDlg.DoModal() == IDCANCEL )
return;
filename=FileDlg.GetPathName();
//检查是否有同名文件
CFileFind find;
BOOL have=find.FindFile(filename,0);
find.Close();
if(have)
::remove(filename);
CFileRandom filerandom;
for(int r=2;r<m_list1.GetRows();r++)//列表前两行是表头,故不获取。
{
if(!(m_list1.GetTextMatrix(r,1).IsEmpty()))
{
char item[512];
CString temp;
for(int c=1;c<m_list1.GetCols();c++)//列表的首列为序号,故不获取。
{
temp+=m_list1.GetTextMatrix(r,c);
if(c<m_list1.GetCols()-1)
temp+=';';//项间以分号相隔
else
temp+='/';//记录间以星号相隔
}
strcpy(item,(LPCTSTR)temp);//,temp.GetLength()
filerandom.add(item);//追加记录至链表
}
}
filerandom.save(filename);//将链表保存至保存文件
MessageBox("已将数据成功保存为以下文件:\r\n"+filename,"提示",MB_OK|MB_ICONASTERISK);
}
BOOL CQueryData::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
if(m_list1.GetRows()>3)
{
int nPos = m_list1.GetScrollPos(SB_VERT); //获得当前垂直滚动条位置
int nMax = m_list1.GetScrollLimit(SB_VERT); //获得当前垂直滚动条位置范围的最大值
if(zDelta < 0) //判断滚动方向为向下滚动
{
nPos += 3;
(nPos >= nMax) ? (nPos = nMax-1) : NULL;
}
else
{
nPos -= 3;
(nPos <= 1) ? (nPos = 1) : NULL;
}
m_list1.SetTopRow(nPos+1);//固一行为2,所以加1
}
return CDialog::OnMouseWheel(nFlags, zDelta, pt);
}
void CQueryData::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_list1.GetRows()-1;r>1;r--)//表头不清除
{
for(int c=0;c<m_list1.GetCols();c++)
m_list1.SetTextMatrix(r,c,"");
if(r>2)
m_list1.RemoveItem(r);
}
}
}
BEGIN_EVENTSINK_MAP(CQueryData, CDialog)
ON_EVENT(CQueryData, IDC_LIST1, DISPID_CLICK, CQueryData::ClickList1, VTS_NONE)
END_EVENTSINK_MAP()
void CQueryData::ClickList1()
{
int row=m_list1.GetRow();
m_class.SetCurSel(m_class.FindString(0,m_list1.GetTextMatrix(row,2)+m_list1.GetTextMatrix(row,3)));
m_scoreseg.SetCurSel(m_scoreseg.FindString(0,m_list1.GetTextMatrix(row,4)));
CString temp(_T(""));
if(theApp.bExist)//从配置文件中获取该班学籍号前缀
{
int row;
theApp.IniFile.GetItemInt("SCHOOL","row",row);//获取班级行数
CString str,strnumber;
BOOL per=FALSE;//是否找到对应班级的标志
int r=1;
while(r<=row && !per)//当未查找完并且没找到对应班级时继续,否则退出
{
str.Format("var%d",r);
r++;
theApp.IniFile.GetItemString("CLASS",str,temp);
if(temp.Find(m_list1.GetTextMatrix(row,2),0)>=0 && temp.Find(m_list1.GetTextMatrix(row,3),0)>=0)//找到年级及班级时取出学籍号前缀
{
per=TRUE;
temp=temp.Left(temp.ReverseFind(';'));
temp=temp.Right(temp.GetLength()-temp.ReverseFind(';')-1);
}
}
}
SetDlgItemText(IDC_NUMBER,m_list1.GetTextMatrix(row,5).Right(m_list1.GetTextMatrix(row,5).GetLength()-temp.GetLength()));
SetDlgItemText(IDC_NAME,m_list1.GetTextMatrix(row,6));
m_sex.SetCurSel(m_sex.FindString(0,m_list1.GetTextMatrix(row,7)));
SetDlgItemText(IDC_SEG,m_list1.GetTextMatrix(row,8));
m_xueke.SetCurSel(-1);
SetDlgItemText(IDC_SCORE,"");
m_readem.SetCurSel(m_readem.FindString(0,m_list1.GetTextMatrix(row,m_list1.GetCols()-1)));
}
void CQueryData::OnCbnSelchangeClass()
{
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_scoreseg.AddString("无");
m_xueke.ResetContent();
m_xueke.AddString("不选");
SetDlgItemText(IDC_SCORE,"");
m_sex.SetCurSel(-1);
m_readem.SetCurSel(-1);
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("无");//没有分系则为“无”
//系别填写完毕,开始填写学科
//截取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_list1.GetRows();r++)
for(int c=0;c<m_list1.GetCols();c++)
m_list1.SetTextMatrix(r,c,"");
UpdateFlexGrid(3,9,2,1);//还原网格为初始状态
m_list1.SetCols(m_list1.GetCols()+c+1);//设置为初始列加c个学科列及1个备注列
//对新增单元格进行初始化
for(int r=0;r<m_list1.GetRows();r++)
{
for(int c=9;c<m_list1.GetCols();c++)
{
m_list1.SetRow(long(r));
m_list1.SetCol(long(c));
m_list1.SetCellAlignment(4);
}
}
//设定第一列可见
m_list1.SetLeftCol(1);
//补充表头
int n,x=0;
for(n=9;n<c+10;n++)
{
if(n<c+9)
{
m_list1.SetTextMatrix(0,n,"学 科");
m_xueke.GetLBText(x,temp);
m_list1.SetTextMatrix(1,n,temp);
x++;
}
else
{
m_list1.SetTextMatrix(0,n,"备注");
m_list1.SetTextMatrix(1,n,"备注");
}
}
m_list1.SetMergeCol(n-1,TRUE);//此时的变量n在上面循环结束时也自加1,故需减1
m_list1.SetMergeRow(0,TRUE);
}
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -