📄 usermanageview.cpp
字号:
// UserManageView.cpp : implementation file
//
#include "stdafx.h"
#include "StationManage.h"
#include "UserManageView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CUserManageView
IMPLEMENT_DYNCREATE(CUserManageView, CRecordView)
CUserManageView::CUserManageView()
: CRecordView(CUserManageView::IDD)
{
//{{AFX_DATA_INIT(CUserManageView)
m_pSet = NULL;
m_username = _T("");
curiItem = 0;
//}}AFX_DATA_INIT
}
CUserManageView::~CUserManageView()
{
if (!(((CStationManageApp*)AfxGetApp())->m_usermanagecontrol))
{
((CStationManageApp*)AfxGetApp())->m_usermanage=0;
}
if (m_pSet)
delete m_pSet;
}
void CUserManageView::DoDataExchange(CDataExchange* pDX)
{
CRecordView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CUserManageView)
DDX_Control(pDX, IDC_USERTYPE_COMBO, m_usertype);
DDX_Control(pDX, IDC_USERID_EDIT, m_useridcontrol);
DDX_Control(pDX, IDC_DELETEUSER, m_deleteusercontorl);
DDX_Control(pDX, IDC_CHANGEUSER, m_changeusercontrol);
DDX_Control(pDX, IDC_CHANGEPW, m_changepwcontrol);
DDX_Control(pDX, IDC_ADDUSER, m_addcontrol);
DDX_Control(pDX, IDC_RETURN, m_returncontrol);
DDX_Control(pDX, IDC_SAVE, m_savecontrol);
DDX_Control(pDX, IDC_USERLIST, m_userlist);
DDX_Text(pDX, IDC_USERID_EDIT, m_username);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CUserManageView, CRecordView)
//{{AFX_MSG_MAP(CUserManageView)
ON_BN_CLICKED(IDC_ADDUSER, OnAdduser)
ON_NOTIFY(NM_CLICK, IDC_USERLIST, OnClickUserlist)
ON_BN_CLICKED(IDC_DELETEUSER, OnDeleteuser)
ON_BN_CLICKED(IDC_CHANGEUSER, OnChangeuser)
ON_BN_CLICKED(IDC_CHANGEPW, OnChangepw)
ON_BN_CLICKED(IDC_SAVE, OnSave)
ON_BN_CLICKED(IDC_RETURN, OnReturn)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CUserManageView diagnostics
#ifdef _DEBUG
void CUserManageView::AssertValid() const
{
CRecordView::AssertValid();
}
void CUserManageView::Dump(CDumpContext& dc) const
{
CRecordView::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CUserManageView message handlers
CRecordset* CUserManageView::OnGetRecordset()
{
if (m_pSet != NULL)
return m_pSet;
m_pSet = new CUserSet(NULL);
m_pSet->Open();
return m_pSet;
}
CUserSet* CUserManageView::GetRecordset()
{
CUserSet* pData = (CUserSet*) OnGetRecordset();
ASSERT(pData == NULL || pData->IsKindOf(RUNTIME_CLASS(CUserSet)));
return pData;
}
void CUserManageView::OnInitialUpdate()
{
BeginWaitCursor();
GetRecordset();
CRecordView::OnInitialUpdate();
GetDocument()->SetTitle("用户管理");
GetParent()->SetWindowText("用户管理");
EndWaitCursor();
RECT rectuserlist;
m_userlist.GetWindowRect(&rectuserlist);
int userlistwidth=rectuserlist.right-rectuserlist.left;
DWORD dwExtendedStyle=LVS_EX_GRIDLINES | LVS_EX_HEADERDRAGDROP | LVS_EX_ONECLICKACTIVATE;
m_userlist.SetExtendedStyle(dwExtendedStyle|LVS_EX_FULLROWSELECT);
m_userlist.InsertColumn(0,"用户名",LVCFMT_LEFT,userlistwidth/2-10);
m_userlist.InsertColumn(1,"用户类型",LVCFMT_LEFT,userlistwidth/2-10);
Showlist();
curiItem=0;
Getvalue();
m_userlist.SetItemState(curiItem,LVIS_SELECTED,LVIS_SELECTED);
EnableView(true);
UpdateData(FALSE);
}
void CUserManageView::Showlist()
{
if (!m_pSet->IsOpen())
m_pSet->Open();
m_userlist.DeleteAllItems();
m_pSet->MoveFirst();
if (!m_pSet->IsEOF())
{
while(!m_pSet->IsEOF())
m_pSet->MoveNext();
int cout=m_pSet->GetRecordCount();
m_pSet->MoveFirst();
while(!m_pSet->IsEOF())
{
int nCount=m_userlist.GetItemCount();
LV_ITEM lv_item;
lv_item.mask=LVIF_TEXT;
lv_item.iItem=nCount;
lv_item.iSubItem=0;
m_pSet->m_username.TrimRight();
lv_item.pszText=m_pSet->m_username.GetBuffer(m_pSet->m_username.GetLength());
m_userlist.InsertItem(&lv_item);
m_userlist.SetItemText(nCount,0,m_pSet->m_username);
m_pSet->m_usertype.TrimRight();
m_userlist.SetItemText(nCount,1,m_pSet->m_usertype);
m_pSet->MoveNext();
}
}
}
void CUserManageView::Getvalue()
{
m_username = m_userlist.GetItemText(curiItem,0);
if (m_userlist.GetItemText(curiItem,1) == "admin")
{
m_usertype.SelectString(0,"管理员");
}
else
{
m_usertype.SelectString(0,"售票员");
}
}
void CUserManageView::EnableView(bool b)
{
m_useridcontrol.SetReadOnly(b);
m_usertype.EnableWindow(!b);
m_addcontrol.EnableWindow(b);
m_changepwcontrol.EnableWindow(b);
m_deleteusercontorl.EnableWindow(b);
m_changeusercontrol.EnableWindow(b);
m_savecontrol.EnableWindow(!b);
m_returncontrol.EnableWindow(!b);
}
void CUserManageView::OnClickUserlist(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
curiItem = pNMListView->iItem;
if(curiItem != -1)
{
Getvalue();
}
UpdateData(FALSE);
*pResult = 0;
}
void CUserManageView::OnAdduser()
{
// TODO: Add your control notification handler code here
EnableView(false);
curiItem=m_userlist.GetItemCount();
m_username = _T("");
UpdateData(FALSE);
}
void CUserManageView::OnDeleteuser()
{
// TODO: Add your control notification handler code here
if (m_username == ((CStationManageApp*)AfxGetApp())->curusername)
{
MessageBox("您要删除的用户是当前用户,您不能对自己进行删除,如有疑问请与其它管理员联系!","警告",MB_OK|MB_ICONEXCLAMATION);
return;
}
if (curiItem==m_userlist.GetItemCount())
{
MessageBox("请选择您要删除的行!","警告",MB_OK|MB_ICONEXCLAMATION);
return;
}
CString info;
info="确认要删除用户“"+m_username+"”?";
if (IDOK==MessageBox(info,"提示",MB_OKCANCEL|MB_ICONQUESTION))
{
if (!m_pSet->IsOpen())
{
m_pSet->Open();
}
m_pSet->MoveFirst();
int i=curiItem;
while (i>0)
{
m_pSet->MoveNext();
i--;
}
m_pSet->Delete();
m_pSet->Requery();
m_userlist.DeleteItem(curiItem);
info.Empty();
info="用户“"+m_username+"”已经删除!";
curiItem=0;
Getvalue();
MessageBox(info,"删除成功",MB_OK|MB_ICONINFORMATION);
UpdateData(FALSE);
}
}
void CUserManageView::OnChangeuser()
{
// TODO: Add your control notification handler code here
if (curiItem==m_userlist.GetItemCount())
{
MessageBox("请选择您要修改的行!","警告",MB_OK|MB_ICONEXCLAMATION);
return;
}
if (m_username == ((CStationManageApp*)AfxGetApp())->curusername)
{
MessageBox("您要修改的用户是当前用户,您不能对自己进行修改,如有疑问请与其它管理员联系!","警告",MB_OK|MB_ICONEXCLAMATION);
return;
}
if (m_userlist.GetItemText(curiItem,1)=="admin")
{
CString info;
info="用户“"+m_username+"”的类型是管理员,是否要把其类型转换为售票员?";
if (IDOK==MessageBox(info,"提示",MB_OKCANCEL|MB_ICONQUESTION))
{
if (!m_pSet->IsOpen())
{
m_pSet->Open();
}
m_pSet->MoveFirst();
int i=curiItem;
while (i>0)
{
m_pSet->MoveNext();
i--;
}
m_pSet->Edit();
m_pSet->m_usertype = "conductor";
m_pSet->Update();
m_pSet->Requery();
Showlist();
Getvalue();
UpdateData(FALSE);
MessageBox("修改成功!","修改成功",MB_OK|MB_ICONINFORMATION);
}
return;
}
else
{
CString info;
info="用户“"+m_username+"”的类型是售票员,是否要把其类型转换为管理员?";
if (IDOK==MessageBox(info,"提示",MB_OKCANCEL|MB_ICONQUESTION))
{
if (!m_pSet->IsOpen())
{
m_pSet->Open();
}
m_pSet->MoveFirst();
int i=curiItem;
while (i>0)
{
m_pSet->MoveNext();
i--;
}
m_pSet->Edit();
m_pSet->m_usertype = "admin";
m_pSet->Update();
m_pSet->Requery();
Showlist();
Getvalue();
UpdateData(FALSE);
MessageBox("修改成功!","修改成功",MB_OK|MB_ICONINFORMATION);
}
return;
}
}
void CUserManageView::OnChangepw()
{
// TODO: Add your control notification handler code here
if (curiItem==m_userlist.GetItemCount())
{
MessageBox("请选择您要重置的行!","警告",MB_OK|MB_ICONEXCLAMATION);
return;
}
UpdateData();
if (m_username == ((CStationManageApp*)AfxGetApp())->curusername)
{
MessageBox("您要重置的用户是当前用户,您不能对自己进行密码重置,如果您忘记密码请与其它管理员联系!","警告",MB_OK|MB_ICONEXCLAMATION);
return;
}
CString info;
info="确认重置要用户“"+m_username+"”的密码?";
if (IDOK==MessageBox(info,"提示",MB_OKCANCEL|MB_ICONQUESTION))
{
if (!m_pSet->IsOpen())
{
m_pSet->Open();
}
m_pSet->MoveFirst();
int i=curiItem;
while (i>0)
{
m_pSet->MoveNext();
i--;
}
m_pSet->Edit();
m_pSet->m_password = "8888";
m_pSet->Update();
m_pSet->Requery();
MessageBox("密码已经重置!","重置成功",MB_OK|MB_ICONINFORMATION);
}
}
void CUserManageView::OnSave()
{
// TODO: Add your control notification handler code here
UpdateData();
if (!m_pSet->IsOpen())
{
m_pSet->Open();
}
m_pSet->AddNew(); //在表的末尾添加新记录
m_pSet->m_username = m_username;
m_pSet->m_password = "8888";
CString str;
m_usertype.GetWindowText(str);
if (str == "管理员")
{
str.Empty();
str="admin";
}
else
{
str.Empty();
str="conductor";
}
m_pSet->m_usertype = str;
m_pSet->Update();
m_pSet->Requery();
Showlist();
MessageBox("增加成功!","增加成功",MB_OK|MB_ICONINFORMATION);
EnableView(true);
return;
}
void CUserManageView::OnReturn()
{
// TODO: Add your control notification handler code here
EnableView(true);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -