read&write - yamaumima/hello.github.io GitHub Wiki

extern CQQApp theApp;

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CLoginDlg dialog


CLoginDlg::CLoginDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CLoginDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CLoginDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CLoginDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLoginDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CLoginDlg, CDialog)
	//{{AFX_MSG_MAP(CLoginDlg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLoginDlg message handlers

void CLoginDlg::OnOK() 
{
	if(CheckUser())
		CDialog::OnOK();
	else
	{
		AfxMessageBox("用户名或密码错误");
		SetDlgItemText(IDC_NAME,"");
		SetDlgItemText(IDC_PASS,"");
		this ->SetFocus();
	}
}

void CLoginDlg::OnCancel() 
{
	
	CDialog::OnCancel();
}
//file.m_hFile 
//(0xffffffff) -1,4G-1 未打开文件的状态
//其他数值,一般表示已经打开了文件的状态
//读取失败大部分原因是因为文件不存在或者程序员代入的文件名是错误的。
//保存失败大部分原因是因为文件是只读的或者磁盘安全设置禁止写入。

BOOL CLoginDlg::CheckUser()
{
	CString szName,szPass;
	GetDlgItemText(IDC_NAME,szName);
	GetDlgItemText(IDC_PASS,szPass);
	szName.MakeLower();
	CFile file;
	if(!file.Open("./user.dat",CFile::modeRead))
	{
		CreateUser();
		if(!file.Open("./user.dat",CFile::modeRead))
			return FALSE;
	}
	SUser u;
	while(file.Read(&u,sizeof(u)))
	{
		if(szName == u.sName && szPass == u.sPass)
		{
			theApp.m_us = u;
			return TRUE;
		}
	}
	return FALSE;
}

void CInfoDlg::OnOpen() 
{
	LPCSTR szFilter = "员工信息 (*.ifo)|*.ifo|所有文件 (*.*)|*.*||";

	CFileDialog dlg(TRUE,"ifo","C:\\*.txt", OFN_HIDEREADONLY,szFilter);
	if(IDCANCEL == dlg.DoModal())
		return;
	CString szFile = dlg.GetPathName();
	CFile file;
	if(!file.Open(szFile,CFile::modeRead|CFile::shareDenyNone))
	{
		AfxMessageBox("打开文件时失败!");
		return;
	}	
	m_list.DeleteAllItems();
	SInfo info;
	int i = 0;
	CString str;
	while(file.Read(&info,sizeof(info)) == sizeof(info))
	{
		str.Format("%d",info.nNumb);
		m_list.InsertItem(i,str);
		m_list.SetItemText(i,1,info.sName);
		m_list.SetItemText(i,2,info.sBirth);
		m_list.SetItemText(i,3,info.sDept);
		str.Format("%0.1f",info.fSala);
		m_list.SetItemText(i,4,str);

		++i;
	}
}

void CInfoDlg::OnSave() 
{
	LPCSTR szFilter = "员工信息 (*.ifo)|*.ifo|所有文件 (*.*)|*.*||";

	CFileDialog dlg(FALSE,"ifo","",  OFN_OVERWRITEPROMPT,szFilter);
	if(IDCANCEL == dlg.DoModal())
		return;
	CString szFile = dlg.GetPathName();
	CFile file;
	if(!file.Open(szFile,CFile::modeCreate|CFile::modeWrite))
	{
		AfxMessageBox("保存文件时失败!");
		return;
	}
	int i = 0,nCount = m_list.GetItemCount();
	SInfo info;
	while(i<nCount)
	{
		info.nNumb = atoi(m_list.GetItemText(i,0));
		m_list.GetItemText(i,1,info.sName,sizeof(info.sDept));
		m_list.GetItemText(i,2,info.sBirth,sizeof(info.sBirth));
		m_list.GetItemText(i,3,info.sDept,sizeof(info.sDept));
		info.fSala = (float)atof(m_list.GetItemText(i,4));
		file.Write(&info,sizeof(info));
		++i;
	}
}
BOOL CQQDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	CString str = "控制面板 - ";
	str += theApp.m_us.sName;
	SetWindowText(str); 
	GetDlgItem(IDC_ADMIN) ->EnableWindow(theApp.m_us.nPrior);
	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CQQDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CQQDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CQQDlg::OnCancel() 
{
	
	EndDialog(IDCANCEL);
}

void CQQDlg::OnOK() 
{
	//CDialog::OnOK();
}

void CQQDlg::OnExit() 
{
	EndDialog(IDCANCEL);
}

void CQQDlg::OnRelogin() 
{
	ShowWindow(SW_HIDE);
	CLoginDlg dlg;
	if(IDCANCEL== dlg.DoModal())
		OnCancel();
	else
	{
		CString str = "控制面板 - ";
		str += theApp.m_us.sName;
		SetWindowText(str); 
		GetDlgItem(IDC_ADMIN) ->EnableWindow(theApp.m_us.nPrior);

		ShowWindow(SW_SHOW);
	}

}

void CQQDlg::OnAdmin() 
{
	ShowWindow(SW_HIDE);
	CPriorDlg dlg;
	dlg.DoModal();
	ShowWindow(SW_SHOW);
}

void CQQDlg::OnInfo() 
{
	ShowWindow(SW_HIDE);
	CInfoDlg dlg;
	dlg.DoModal();
	ShowWindow(SW_SHOW);
		
}
//凡是在一个函数中,出现了大量同一个类对象的调用.转移到该对象的类内
void CQQDlg::OnBackcol() 
{
	theApp.OnBackCol();
	Invalidate();
}

void CQQDlg::OnTextcol() 
{
	theApp.OnTextCol();
	Invalidate();
	
}
 PriorDlg.cpp : implementation file
//

#include "stdafx.h"
#include "QQ.h"
#include "PriorDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPriorDlg dialog


CPriorDlg::CPriorDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPriorDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPriorDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CPriorDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPriorDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPriorDlg, CDialog)
	//{{AFX_MSG_MAP(CPriorDlg)
	ON_BN_CLICKED(IDC_ADD, OnAdd)
	ON_BN_CLICKED(IDC_DEL, OnDel)
	ON_BN_CLICKED(IDC_MODIFY, OnModify)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPriorDlg message handlers

void CPriorDlg::OnOK() 
{
	
//	CDialog::OnOK();
}
/*

	SUser u;
	GetDlgItemText(IDC_NAME,u.sName,sizeof(u.sName));
	strlwr(u.sName);
	CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_LIST);
	int i = 0,nCount = pList ->GetItemCount();
	while(i<nCount)
	{
		if(pList ->GetItemText(i,0) == u.sName)
		{
			AfxMessageBox("账户已经存在!");
			return ;
		}
		++i;
	}
	pList ->InsertItem(nCount,u.sName);
	GetDlgItemText(IDC_PASS,u.sPass,sizeof(u.sPass));
	pList ->SetItemText(nCount,1,u.sPass);
	CComboBox* pComb = (CComboBox*)GetDlgItem(IDC_PRIOR);
	int nSel = pComb ->GetCurSel();
	pList ->SetItemText(nCount,2,nSel?"高级":"普通");


*/
void CPriorDlg::OnAdd() 
{
	CString szName;
	GetDlgItemText(IDC_NAME,szName);
//	strlwr(u.sName);
	szName.MakeLower();
	CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_LIST);
	int i = 0,nCount = pList ->GetItemCount();
	while(i<nCount)
	{
		if(pList ->GetItemText(i,0) == szName)
		{
			AfxMessageBox("账户已经存在!");
			return ;
		}
		++i;
	}
	pList ->InsertItem(nCount,szName);
	CString szPass;
	GetDlgItemText(IDC_PASS,szPass);
	pList ->SetItemText(nCount,1,szPass);
	CComboBox* pComb = (CComboBox*)GetDlgItem(IDC_PRIOR);
	int nSel = pComb ->GetCurSel();
	pList ->SetItemText(nCount,2,nSel?"高级":"普通");

}
//GetSelectionMark以选中项的虚线焦点选中标志
//如果和GetSelectedCount联合使用效果会好些。

//GetFirstSelectedItemPosition和GetNextSelectedItem
//获取的是选中的蓝色的多个列表项,GetSelectedCount也属于这一组
void CPriorDlg::OnDel() 
{
	CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_LIST);

	//int nSel = pList ->GetSelectionMark();

	POSITION pos = pList ->GetFirstSelectedItemPosition( );
	int nSel = pList ->GetNextSelectedItem(pos);
//	if(pList->GetSelectedCount()<1)
	if(nSel < 0)
	{
		AfxMessageBox("请选中一个账户再删除");
		return;
	}

	if(pList ->GetItemText(nSel,0) == "admin")
		return;
	CString str;
	str.Format("确认要删除 %s 吗?",pList ->GetItemText(nSel,0));
	if(IDYES == AfxMessageBox(str,MB_YESNO))
		pList ->DeleteItem(nSel);




}
//帐户名不修改,修改其他的数据
void CPriorDlg::OnModify() 
{
	CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_LIST);
	POSITION pos = pList ->GetFirstSelectedItemPosition( );
	int nSel = pList ->GetNextSelectedItem(pos);
	if(nSel < 0)
	{
		AfxMessageBox("请选中一个账户再修改");
		return;
	}
	char sPass[20];
	GetDlgItemText(IDC_PASS,sPass,sizeof(sPass));
	pList ->SetItemText(nSel,1,sPass);
	CComboBox* pComb = (CComboBox*)GetDlgItem(IDC_PRIOR);
	CString str;
	pComb ->GetWindowText(str);
	if(pList ->GetItemText(nSel,0)!="admin")
		pList ->SetItemText(nSel,2,str);



}
void CPriorDlg::ReadUser(CListCtrl *pList)
{
	CFile file;
	if(!file.Open("./user.dat",CFile::modeRead|CFile::shareDenyNone))
		return;
	pList ->DeleteAllItems();
	
	SUser u;
	int i = 0;
	while(file.Read(&u,sizeof(u)) == sizeof(u))  //>0 ) 
	{
		pList ->InsertItem(i,u.sName);
		pList ->SetItemText(i,1,u.sPass);
		pList ->SetItemText(i,2,u.nPrior?"高级":"普通");
		++i;
	}
	file.Close();
}

BOOL CPriorDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_LIST);
	pList ->InsertColumn(0,"账户",0,100);
	pList ->InsertColumn(1,"密码",0,100);
	pList ->InsertColumn(2,"权限",0,100);
	ReadUser(pList);
	CComboBox* pComb = (CComboBox*)GetDlgItem(IDC_PRIOR);
	pComb ->AddString("普通");
	pComb ->AddString("高级");
	pComb ->SetCurSel(0);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}






void CPriorDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	CFile file;
	if(!file.Open("./user.dat",CFile::modeCreate|CFile::modeWrite))
	{
		AfxMessageBox("保存文件时失败!");
		return;
	}
	CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_LIST);
	int i = 0,nCount = pList ->GetItemCount();
	SUser u;
	while(i<nCount)
	{
		pList ->GetItemText(i,0,u.sName,sizeof(u.sName));
		pList ->GetItemText(i,1,u.sPass,sizeof(u.sPass));
		u.nPrior = pList ->GetItemText(i,2)=="高级";
		file.Write(&u,sizeof(u));
		++i;
	}

	file.Close();
	
}