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

📄 main1.cpp

📁 一个evc下的关于文件操作的程序
💻 CPP
字号:
// Main1.cpp : implementation file
//

#include "stdafx.h"
#include "FileExam.h"
#include "Main1.h"
#include "FileExamDlg.h"
#include "hint.h"
#include "Search.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//引用外部变量
//extern  CStudentData  studentdata;
extern  const LPCTSTR  studentfilepath;
extern  CList<CStudentData,CStudentData&>  m_lststudent;
extern  int  btn1;
extern  int  item;
extern  CString  key;
extern   int btnsearch;//判断是否执行了查找
int m_operID;
/*int btn;*/
int listnum; //选择的第几条记录
int findpos[10000];
int num;//找到的记录数
int hinttext;
/////////////////////////////////////////////////////////////////////////////
// CMain dialog


CMain::CMain(CWnd* pParent /*=NULL*/)
	: CDialog(CMain::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMain)
	//}}AFX_DATA_INIT
}


void CMain::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMain)
	DDX_Control(pDX, IDC_display, m_btndisplay);
	DDX_Control(pDX, IDC_count, m_count);
	DDX_Control(pDX, IDC_btndelete, m_btndelete);
	DDX_Control(pDX, IDC_edit, m_btnedit);
	DDX_Control(pDX, IDC_btnsearch, m_btnsearch);
	DDX_Control(pDX, IDC_btnnew, m_btnnew);
	DDX_Control(pDX, IDC_StuList, m_stulist);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMain, CDialog)
	//{{AFX_MSG_MAP(CMain)
	ON_BN_CLICKED(IDC_btnnew, Onbtnnew)
	ON_BN_CLICKED(IDC_edit, Onedit)
	ON_NOTIFY(NM_DBLCLK, IDC_StuList, OnDblclkStuList)
	ON_BN_CLICKED(IDC_btndelete, Onbtndelete)
	ON_BN_CLICKED(IDC_btnsearch, Onbtnsearch)
	ON_BN_CLICKED(IDC_display, Ondisplay)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMain message handlers

BOOL CMain::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_count.ShowWindow(SW_HIDE);
	CStudentData  studentdata;
	ShowWindow(SW_SHOWMAXIMIZED);
	setstulistcontent();
	CFile stufile;
	DWORD dwread;
	if(stufile.Open(studentfilepath,CFile::modeRead))
	{
		do 
		{
			dwread=stufile.Read(&studentdata,sizeof(studentdata));
			if(dwread!=0)
			{
				m_lststudent.AddTail(studentdata);
			}
			
		} while(dwread>0);
		//关闭数据库
		stufile.Close();
	}
	else //创建文件
	{
		if(!stufile.Open(studentfilepath,CFile::modeCreate|CFile::modeWrite))
		{
			AfxMessageBox(L"创建数据库失败!");
			return false;
		}
		stufile.Close();	
	}
	listcontent();//列表显示记录
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
//设置记录列表显示及控件大小和位置
void CMain::setstulistcontent()
{
	CRect rect;
	GetClientRect(&rect);
	int  L,h;
	L=int(rect.right);
	h=int(rect.bottom);
	m_stulist.MoveWindow(2,2,L-2,h-55);
	m_count.MoveWindow(2,h-47,L-int(3*L/8)-2,20);
	m_btndisplay.MoveWindow(L-int(3*L/8),h-52,int(3*L/8),25);
	m_btnnew.MoveWindow(2,h-25,int(L/4),25);
	m_btnedit.MoveWindow(int(L/4),h-25,int(L/4)+1,25);
	m_btndelete.MoveWindow(int(L/2),h-25,int(L/4)+1,25);
	m_btnsearch.MoveWindow(int(3*L/4),h-25,int(L/4)+1,25);
	m_stulist.InsertColumn(0,L"学号",LVCFMT_LEFT,int(L/3)-2);
	m_stulist.InsertColumn(1,L"姓名",LVCFMT_LEFT,int(L/3)-2);
	m_stulist.InsertColumn(2,L"班级",LVCFMT_LEFT,int(L/3));

}
//新建
void CMain::Onbtnnew() 
{
	// TODO: Add your control notification handler code here
/*	btn=1;*/
/*	hinttext=2;*/
	m_count.ShowWindow(SW_HIDE);
	m_operID=0;//表示新建操作
	CFileExamDlg newcreate;
	if(newcreate.DoModal()==IDOK)
	{
		
		listcontent();

	}

}
//列表显示记录
void CMain::listcontent()
{
	CStudentData  studentdata;
	POSITION pos;
	if(m_stulist.DeleteAllItems())
	for(int i=0;i<m_lststudent.GetCount();i++)
	{
		ZeroMemory(&studentdata,sizeof(studentdata));
		pos=m_lststudent.FindIndex(i);	
		studentdata=m_lststudent.GetAt(pos);

		m_stulist.InsertItem(i,studentdata.xsbh);
		m_stulist.SetItemText(i,1,studentdata.xsxm);
		m_stulist.SetItemText(i,2,studentdata.bjmc);
	}
}

void CMain::Onedit() 
{
	// TODO: Add your control notification handler code here
    /*btn=2;*/
    CFileExamDlg edit;
    m_count.ShowWindow(SW_HIDE);
	switch(btnsearch)
	{
    case 0:
		m_operID=1;//查找前的编辑
		//取得选择的是第几条记录
		listnum=m_stulist.GetNextItem( -1, LVNI_ALL | LVNI_SELECTED);
		if(listnum>=0 && listnum<m_lststudent.GetCount())
			if(edit.DoModal()==IDOK)
			{
				listcontent();
			}
    	break;
    case 1:
		m_operID=2;//查找后的编辑
		//取得选择的是第几条记录
		listnum=m_stulist.GetNextItem( -1, LVNI_ALL | LVNI_SELECTED);
		if(listnum>=0 && listnum<m_lststudent.GetCount())
			if(edit.DoModal()==IDOK)
			{
				listsearchresult();
			}
    	break;
   
    }





}
void CMain::OnDblclkStuList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	Onedit();
	*pResult = 0;
}

void CMain::Onbtndelete() 
{
	// TODO: Add your control notification handler code here
	hinttext=1;
	m_count.ShowWindow(SW_HIDE);
	Chint hint;
	switch(btnsearch) {
	case 0://没有执行查找
		
		listnum=m_stulist.GetNextItem( -1, LVNI_ALL | LVNI_SELECTED);
		if(listnum==-1)
		{
			AfxMessageBox(L"请选择要删除的记录!");
			return;
		}
		else
			if(hint.DoModal()==IDOK)
			{	
			POSITION pos;
			pos=m_lststudent.FindIndex(listnum);
			//删除队列中的记录
			m_lststudent.RemoveAt(pos);
			//将文件重写
			CStudentData studentdata;
			CFile stufile;
			stufile.Open(studentfilepath,CFile::modeCreate|CFile::modeWrite);
			pos=m_lststudent.GetHeadPosition();
			for(int i=0;i<m_lststudent.GetCount();i++)
			{
				ZeroMemory(&studentdata,sizeof(studentdata));
				studentdata=m_lststudent.GetNext(pos);
				stufile.Write(&studentdata,sizeof(studentdata));
			}
			stufile.Close();
			}
			listcontent();
		break;
	case 1:
		
		listnum=m_stulist.GetNextItem( -1, LVNI_ALL | LVNI_SELECTED);
		if(listnum==-1)
		{
			AfxMessageBox(L"请选择要删除的记录!");
			return;
		}
		else
			if(hint.DoModal()==IDOK)
			{	
			POSITION pos;
			pos=m_lststudent.FindIndex(findpos[listnum]);
			//删除队列中的记录
			m_lststudent.RemoveAt(pos);
			//将文件重写
			CStudentData studentdata;
			CFile stufile;
			stufile.Open(studentfilepath,CFile::modeCreate|CFile::modeWrite);
			pos=m_lststudent.GetHeadPosition();
			for(int i=0;i<m_lststudent.GetCount();i++)
			{
				ZeroMemory(&studentdata,sizeof(studentdata));
				studentdata=m_lststudent.GetNext(pos);
				stufile.Write(&studentdata,sizeof(studentdata));
			}
			stufile.Close();
			}
			listsearchresult();
		break;

	}

}

void CMain::Onbtnsearch() 
{
	// TODO: Add your control notification handler code here
	CSearch search;
    num=0;//找到的记录数
	if(search.DoModal()==IDOK)
	{
		int searchresult=0;//查找结果
		wchar_t *cmp;
		CStudentData studentdata;
		POSITION pos;
		for(int i=0;i<m_lststudent.GetCount();i++)
		{
			cmp=NULL;
			pos=m_lststudent.FindIndex(i);
			ZeroMemory(&studentdata,sizeof(studentdata));
			studentdata=m_lststudent.GetAt(pos);
			switch(item)
			{
			case 1://按学生编号查找
	//			 cmp=wcsstr(studentdata.xsbh,key);
				 if(studentdata.xsbh==key)
					 cmp=L"1";//不为NULL
				break;
			
			case 3://按姓名查找
				//cmp=wcsstr(studentdata.xsxm,key);
				if(studentdata.xsxm==key)
					cmp=L"1";//不为NULL
				break;
			
			case 5://按班级名称查找
				//cmp=wcsstr(studentdata.bjmc,key);
				if(studentdata.bjmc==key)
					cmp=L"1";//不为NULL
				break;	
			default:
				return;
				break;
			}
			if(cmp!=NULL)//找到记录
			{
				findpos[num]=i;
				searchresult=1;
				num++;
			}
		}
		if(searchresult==0)
		{
			m_stulist.DeleteAllItems();
			AfxMessageBox(L"没有符合条件的记录!");
		}
		else
			listsearchresult();
			m_count.ShowWindow(SW_SHOW);
			CString str;
			str.Format(L"共找到%d条记录.",num);
			m_count.SetWindowText(str);
	
	}

}
//查找的结果显示
void CMain::listsearchresult()
{
	CStudentData  studentdata;
	POSITION pos;
	m_stulist.DeleteAllItems();

	for(int i=0;i<num;i++)
		{
			ZeroMemory(&studentdata,sizeof(studentdata));
			pos=m_lststudent.FindIndex(findpos[i]);	
			studentdata=m_lststudent.GetAt(pos);
			
			m_stulist.InsertItem(i,studentdata.xsbh);
			m_stulist.SetItemText(i,1,studentdata.xsxm);
			m_stulist.SetItemText(i,2,studentdata.bjmc);
		
		}
}
//显示全部记录
void CMain::Ondisplay() 
{
	// TODO: Add your control notification handler code here
	m_count.ShowWindow(SW_HIDE);
	CStudentData  studentdata;
	POSITION pos;
	if(m_stulist.DeleteAllItems())
		for(int i=0;i<m_lststudent.GetCount();i++)
		{
			ZeroMemory(&studentdata,sizeof(studentdata));
			pos=m_lststudent.FindIndex(i);	
			studentdata=m_lststudent.GetAt(pos);
			
			m_stulist.InsertItem(i,studentdata.xsbh);
			m_stulist.SetItemText(i,1,studentdata.xsxm);
			m_stulist.SetItemText(i,2,studentdata.bjmc);
		}
}

⌨️ 快捷键说明

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