📄 paitxtdata.cpp
字号:
// PaiTxtData.cpp : 实现文件
//
#include "stdafx.h"
#include "StudentRecords.h"
#include "PaiTxtData.h"
// CPaiTxtData 对话框
IMPLEMENT_DYNAMIC(CPaiTxtData, CDialog)
CPaiTxtData::CPaiTxtData(CWnd* pParent /*=NULL*/)
: CDialog(CPaiTxtData::IDD, pParent)
{
}
CPaiTxtData::~CPaiTxtData()
{
}
void CPaiTxtData::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_CLASS, m_class);
DDX_Control(pDX, IDC_LIST1, m_list1);
DDX_Control(pDX, IDC_INDEX, m_index);
}
BEGIN_MESSAGE_MAP(CPaiTxtData, CDialog)
ON_BN_CLICKED(IDC_SORT, &CPaiTxtData::OnBnClickedSort)
ON_BN_CLICKED(IDC_SAVETXT, &CPaiTxtData::OnBnClickedSavetxt)
ON_WM_MOUSEWHEEL()
ON_WM_SHOWWINDOW()
ON_BN_CLICKED(IDC_NEWVIEW, &CPaiTxtData::OnBnClickedNewview)
ON_CBN_SELCHANGE(IDC_CLASS, &CPaiTxtData::OnCbnSelchangeClass)
END_MESSAGE_MAP()
// CPaiTxtData 消息处理程序
BOOL CPaiTxtData::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message==WM_KEYDOWN && pMsg->wParam==VK_RETURN)
{
OnBnClickedSort();
return true;
}
if(pMsg->message==WM_KEYDOWN && pMsg->wParam==VK_ESCAPE)
return true;
return CDialog::PreTranslateMessage(pMsg);
}
BOOL CPaiTxtData::OnInitDialog()
{
CDialog::OnInitDialog();
UpdateFlexGrid(3,9,2,1);
CheckRadioButton(IDC_RADIO1,IDC_RADIO2,IDC_RADIO1);
return TRUE;
}
void CPaiTxtData::UpdateFlexGrid(int m,int n,int fm,int fn)
{
m_list1.Clear();
m_list1.SetRows(m);
m_list1.SetCols(n);
m_list1.SetFixedRows(fm);
m_list1.SetFixedCols(fn);
m_list1.SetSelectionMode(0);
m_list1.SetMergeCells(3);//此风格的设定与上一句联合使用,后面用m_list1.SetMergeCol(x,TRUE);可合并单元格
m_list1.SetRowHeightMin(260);
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
m_list1.SetRow(long(i));
m_list1.SetCol(long(j));
m_list1.SetCellAlignment(4);
}
}
CString str;
str="序号";
m_list1.SetTextMatrix(0,0,str);
m_list1.SetTextMatrix(1,0,str);
m_list1.SetColWidth(0,480);
str="学 校";
m_list1.SetTextMatrix(0,1,str);
m_list1.SetTextMatrix(1,1,str);
m_list1.SetColWidth(1,2200);
str="年 级";
m_list1.SetTextMatrix(0,2,str);
m_list1.SetTextMatrix(1,2,str);
m_list1.SetColWidth(2,1200);
str="班";
m_list1.SetTextMatrix(0,3,str);
m_list1.SetTextMatrix(1,3,str);
m_list1.SetColWidth(3,720);
str="系 别";
m_list1.SetTextMatrix(0,4,str);
m_list1.SetTextMatrix(1,4,str);
m_list1.SetColWidth(4,1600);
str="学 籍 号";
m_list1.SetTextMatrix(0,5,str);
m_list1.SetTextMatrix(1,5,str);
m_list1.SetColWidth(5,1400);
str="姓 名";
m_list1.SetTextMatrix(0,6,str);
m_list1.SetTextMatrix(1,6,str);
m_list1.SetColWidth(6,800);
str="性别";
m_list1.SetTextMatrix(0,7,str);
m_list1.SetTextMatrix(1,7,str);
m_list1.SetColWidth(7,540);
str="年龄";
m_list1.SetTextMatrix(0,8,str);
m_list1.SetTextMatrix(1,8,str);
m_list1.SetColWidth(8,540);
m_list1.SetRowHeight(m_list1.GetRows()-1,320);
for(int c=0;c<n;c++)
m_list1.SetMergeCol(c,TRUE);
}
void CPaiTxtData::OnBnClickedSort()
{
if(m_index.GetCurSel()<0)
{
MessageBox("请选择一个排序的关键字!","提示",MB_OK|MB_ICONASTERISK);
return;
}
CFileRandom filerandom;
int iRadioButton=GetCheckedRadioButton(IDC_RADIO1,IDC_RADIO2);
if(iRadioButton==IDC_RADIO1)
SortNewView(TRUE,m_index.GetCurSel()+4,0);//表中前4个字段未用,为了能在文件的一行中查找,所以加4,0表示降序
if(iRadioButton==IDC_RADIO2)
SortNewView(TRUE,m_index.GetCurSel()+4,1);//表中前4个字段未用,为了能在文件的一行中查找,所以加4,1表示升序
}
void CPaiTxtData::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);
}
void CPaiTxtData::OnBnClickedNewview()
{
if(theApp.bExist)//如果找开依据配置文件所与选班级构造文件名
{
SortNewView(FALSE);
}
else
{
MessageBox("当前目录下没有发现配置文件!","提示",MB_OK|MB_ICONASTERISK);
return;
}
}
void CPaiTxtData::SortNewView(BOOL sort,int col,int type)
{
CString temp;
theApp.IniFile.GetItemString("SCHOOL","name",temp);//获取学校名称
if(m_class.GetCurSel()>=0)
{
CString str;
m_class.GetLBText(m_class.GetCurSel(),str);//获得年级班
temp+=str;
temp+="学籍.bin";
CString records="";
//检验文件是否存在
CString path;
path=AfxGetApp()->m_pszHelpFilePath;
str=AfxGetApp()->m_pszExeName;
path=path.Left(path.GetLength()-str.GetLength()-4);
CFileFind find;
BOOL have=find.FindFile(path+temp,0);
find.Close();
if(have)
{
if(!sort)
if(!(m_list1.GetTextMatrix(2,1).IsEmpty()))
if(MessageBox("在刷新显示之前请确认数据是否保存!","提示",MB_YESNO|MB_ICONASTERISK|MB_DEFBUTTON2)==IDNO)
return;
CFileRandom filerandom;
filerandom.Read(path+temp);
if(sort)
filerandom.DescentSort(col,type);
filerandom.Display(records);
m_list1.ShowWindow(SW_HIDE);
//对表初始化为3行,并清空除表头的单元格
m_list1.SetRows(3);
for(int c=0;c<m_list1.GetCols();c++)
m_list1.SetTextMatrix(2,c,"");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -