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

📄 mysystemset.cpp

📁 一个学生考试成绩管理的半成品
💻 CPP
字号:
// MySystemSet.cpp : 实现文件
//

#include "stdafx.h"
#include "ScoreGather.h"
#include "MySystemSet.h"
#include ".\mysystemset.h"


// CMySystemSet 对话框

IMPLEMENT_DYNAMIC(CMySystemSet, CDialog)
CMySystemSet::CMySystemSet(CWnd* pParent /*=NULL*/)
	: CDialog(CMySystemSet::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDI_PASSWORD);
}

CMySystemSet::~CMySystemSet()
{
}

void CMySystemSet::DoDataExchange(CDataExchange* pDX)
{
	if(pDX->m_bSaveAndValidate)// 条件
		if(pDX->m_bSaveAndValidate)
		{
			m_userpassword.g_bAuthorIdentity = TRUE;
			m_userpassword1.g_bAuthorIdentity = TRUE;
		}
		CDialog::DoDataExchange(pDX);
		DDX_Control(pDX, IDC_CHECK1, m_checkadd);
		DDX_Control(pDX, IDC_CHECK2, m_checkmodify);
		DDX_Control(pDX, IDC_CHECK3, m_checkdel);
		DDX_Control(pDX, IDC_USERNAME, m_comboxusername);
		DDX_Control(pDX, IDC_USERPASSWORD, m_userpassword);
		DDX_Control(pDX, IDC_USERPASSWORD1, m_userpassword1);
		DDX_Control(pDX, IDC_ADD, m_button1);
		DDX_Control(pDX, ID_OK, m_ok);
}


BEGIN_MESSAGE_MAP(CMySystemSet, CDialog)
	ON_BN_CLICKED(ID_OK, OnBnClickedOk)
	ON_BN_CLICKED(IDC_CHECK1, OnBnClickedCheckAdd)
	ON_BN_CLICKED(IDC_CHECK2, OnBnClickedCheckModify)
	ON_BN_CLICKED(IDC_CHECK3, OnBnClickedCheckDel)
	ON_BN_CLICKED(IDC_ADD, OnBnClickedDo)
	ON_WM_ERASEBKGND()
	ON_WM_CTLCOLOR()
END_MESSAGE_MAP()


// CMySystemSet 消息处理程序

void CMySystemSet::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	OnOK();
}

BOOL CMySystemSet::OnInitDialog()
{
	CDialog::OnInitDialog();
	m_userpassword.SubclassDlgItem(IDC_USERPASSWORD, this);
	m_userpassword1.SubclassDlgItem(IDC_USERPASSWORD1, this);
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标
	m_tooltip.Create(this,TTS_ALWAYSTIP);
	m_tooltip.Activate(TRUE);
	m_tooltip.AddTool(GetDlgItem(IDC_USERNAME),IDS_USERNAME_SYSTEMSET);
	m_tooltip.AddTool(GetDlgItem(IDC_ADDUSER),IDS_ADDUSER);
	m_tooltip.AddTool(GetDlgItem(IDC_ADD),IDS_ADD_SYSTEMSET);
	m_tooltip.AddTool(GetDlgItem(IDOK),IDS_OK_SYSTEMSET);
	m_tooltip.AddTool(GetDlgItem(IDC_CHECK1),IDS_CHECK1_SYSTEMSET);
	m_tooltip.AddTool(GetDlgItem(IDC_CHECK2),IDS_CHECK2_SYSTEMSET);
	m_tooltip.AddTool(GetDlgItem(IDC_CHECK3),IDS_CHECK3_SYSTEMSET);
	m_comboxusername.EnableWindow(0);
	m_userpassword.EnableWindow(0);
	m_userpassword1.EnableWindow(0);
	m_button1.EnableWindow(0);
	StartAdd();
	return TRUE;
}

void CMySystemSet::OnBnClickedCheckAdd()
{
	m_checkmodify.SetCheck(0);
	m_checkdel.SetCheck(0);
	GetDlgItem(IDC_USERNAME)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_ADDUSER)->ShowWindow(SW_SHOW);
	SetDlgItemText(IDC_ADDUSER,"");
	SetDlgItemText(IDC_USERPASSWORD,"");
	SetDlgItemText(IDC_USERPASSWORD1,"");
	GetDlgItem(IDC_ADDUSER)->EnableWindow(1);
	m_userpassword.EnableWindow(1);
	m_userpassword1.EnableWindow(1);
	m_tooltip.AddTool(GetDlgItem(IDC_USERPASSWORD),IDS_USERPASSWORD);
	m_tooltip.AddTool(GetDlgItem(IDC_USERPASSWORD1),IDS_USERPASSWORD1);
	SetDlgItemText(IDC_STATICPASSWORD,"密    码");
	SetDlgItemText(IDC_STATICPASSWORD1,"确    认:");
	SetDlgItemText(IDC_ADD,"增      加");
	m_button1.EnableWindow(1);
}

void CMySystemSet::OnBnClickedCheckModify()
{
	m_checkadd.SetCheck(0);
	m_checkdel.SetCheck(0);
	GetDlgItem(IDC_USERNAME)->ShowWindow(SW_SHOW);
	GetDlgItem(IDC_ADDUSER)->ShowWindow(SW_HIDE);
	SetDlgItemText(IDC_USERNAME,"");
	SetDlgItemText(IDC_USERPASSWORD,"");
	SetDlgItemText(IDC_USERPASSWORD1,"");
	m_comboxusername.EnableWindow(1);
	m_userpassword.EnableWindow(1);
	m_userpassword1.EnableWindow(1);
	m_tooltip.AddTool(GetDlgItem(IDC_USERPASSWORD),IDS_USERPASSWORD_A);
	m_tooltip.AddTool(GetDlgItem(IDC_USERPASSWORD1),IDS_USERPASSWORD1_A);
	SetDlgItemText(IDC_STATICPASSWORD,"旧密码:");
	SetDlgItemText(IDC_STATICPASSWORD1,"新密码:");
	SetDlgItemText(IDC_ADD,"修      改");
	StartAdd();
	m_button1.EnableWindow(1);
}

void CMySystemSet::OnBnClickedCheckDel()
{
	m_checkadd.SetCheck(0);
	m_checkmodify.SetCheck(0);
	GetDlgItem(IDC_USERNAME)->ShowWindow(SW_SHOW);
	GetDlgItem(IDC_ADDUSER)->ShowWindow(SW_HIDE);
	SetDlgItemText(IDC_USERNAME,"");
	SetDlgItemText(IDC_USERPASSWORD,"");
	SetDlgItemText(IDC_USERPASSWORD1,"");
	m_comboxusername.EnableWindow(1);
	m_userpassword.EnableWindow(1);
	m_userpassword1.EnableWindow(1);
	m_tooltip.AddTool(GetDlgItem(IDC_USERPASSWORD),IDS_USERPASSWORD);
	m_tooltip.AddTool(GetDlgItem(IDC_USERPASSWORD1),IDS_USERPASSWORD1);
	SetDlgItemText(IDC_STATICPASSWORD,"密    码");
	SetDlgItemText(IDC_STATICPASSWORD1,"确    认:");
	SetDlgItemText(IDC_ADD,"删      除");
	StartAdd();
	m_button1.EnableWindow(1);
}

void CMySystemSet::OnBnClickedDo()
{
	_RecordsetPtr m_pRecordset;
	HRESULT Select_hTRes;
	CString strSQL;
	CString ctrl,user,password,password1;
	ctrl=user=password=password1=_T("");
	GetDlgItemText(IDC_ADD,ctrl);
	if(ctrl=="增      加")
	{
		GetDlgItemText(IDC_ADDUSER,user);
		user.MakeLower();
		if(user.IsEmpty())
		{
			AfxMessageBox("用户名不能为空!");
			GotoDlgCtrl(GetDlgItem(IDC_ADDUSER));
			return;
		}
		GetDlgItemText(IDC_USERPASSWORD,password);
		GetDlgItemText(IDC_USERPASSWORD1,password1);
		AfxMessageBox(password+"\r\n"+password1);
		if(password!=password1)
		{
			AfxMessageBox("两次密码不一致!");
			GotoDlgCtrl(GetDlgItem(IDC_USERPASSWORD));
			return;
		}
		user=p_MySetClass->addordelpassword(user,TRUE);
		password=p_MySetClass->addordelpassword(password,TRUE);
		password1=p_MySetClass->addordelpassword(password1,TRUE);
		strSQL="select * from users ";
		strSQL=strSQL+" where UserID='";
		strSQL=strSQL+user+"'";
		strSQL=strSQL+" AND UserPassword='";
		strSQL=strSQL+password+"'";
		Select_hTRes = m_pRecordset.CreateInstance(_T("ADODB.Recordset"));
		if (SUCCEEDED(Select_hTRes))
		{
			Select_hTRes = m_pRecordset->Open((LPTSTR)strSQL.GetBuffer(strSQL.GetLength()),
				((CScoreGatherApp*)AfxGetApp())->m_pConn.GetInterfacePtr(),
				adOpenDynamic,adLockPessimistic,adCmdText); //执行查询,打开记录集
			if(SUCCEEDED(Select_hTRes))
			{
				if (!(m_pRecordset->adoEOF))
				{
					AfxMessageBox("此用户名已被占用!");
					GotoDlgCtrl(GetDlgItem(IDC_ADDUSER));
					return;
				}
				else
				{
					m_pRecordset->AddNew();
					m_pRecordset->PutCollect("UserID",_variant_t(user));
					m_pRecordset->PutCollect("UserPassword",_variant_t(password));
					////从文件加载BMP数据
					CString PathName,PathNameTmp;
					TCHAR exeFullPath[MAX_PATH];
					GetModuleFileName(NULL,exeFullPath,MAX_PATH);
					PathNameTmp.Format("%s",exeFullPath);
					PathName=PathNameTmp.Left(PathNameTmp.ReverseFind('\\')+1)+_T("ScoreGather.bmp");
					if(LoadBMPFile(PathName))
					{
						m_hPhotoBitmap = BufferToHBITMAP();
						char *pBuf = m_pBMPBuffer;
						VARIANT	varBLOB;
						SAFEARRAY *psa;
						SAFEARRAYBOUND rgsabound[1];
						if(pBuf)
						{    
							rgsabound[0].lLbound = 0;
							rgsabound[0].cElements = m_nFileLen;
							psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
							for (long i = 0; i < (long)m_nFileLen; i++)
								SafeArrayPutElement (psa, &i, pBuf++);
							varBLOB.vt = VT_ARRAY | VT_UI1;
							varBLOB.parray = psa;
							m_pRecordset->GetFields()->GetItem("bmp")->AppendChunk(varBLOB);
						}
					}
					m_pRecordset->Update();
				}
			}
		}
	}
	if(ctrl=="修      改" || ctrl=="删      除")
	{
		if(m_comboxusername.GetCurSel()>=0)
		{
			m_comboxusername.GetLBText(m_comboxusername.GetCurSel(),user);
			user.MakeLower();
			user=p_MySetClass->addordelpassword(user,TRUE);
		}
		else
		{
			AfxMessageBox("请选择用户名!");
			GotoDlgCtrl(GetDlgItem(IDC_USERNAME));
			return;
		}
		GetDlgItemText(IDC_USERPASSWORD,password);
		GetDlgItemText(IDC_USERPASSWORD1,password1);
		password=p_MySetClass->addordelpassword(password,TRUE);
		password1=p_MySetClass->addordelpassword(password1,TRUE);
		if(ctrl=="修      改")
		{
			strSQL="select * from users ";
			strSQL=strSQL+" where UserID='";
			strSQL=strSQL+user+"'";
			Select_hTRes = m_pRecordset.CreateInstance(_T("ADODB.Recordset"));
			if (SUCCEEDED(Select_hTRes))
			{
				Select_hTRes = m_pRecordset->Open((LPTSTR)strSQL.GetBuffer(strSQL.GetLength()),
					((CScoreGatherApp*)AfxGetApp())->m_pConn.GetInterfacePtr(),
					adOpenDynamic,adLockPessimistic,adCmdText); //执行查询,打开记录集
				if(SUCCEEDED(Select_hTRes))
				{
					if(password!=p_MySetClass->GetString(m_pRecordset->GetCollect("UserPassword")))
					{
						AfxMessageBox("试图非法修改,\n\n请验证密码!!");
						GotoDlgCtrl(GetDlgItem(IDC_USERPASSWORD));
						return;
					}
				}
			}
			strSQL="update users set ";
			strSQL=strSQL+"UserPassword='"+password1+"'";
			strSQL=strSQL+" where UserID='";
			strSQL=strSQL+user+"'";
			strSQL=strSQL+" AND UserPassword='";
			strSQL=strSQL+password+"'";
			(((CScoreGatherApp*)AfxGetApp())->m_pConn)->Execute((_bstr_t)strSQL,NULL,adCmdText);
			user=p_MySetClass->addordelpassword(user,FALSE);
			password1=p_MySetClass->addordelpassword(password1,FALSE);
			AfxMessageBox("修改成功,请牢记:\n\n用户名-> "+user+"\n\n密码-> "+password1);
		}
		else
		{
			if(password!=password1)
			{
				AfxMessageBox("两次密码不一致!");
				GotoDlgCtrl(GetDlgItem(IDC_USERPASSWORD));
				return;
			}
			strSQL="select * from users ";
			strSQL=strSQL+" where UserID='";
			strSQL=strSQL+user+"'";
			Select_hTRes = m_pRecordset.CreateInstance(_T("ADODB.Recordset"));
			if (SUCCEEDED(Select_hTRes))
			{
				Select_hTRes = m_pRecordset->Open((LPTSTR)strSQL.GetBuffer(strSQL.GetLength()),
					((CScoreGatherApp*)AfxGetApp())->m_pConn.GetInterfacePtr(),
					adOpenDynamic,adLockPessimistic,adCmdText); //执行查询,打开记录集
				if(SUCCEEDED(Select_hTRes))
				{
					if(password!=p_MySetClass->GetString(m_pRecordset->GetCollect("UserPassword")))
					{
						AfxMessageBox("试图非法删除,\n\n请验证密码!");
						GotoDlgCtrl(GetDlgItem(IDC_USERPASSWORD));
						return;
					}
				}
			}
			strSQL="delete from users";
			strSQL=strSQL+" where UserID='";
			strSQL=strSQL+user+"'";
			strSQL=strSQL+" AND UserPassword='";
			strSQL=strSQL+password+"'";
			(((CScoreGatherApp*)AfxGetApp())->m_pConn)->Execute((_bstr_t)strSQL,NULL,adCmdText);
			m_comboxusername.DeleteString(m_comboxusername.GetCurSel());
			user=p_MySetClass->addordelpassword(user,FALSE);
			AfxMessageBox("用户-> "+user+" 已被删除!");
		}
	}
	m_checkadd.SetCheck(0);
	m_checkmodify.SetCheck(0);
	m_checkdel.SetCheck(0);
	SetDlgItemText(IDC_USERNAME,"");
	SetDlgItemText(IDC_ADDUSER,"");
	SetDlgItemText(IDC_USERPASSWORD,"");
	SetDlgItemText(IDC_USERPASSWORD1,"");
	GetDlgItem(IDC_ADDUSER)->EnableWindow(0);
	m_comboxusername.SetCurSel(-1);
	m_comboxusername.EnableWindow(0);
	m_userpassword.EnableWindow(0);
	m_userpassword1.EnableWindow(0);
	m_button1.EnableWindow(0);
}

void CMySystemSet::StartAdd(void)
{
	m_comboxusername.SetCurSel(-1);
	int i=m_comboxusername.GetCount();
	for(int n=0;n<=i;n++)
		m_comboxusername.DeleteString(n);
	_RecordsetPtr m_pRecordset;
	CString strSQL;
	strSQL="select UserID from users";
	HRESULT hTRes;
	hTRes = m_pRecordset.CreateInstance(_T("ADODB.Recordset"));
	if (SUCCEEDED(hTRes))
	{
		hTRes = m_pRecordset->Open((LPTSTR)strSQL.GetBuffer(130),
			((CScoreGatherApp*)AfxGetApp())->m_pConn.GetInterfacePtr(),
			adOpenDynamic,adLockPessimistic,adCmdText);
		while(!(m_pRecordset->adoEOF))
		{
			if(m_comboxusername.FindString(0,p_MySetClass->addordelpassword(p_MySetClass->GetString(m_pRecordset->GetCollect("UserID")),FALSE))==-1)
				m_comboxusername.AddString(p_MySetClass->addordelpassword(p_MySetClass->GetString(m_pRecordset->GetCollect("UserID")),FALSE));
			if(!(m_pRecordset->adoEOF))
				m_pRecordset->MoveNext();
		}
		m_pRecordset->Close();
		m_pRecordset=NULL;
	}
}

BOOL CMySystemSet::PreTranslateMessage(MSG* pMsg)
{
	m_tooltip.RelayEvent(pMsg);
	return CDialog::PreTranslateMessage(pMsg);
}

BOOL CMySystemSet::OnEraseBkgnd(CDC* pDC)
{
	CBrush brush(RGB(250,220,250));
	CBrush* pOldBrush=pDC->SelectObject(&brush);
	CRect rcClip;
	pDC->GetClipBox(&rcClip);
	pDC->PatBlt(rcClip.left,rcClip.top,rcClip.Width(),rcClip.Height(),PATCOPY);
	pDC->SelectObject(pOldBrush);
	return TRUE;
}

HBRUSH CMySystemSet::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	if(nCtlColor!=CTLCOLOR_EDIT)
	{
		pDC->SetTextColor(RGB(40,20,255));
		pDC->SetBkMode(TRANSPARENT);
		HBRUSH B = CreateSolidBrush(RGB(250,220,250));
		return (HBRUSH) B;
	}
	else
		return CDialog::OnCtlColor(pDC, pWnd, nCtlColor); 
}

BOOL CMySystemSet::LoadBMPFile(const char * pBMPPathname)
{
	CFile file;
	if( !file.Open( pBMPPathname, CFile::modeRead) )
		return FALSE;
	m_nFileLen = (DWORD)file.GetLength();
	m_pBMPBuffer = new char[m_nFileLen + 1];
	if(!m_pBMPBuffer)
		return FALSE;
	if(file.Read(m_pBMPBuffer,m_nFileLen) != m_nFileLen)
		return FALSE;
	return TRUE;
}

HBITMAP CMySystemSet::BufferToHBITMAP(void)
{
	HBITMAP				hBmp;
	LPSTR				hDIB,lpBuffer = m_pBMPBuffer;
	LPVOID				lpDIBBits;
	BITMAPFILEHEADER	bmfHeader;
	DWORD				bmfHeaderLen;

	bmfHeaderLen = sizeof(bmfHeader);
	strncpy((LPSTR)&bmfHeader,(LPSTR)lpBuffer,bmfHeaderLen);
	if (bmfHeader.bfType != (*(WORD*)"BM")) return NULL; 
	hDIB = lpBuffer + bmfHeaderLen;
	BITMAPINFOHEADER &bmiHeader = *(LPBITMAPINFOHEADER)hDIB ;
	BITMAPINFO &bmInfo = *(LPBITMAPINFO)hDIB ;
	lpDIBBits=(lpBuffer)+((BITMAPFILEHEADER *)lpBuffer)->bfOffBits;//这行功能和上面被注释掉的代码相同,容易理解。
	CClientDC dc(this);
	hBmp = CreateDIBitmap(dc.m_hDC,&bmiHeader,CBM_INIT,lpDIBBits,&bmInfo,DIB_RGB_COLORS);
	return hBmp;
}

⌨️ 快捷键说明

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