📄 usermanage.cpp
字号:
// UserManage.cpp : 实现文件
//
#include "stdafx.h"
#include "lfsw.h"
#include "UserManage.h"
#include ".\usermanage.h"
// CUserManage 对话框
IMPLEMENT_DYNAMIC(CUserManage, CDialog)
CUserManage::CUserManage(CWnd* pParent /*=NULL*/)
: CDialog(CUserManage::IDD, pParent)
{
}
CUserManage::~CUserManage()
{
}
void CUserManage::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST_USER, m_list_user);
}
BEGIN_MESSAGE_MAP(CUserManage, CDialog)
ON_BN_CLICKED(IDC_ADD_USER, OnBnClickedAddUser)
ON_BN_CLICKED(IDC_MODI_USER, OnBnClickedModiUser)
ON_BN_CLICKED(IDC_DEL_USER, OnBnClickedDelUser)
ON_BN_CLICKED(IDCANCEL, OnBnClickedCancel)
ON_BN_CLICKED(IDOK, OnBnClickedOk)
ON_NOTIFY(NM_DBLCLK, IDC_LIST_USER, OnNMDblclkListUser)
END_MESSAGE_MAP()
// CUserManage 消息处理程序
BOOL CUserManage::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
DWORD dwStyle;
dwStyle = m_list_user.GetExtendedStyle();
dwStyle |= LVS_EX_GRIDLINES |LVS_EX_FULLROWSELECT|LVS_SHOWSELALWAYS ;
m_list_user.SetExtendedStyle(dwStyle);
m_list_user.InsertColumn(0,"用户名",LVCFMT_LEFT,75);
m_list_user.InsertColumn(1,"用户真实姓名",LVCFMT_LEFT,120);
if(theApp.realname != "admin")
{
GetDlgItem(IDC_ADD_USER)->EnableWindow(false);
GetDlgItem(IDC_DEL_USER)->EnableWindow(false);
}
if(theApp.realname == "admin")
{
int i,nCount;
CString sql = "select * from Employ";
try
{
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
nCount = m_pRecordset->GetRecordCount();
for(i=0;i<nCount;i++)
{
m_list_user.InsertItem(m_list_user.GetItemCount(),(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("username"));
m_list_user.SetItemText(m_list_user.GetItemCount()-1,1,(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("realname"));
m_pRecordset->MoveNext();
}
}
catch(_com_error e)///捕捉异常
{
CString temp;
temp.Format("读取数据库用户记录错误:%s",e.ErrorMessage());
AfxMessageBox(temp);
return false;
}
}
else
{
GetDlgItem(IDC_ADD_USER)->EnableWindow(false);
GetDlgItem(IDC_DEL_USER)->EnableWindow(false);
GetDlgItem(IDC_USER_NAME)->EnableWindow(false);
m_list_user.InsertItem(m_list_user.GetItemCount(),theApp.username);
m_list_user.SetItemText(m_list_user.GetItemCount()-1,1,theApp.realname);
}
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void CUserManage::OnBnClickedAddUser()
{
// TODO: 在此添加控件通知处理程序代码
CString strName,stPass,strRealname;
GetDlgItem(IDC_USER_NAME)->GetWindowText(strName);
GetDlgItem(IDC_USER_PASS)->GetWindowText(stPass);
GetDlgItem(IDC_REAL_NAME)->GetWindowText(strRealname);
CString sql = "select * from employ where username = '";
sql = sql + strName + "'";
try
{
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
if(m_pRecordset->adoEOF)
{
m_list_user.InsertItem(m_list_user.GetItemCount(),strName);
m_list_user.SetItemText(m_list_user.GetItemCount()-1,1,strRealname);
m_pRecordset->AddNew();
m_pRecordset->PutCollect("username",_variant_t(strName));
m_pRecordset->PutCollect("password",_variant_t(stPass));
m_pRecordset->PutCollect("realname",_variant_t(strRealname));
m_pRecordset->Update();
GetDlgItem(IDC_USER_NAME)->SetWindowText("");
GetDlgItem(IDC_USER_PASS)->SetWindowText("");
GetDlgItem(IDC_REAL_NAME)->SetWindowText("");
}
else
{
MessageBox("用户名重复!","警告...",MB_ICONSTOP);
return;
}
}
catch(_com_error e)///捕捉异常
{
CString temp;
temp.Format("读取数据库用户记录错误:%s",e.ErrorMessage());
AfxMessageBox(temp);
return;
}
}
void CUserManage::OnBnClickedModiUser()
{
// TODO: 在此添加控件通知处理程序代码
int id = m_list_user.GetSelectionMark();
CString strName,stPass,strRealname;
GetDlgItem(IDC_USER_NAME)->GetWindowText(strName);
GetDlgItem(IDC_USER_PASS)->GetWindowText(stPass);
GetDlgItem(IDC_REAL_NAME)->GetWindowText(strRealname);
CString sql = "select * from employ where username = '";
sql = sql + strName + "'";
try
{
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
if(!m_pRecordset->adoEOF)
{
m_list_user.SetItemText(id,1,strRealname);
m_pRecordset->PutCollect("password",_variant_t(stPass));
m_pRecordset->PutCollect("realname",_variant_t(strRealname));
m_pRecordset->Update();
GetDlgItem(IDC_USER_NAME)->SetWindowText("");
GetDlgItem(IDC_USER_PASS)->SetWindowText("");
GetDlgItem(IDC_REAL_NAME)->SetWindowText("");
}
else
{
MessageBox("系统错误!请与管理员联系!","警告...",MB_ICONSTOP);
return;
}
}
catch(_com_error e)///捕捉异常
{
CString temp;
temp.Format("读取数据库用户记录错误:%s",e.ErrorMessage());
AfxMessageBox(temp);
return;
}
}
void CUserManage::OnBnClickedDelUser()
{
// TODO: 在此添加控件通知处理程序代码
if(m_list_user.GetItemText(m_list_user.GetSelectionMark(),0) == "admin")
{
MessageBox("此用户不能删除!","警告...",MB_ICONSTOP);
return;
}
if(MessageBox("你确定要删除此用户吗?\n删除不可恢复!","删除询问...",MB_ICONQUESTION|MB_YESNO)==IDYES)
{
int id = m_list_user.GetSelectionMark();
CString sql = "delete * from employ where username = '";
sql = sql + m_list_user.GetItemText(id,0) + "' and realname ='" + m_list_user.GetItemText(id,1) + "'";
try
{
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
}
catch(_com_error e)///捕捉异常
{
CString temp;
temp.Format("读取数据库用户记录错误:%s",e.ErrorMessage());
AfxMessageBox(temp);
return;
}
m_list_user.DeleteItem(id);
}
}
void CUserManage::OnBnClickedCancel()
{
// TODO: 在此添加控件通知处理程序代码
OnCancel();
}
void CUserManage::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
// OnOK();
}
void CUserManage::OnNMDblclkListUser(NMHDR *pNMHDR, LRESULT *pResult)
{
// TODO: 在此添加控件通知处理程序代码
GetDlgItem(IDC_USER_NAME)->SetWindowText(m_list_user.GetItemText(m_list_user.GetSelectionMark(),0));
GetDlgItem(IDC_REAL_NAME)->SetWindowText(m_list_user.GetItemText(m_list_user.GetSelectionMark(),1));
*pResult = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -