UserManagement.cpp
上传用户:lvjun8202
上传日期:2013-04-30
资源大小:797k
文件大小:6k
- // UserManagement.cpp : implementation file
- //
- #include "stdafx.h"
- #include "oam.h"
- #include "UserManagement.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CUserManagement dialog
- extern COAMApp theApp;
- CUserManagement::CUserManagement(CWnd* pParent /*=NULL*/)
- : CDialog(CUserManagement::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CUserManagement)
- m_password = _T("");
- m_passwordc = _T("");
- m_power = _T("");
- m_username = _T("");
- //}}AFX_DATA_INIT
- }
- void CUserManagement::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CUserManagement)
- DDX_Control(pDX, IDC_USERLIST, m_userlist);
- DDX_Control(pDX, IDC_DELITEM, m_cDelItem);
- DDX_Control(pDX, IDC_ADDITEM, m_cAddItem);
- DDX_Text(pDX, IDC_PASSWORD, m_password);
- DDX_Text(pDX, IDC_PASSWORDC, m_passwordc);
- DDX_Text(pDX, IDC_POWER, m_power);
- DDX_Text(pDX, IDC_USERNAME, m_username);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CUserManagement, CDialog)
- //{{AFX_MSG_MAP(CUserManagement)
- ON_BN_CLICKED(IDC_ADDITEM, OnAdditem)
- ON_BN_CLICKED(IDC_DELITEM, OnDelitem)
- ON_NOTIFY(LVN_ITEMCHANGED, IDC_USERLIST, OnItemchangedUserlist)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CUserManagement message handlers
- BOOL CUserManagement::OnInitDialog()
- {
- CDialog::OnInitDialog();
-
- m_cDelItem.EnableWindow(FALSE);
- ::SendMessage(m_userlist.m_hWnd, LVM_SETEXTENDEDLISTVIEWSTYLE,LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);
- //////////为列表控件添加列//////////
- m_userlist.InsertColumn(1,"用户ID",LVCFMT_LEFT,60);
- m_userlist.InsertColumn(1,"用户名",LVCFMT_LEFT,100);
- m_userlist.InsertColumn(2,"用户权限",LVCFMT_LEFT,60);
- //////////读取数据库中的信息添加到列表控件///////////
- int nItem;
- _variant_t vUsername,vID,vPower;
- try
- {
- m_pRecordset.CreateInstance("ADODB.Recordset");
- m_pRecordset->Open("SELECT * FROM users",_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
- m_bSuccess = TRUE;
- while(!m_pRecordset->adoEOF)
- {
-
- vID = m_pRecordset->GetCollect("ID");
- vUsername = m_pRecordset->GetCollect("username");
- vPower = m_pRecordset->GetCollect("userpower");
-
- nItem=m_userlist.InsertItem(0xffff,(_bstr_t)vID);
- m_userlist.SetItem(nItem,1,1,(_bstr_t)vUsername,NULL,0,0,0);
- m_userlist.SetItem(nItem,2,1,(_bstr_t)vPower,NULL,0,0,0);
-
- m_pRecordset->MoveNext();
- }
- }
- catch(_com_error e)///捕捉异常
- {
- AfxMessageBox("读取数据库失败!");///显示错误信息
- }
- // Add "About..." menu item to system menu.
- // IDM_ABOUTBOX must be in the system command range.
-
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
-
- // TODO: Add extra initialization here
-
-
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- void CUserManagement::LoadData()
- {
- m_pRecordset->Move(m_nCurrentSel,_variant_t((long)adBookmarkFirst));
- vUserID = m_pRecordset->GetCollect("ID");
- vUsername = m_pRecordset->GetCollect("username");
- vPower = m_pRecordset->GetCollect("userpower");
- vPassword=m_pRecordset->GetCollect("password");
- // m_nUserID = vUserID.lVal;
- m_username = (LPCTSTR)(_bstr_t)vUsername;
- m_power = (LPCTSTR)(_bstr_t)vPower;
- m_password = (LPCTSTR)(_bstr_t)vPassword;
- m_passwordc = (LPCTSTR)(_bstr_t)vPassword;
- UpdateData(FALSE);
- }
- /////将编辑框的数据保存到记录集与列表框
- void CUserManagement::SaveData()
- {
- if(!m_pRecordset->adoEOF && m_nCurrentSel >= 0 && m_bAutoSave)
- {
- // vUserID = (long)m_nUserID;
- vUsername = m_username;
- vPower = m_power;
- vPassword = m_password;
- //m_pRecordset->PutCollect("ID",vUserID);
- m_pRecordset->PutCollect("username",vUsername);
- m_pRecordset->PutCollect("userpower",vPower);
- m_pRecordset->PutCollect("password",vPassword);
- // m_userlist.SetItem(m_nCurrentSel,0,LVIF_TEXT,(_bstr_t)vUserID,NULL,0,0,0);
- m_userlist.SetItem(m_nCurrentSel,1,LVIF_TEXT,(_bstr_t)vUsername,NULL,0,0,0);
- m_userlist.SetItem(m_nCurrentSel,2,LVIF_TEXT,(_bstr_t)vPower,NULL,0,0,0);
- // m_userlist.SetItem(m_nCurrentSel,3,LVIF_TEXT,(_bstr_t)vBirthday,NULL,0,0,0);
- }
- }
- void CUserManagement::OnAdditem()
- {
- if(UpdateData())
- if(m_username.GetLength()>0)
- {
- m_pRecordset->AddNew();
- m_nCurrentSel = m_userlist.InsertItem(0xffff,"");
- SaveData();///保存数据
- m_userlist.SetItemState(m_nCurrentSel,LVIS_SELECTED|LVIS_FOCUSED,LVIS_SELECTED|LVIS_FOCUSED);
- m_userlist.SetHotItem(m_nCurrentSel);
- m_userlist.SetFocus();
- }
- else
- AfxMessageBox("请输入用户名");
-
- }
- void CUserManagement::OnDelitem()
- {
- // TODO: Add your control notification handler code here
- m_bAutoSave = FALSE;
- if(m_nCurrentSel >= 0)
- {
- m_userlist.DeleteItem(m_nCurrentSel);
- int count = m_userlist.GetItemCount();
- if(count <= m_nCurrentSel)
- m_nCurrentSel = count-1;
- m_pRecordset->Delete(adAffectCurrent);
-
- m_pRecordset->MoveNext();
- if (!m_pRecordset->adoEOF)
- {
- LoadData();
- m_userlist.SetItemState(m_nCurrentSel,LVIS_SELECTED|LVIS_FOCUSED,LVIS_SELECTED|LVIS_FOCUSED);
- m_userlist.SetFocus();
- }
- else
- m_cDelItem.EnableWindow(FALSE);;
- }
- m_bAutoSave = TRUE;
- }
- void CUserManagement::OnItemchangedUserlist(NMHDR* pNMHDR, LRESULT* pResult)
- {
- NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
- if(pNMListView->uNewState&LVIS_SELECTED)
- {
- UpdateData();
- SaveData();///保存旧数据
- m_nCurrentSel = pNMListView->iItem;
- LoadData();///加载新数据
- m_cDelItem.EnableWindow();
- }
- *pResult = 0;
- }
- void CUserManagement::OnOK()
- {
- if(m_bSuccess)
- {
- m_pRecordset->Update();
- m_pRecordset->Close();
- }
- CDialog::OnOK();
- }