📄 indexset.cpp
字号:
int rcount=m_list1.GetRows();//得到总行数
int row=0;//待写数据的行坐标
CString str;
for(int r=1;r<rcount;r++)
{
for(int c=1;c<5;c++)
str+=m_list1.GetTextMatrix(r,c);
if(str==itemstr[0]+itemstr[1]+itemstr[2]+itemstr[3])
row=r;
str.Empty();
}
if(row==0)
{
if(m_list1.GetTextMatrix(1,1).IsEmpty())
row=1;
else
{
m_list1.SetRows(rcount+1);
for(int c=0;c<m_list1.GetCols();c++)
{
m_list1.SetRow(rcount);
m_list1.SetCol(c);
m_list1.SetCellAlignment(4);
}
row=rcount;
}
}
str.Format("%d",row);
m_list1.SetTextMatrix(row,0,str);//填写序号
for(int c=0;c<m_list1.GetCols()-1;c++)
if(c<m_list1.GetCols()-2)
m_list1.SetTextMatrix(row,c+1,itemstr[c]);
else
m_list1.SetTextMatrix(row,c+1,itemstr[c]+"~"+itemstr[c+1]);
itemstr.RemoveAll();
}
void CIndexSet::OnBnClickedDel()
{
int row=m_list1.GetRow();
if(row>0)
{
CString message;
message.Format("将要删除第%d记录,是否继续?",row);
if(MessageBox(message,"警告",MB_YESNO|MB_ICONASTERISK|MB_DEFBUTTON2)==IDYES)
{
if(!(m_list1.GetTextMatrix(1,0).IsEmpty()) && m_list1.GetRows()==2 && row==1)//序号不为空但此行是最末行
for(int col=0;col<m_list1.GetCols();col++)
m_list1.SetTextMatrix(row,col,"");
else
{
CString str;
m_list1.RemoveItem(row);
for(int r=row;r<m_list1.GetRows();r++)
{
str.Format("%d",r);
m_list1.SetTextMatrix(r,0,str);
}
}
}
}
else
MessageBox("执行删除前请先选择对应数据!","提示",MB_OK|MB_ICONASTERISK);
}
void CIndexSet::OnBnClickedSavetofile()
{
theApp.IniFile.CloseIniFile();
CString str,path;
path=AfxGetApp()->m_pszHelpFilePath;
str=AfxGetApp()->m_pszExeName;
path=path.Left(path.GetLength()-str.GetLength()-4);
theApp.IniFile.SetName(AfxGetApp()->m_pszProfileName);
theApp.IniFile.SetPath(path);
theApp.bExist=theApp.IniFile.OpenIniFileForWrite();
if(theApp.bExist)
{
theApp.IniFile.WriteSection("SCHOOL");
theApp.IniFile.WriteItemString("name",m_list1.GetTextMatrix(1,1));
int gradesum=0;
////设置辅助(标志)数组,并初始化为0////
int *p=new int[m_list1.GetRows()-1];
for(int r=0;r<m_list1.GetRows()-1;r++)
p[r]=0;
////对网格控件中的第列值为2的列进行索引,如有相同的值即要辅助数组内将相应下标的元素置为1////
for(int r=1;r<m_list1.GetRows();r++)
for(int r1=1;r1<m_list1.GetRows();r1++)
if(r1!=r && p[r1-1]==0)//外循环与内循环下标不相同并且辅助数组相应下标的元素分仍为初始值时进行才进行比较
if(m_list1.GetTextMatrix(r,2)==m_list1.GetTextMatrix(r1,2))
p[r-1]=1;//两者值相同时辅助数组对应下标值改为1
for(int r=0;r<m_list1.GetRows()-1;r++)//找出辅助数组中值仍为初始值的元素个数,即为实求数组中不相同元素的个数
if(p[r]==0)
gradesum++;
delete p;
theApp.IniFile.WriteItemInt("grade",gradesum);
theApp.IniFile.WriteItemInt("row",m_list1.GetRows()-1);
theApp.IniFile.WriteItemInt("col",m_list1.GetCols()-1);
theApp.IniFile.WriteString("\n");
theApp.IniFile.WriteSection("CLASS");
CString itemstr;
for(int r=1;r<m_list1.GetRows();r++)
{
for(int c=1;c<m_list1.GetCols();c++)
{
itemstr+=m_list1.GetTextMatrix(r,c);
if(c<m_list1.GetCols()-1)
itemstr+=";";
}
theApp.IniFile.WriteItemString("var"+m_list1.GetTextMatrix(r,0),itemstr);
itemstr.Empty();
}
theApp.IniFile.CloseIniFile();
theApp.OpenMyIniFile();
MessageBox("保存完毕!","提示",MB_OK|MB_ICONASTERISK);
}
else
{
AfxMessageBox("无法保存 Ini 文件!",MB_OK);
}
}
void CIndexSet::FromIniFile()
{
if(theApp.bExist)
{
int rcount,ccount;
theApp.IniFile.GetItemInt("SCHOOL","row",rcount);//获到行数
theApp.IniFile.GetItemInt("SCHOOL","col",ccount);//获取列数,不含序号列
m_list1.SetRows(rcount+1);
CString str,strvar,stritem,strleft;//str为列号,strvar为字符串变量,stritem为字符串变量的值,strleft为待截取的左子串
for(int r=1;r<=rcount;r++)
{
strvar.Format("var%d",r);//构造列号
theApp.IniFile.GetItemString("CLASS",strvar,stritem);//获取字符串变量的值
for(int c=0;c<m_list1.GetCols();c++)
{
m_list1.SetRow(r);
m_list1.SetCol(c);
m_list1.SetCellAlignment(4);
if(c>0)//开始从已获取的字符串变量(stritem)中获取单个以分号分隔的子串填充相应单元格
{
if(stritem.Find(";",0)>0)//找到分隔符并且不位于首端
{
strleft=stritem.Left(stritem.Find(";",0)+1);//截取左子串(包含找到的分隔符)
m_list1.SetTextMatrix(r,c,strleft.Left(strleft.Find(";")));//对相应单元格赋值为左子串(但不含分隔符)
stritem=stritem.Right(stritem.GetLength()-strleft.GetLength());//迭代改变本身值为右串
}
else
if(stritem.Find(";",0)==0)
stritem=stritem.Right(stritem.GetLength()-1);//分隔符位于首端时不截取左串,迭代改变本身值为去除首端分隔符后的右串,此时不对相应单元格赋值。
else
m_list1.SetTextMatrix(r,c,stritem);//此时没找到分隔符,说明已经是末尾字串,所以对相应单元格直接用其赋值之
}
}
str.Format("%d",r);
m_list1.SetTextMatrix(r,0,str);//设置序号
}
}
}
BEGIN_EVENTSINK_MAP(CIndexSet, CDialog)
ON_EVENT(CIndexSet, IDC_LIST1, DISPID_CLICK, CIndexSet::ClickList1, VTS_NONE)
END_EVENTSINK_MAP()
void CIndexSet::ClickList1()//单击行时,将数据对应显示在编辑控件内,数据为表格中实有数据
{
int gradesum=0;//年级数
int classsum=0;//该年级班级数
if(theApp.bExist)//如果配置文件存在,年级数及年级的班级数从配置文件中获取
{
theApp.IniFile.GetItemInt("SCHOOL","row",gradesum);//获得总班级数
CString gradename=m_list1.GetTextMatrix(m_list1.GetRow(),2);
CString str,temp;
CStringArray classstr;
for(int r=1;r<=gradesum;r++)
{
str.Format("var%d",r);
theApp.IniFile.GetItemString("CLASS",str,temp);
str=temp.Left(temp.Find("级",0)+2);
str=str.Right(str.GetLength()-str.ReverseFind(';')-1);//获得年级信息以作参照
if(str==gradename)//如果所选行的年级信息与读出的年级信息相符合时进行获取该年级的班数
{
temp=temp.Left(temp.Find("班",0));
temp=temp.Right(temp.GetLength()-temp.ReverseFind(';')-1);//获得班的序号
classstr.Add(temp);
}
}
for(int n=0;n<classstr.GetCount();n++)
{
if(atoi(classstr[n])>classsum)
classsum=atoi(classstr[n]);
}
}
else
{
////设置辅助(标志)数组,并初始化为0////
int *p=new int[m_list1.GetRows()-1];
for(int r=0;r<m_list1.GetRows()-1;r++)
p[r]=0;
////对网格控件中的第列值为2的列进行索引,如有相同的值即要辅助数组内将相应下标的元素置为1////
for(int r=1;r<m_list1.GetRows();r++)
for(int r1=1;r1<m_list1.GetRows();r1++)
if(r1!=r && p[r1-1]==0)//外循环与内循环下标不相同并且辅助数组相应下标的元素分仍为初始值时进行才进行比较
if(m_list1.GetTextMatrix(r,2)==m_list1.GetTextMatrix(r1,2))
p[r-1]=1;//两者值相同时辅助数组对应下标值改为1
for(int r=0;r<m_list1.GetRows()-1;r++)//找出辅助数组中值仍为初始值的元素个数,即为实求数组中不相同元素的个数
if(p[r]==0)
gradesum++;
delete p;
for(int r=1;r<m_list1.GetRows();r++)
if(m_list1.GetTextMatrix(r,2)==m_list1.GetTextMatrix(m_list1.GetRow(),2))
classsum++;//该年级的班数
}
SetDlgItemText(IDC_EDIT1,m_list1.GetTextMatrix(m_list1.GetRow(),1));
SetDlgItemInt(IDC_EDIT2,gradesum,1);
if(gradesum>0)
m_grade.SetCurSel(m_grade.FindString(0,m_list1.GetTextMatrix(m_list1.GetRow(),2)));
SetDlgItemText(IDC_EDIT6,m_list1.GetTextMatrix(m_list1.GetRow(),3));
SetDlgItemInt(IDC_EDIT3,classsum,1);
if(classsum>0)
m_class.SetCurSel(m_class.FindString(0,m_list1.GetTextMatrix(m_list1.GetRow(),4)));
SetDlgItemText(IDC_EDIT7,m_list1.GetTextMatrix(m_list1.GetRow(),5));
SetDlgItemText(IDC_EDIT8,m_list1.GetTextMatrix(m_list1.GetRow(),6));
SetDlgItemText(IDC_EDIT4,m_list1.GetTextMatrix(m_list1.GetRow(),7).Left(m_list1.GetTextMatrix(m_list1.GetRow(),7).Find("~")));
SetDlgItemText(IDC_EDIT5,m_list1.GetTextMatrix(m_list1.GetRow(),7).Right(m_list1.GetTextMatrix(m_list1.GetRow(),7).GetLength()-m_list1.GetTextMatrix(m_list1.GetRow(),7).Find("~")-2));
}
BOOL CIndexSet::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)//让对话框支持鼠标滚轮事件,事件发生时主要对网格控件处理
{
if(m_list1.GetRows()>2)
{
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);
}
return CDialog::OnMouseWheel(nFlags, zDelta, pt);
}
void CIndexSet::OnShowWindow(BOOL bShow, UINT nStatus)//对话框显示时列表重新获取数据,隐藏时清除数据
{
CDialog::OnShowWindow(bShow, nStatus);
if(bShow)
FromIniFile();
else
{
SetDlgItemText(IDC_EDIT1,"");
SetDlgItemText(IDC_EDIT2,"");
SetDlgItemText(IDC_EDIT3,"");
SetDlgItemText(IDC_EDIT4,"");
SetDlgItemText(IDC_EDIT5,"");
SetDlgItemText(IDC_EDIT6,"");
SetDlgItemText(IDC_EDIT7,"");
SetDlgItemText(IDC_EDIT8,"");
m_grade.ResetContent();
m_class.ResetContent();
for(int r=m_list1.GetRows()-1;r>0;r--)//表头不清除
{
for(int c=0;c<m_list1.GetCols();c++)
m_list1.SetTextMatrix(r,c,"");
if(r>1)
m_list1.RemoveItem(r);
}
}
}
void CIndexSet::OnBnClickedNew()
{
FromIniFile();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -