UserManagement.cpp
上传用户:lvjun8202
上传日期:2013-04-30
资源大小:797k
文件大小:6k
源码类别:

SNMP编程

开发平台:

C/C++

  1. // UserManagement.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "oam.h"
  5. #include "UserManagement.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CUserManagement dialog
  13. extern COAMApp theApp;
  14. CUserManagement::CUserManagement(CWnd* pParent /*=NULL*/)
  15. : CDialog(CUserManagement::IDD, pParent)
  16. {
  17. //{{AFX_DATA_INIT(CUserManagement)
  18. m_password = _T("");
  19. m_passwordc = _T("");
  20. m_power = _T("");
  21. m_username = _T("");
  22. //}}AFX_DATA_INIT
  23. }
  24. void CUserManagement::DoDataExchange(CDataExchange* pDX)
  25. {
  26. CDialog::DoDataExchange(pDX);
  27. //{{AFX_DATA_MAP(CUserManagement)
  28. DDX_Control(pDX, IDC_USERLIST, m_userlist);
  29. DDX_Control(pDX, IDC_DELITEM, m_cDelItem);
  30. DDX_Control(pDX, IDC_ADDITEM, m_cAddItem);
  31. DDX_Text(pDX, IDC_PASSWORD, m_password);
  32. DDX_Text(pDX, IDC_PASSWORDC, m_passwordc);
  33. DDX_Text(pDX, IDC_POWER, m_power);
  34. DDX_Text(pDX, IDC_USERNAME, m_username);
  35. //}}AFX_DATA_MAP
  36. }
  37. BEGIN_MESSAGE_MAP(CUserManagement, CDialog)
  38. //{{AFX_MSG_MAP(CUserManagement)
  39. ON_BN_CLICKED(IDC_ADDITEM, OnAdditem)
  40. ON_BN_CLICKED(IDC_DELITEM, OnDelitem)
  41. ON_NOTIFY(LVN_ITEMCHANGED, IDC_USERLIST, OnItemchangedUserlist)
  42. //}}AFX_MSG_MAP
  43. END_MESSAGE_MAP()
  44. /////////////////////////////////////////////////////////////////////////////
  45. // CUserManagement message handlers
  46. BOOL CUserManagement::OnInitDialog() 
  47. {
  48. CDialog::OnInitDialog();
  49. m_cDelItem.EnableWindow(FALSE);
  50. ::SendMessage(m_userlist.m_hWnd, LVM_SETEXTENDEDLISTVIEWSTYLE,LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);
  51. //////////为列表控件添加列//////////
  52.    m_userlist.InsertColumn(1,"用户ID",LVCFMT_LEFT,60);
  53. m_userlist.InsertColumn(1,"用户名",LVCFMT_LEFT,100);
  54. m_userlist.InsertColumn(2,"用户权限",LVCFMT_LEFT,60);
  55. //////////读取数据库中的信息添加到列表控件///////////
  56. int nItem;
  57. _variant_t vUsername,vID,vPower;
  58. try
  59. {
  60. m_pRecordset.CreateInstance("ADODB.Recordset");
  61. m_pRecordset->Open("SELECT * FROM users",_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  62. m_bSuccess = TRUE;
  63. while(!m_pRecordset->adoEOF)
  64. {
  65. vID = m_pRecordset->GetCollect("ID");
  66. vUsername = m_pRecordset->GetCollect("username");
  67. vPower = m_pRecordset->GetCollect("userpower");
  68. nItem=m_userlist.InsertItem(0xffff,(_bstr_t)vID);
  69. m_userlist.SetItem(nItem,1,1,(_bstr_t)vUsername,NULL,0,0,0);
  70. m_userlist.SetItem(nItem,2,1,(_bstr_t)vPower,NULL,0,0,0);
  71. m_pRecordset->MoveNext();
  72. }
  73. }
  74. catch(_com_error e)///捕捉异常
  75. {
  76. AfxMessageBox("读取数据库失败!");///显示错误信息
  77. }
  78. // Add "About..." menu item to system menu.
  79. // IDM_ABOUTBOX must be in the system command range.
  80. // Set the icon for this dialog.  The framework does this automatically
  81. //  when the application's main window is not a dialog
  82. // TODO: Add extra initialization here
  83. return TRUE;  // return TRUE unless you set the focus to a control
  84.               // EXCEPTION: OCX Property Pages should return FALSE
  85. }
  86. void CUserManagement::LoadData()
  87. {
  88. m_pRecordset->Move(m_nCurrentSel,_variant_t((long)adBookmarkFirst));
  89. vUserID = m_pRecordset->GetCollect("ID");
  90. vUsername = m_pRecordset->GetCollect("username");
  91. vPower = m_pRecordset->GetCollect("userpower");
  92.     vPassword=m_pRecordset->GetCollect("password");
  93. // m_nUserID = vUserID.lVal;
  94. m_username = (LPCTSTR)(_bstr_t)vUsername;
  95. m_power = (LPCTSTR)(_bstr_t)vPower;
  96. m_password = (LPCTSTR)(_bstr_t)vPassword;
  97. m_passwordc = (LPCTSTR)(_bstr_t)vPassword;
  98. UpdateData(FALSE);
  99. }
  100. /////将编辑框的数据保存到记录集与列表框
  101. void CUserManagement::SaveData()
  102. {
  103. if(!m_pRecordset->adoEOF && m_nCurrentSel >= 0 && m_bAutoSave)
  104. {
  105. // vUserID = (long)m_nUserID;
  106. vUsername = m_username;
  107. vPower = m_power;
  108. vPassword = m_password;
  109. //m_pRecordset->PutCollect("ID",vUserID);
  110. m_pRecordset->PutCollect("username",vUsername);
  111. m_pRecordset->PutCollect("userpower",vPower);
  112. m_pRecordset->PutCollect("password",vPassword);
  113. // m_userlist.SetItem(m_nCurrentSel,0,LVIF_TEXT,(_bstr_t)vUserID,NULL,0,0,0);
  114. m_userlist.SetItem(m_nCurrentSel,1,LVIF_TEXT,(_bstr_t)vUsername,NULL,0,0,0);
  115. m_userlist.SetItem(m_nCurrentSel,2,LVIF_TEXT,(_bstr_t)vPower,NULL,0,0,0);
  116. // m_userlist.SetItem(m_nCurrentSel,3,LVIF_TEXT,(_bstr_t)vBirthday,NULL,0,0,0);
  117. }
  118. }
  119. void CUserManagement::OnAdditem() 
  120. {
  121. if(UpdateData())
  122. if(m_username.GetLength()>0)
  123. {
  124. m_pRecordset->AddNew();
  125. m_nCurrentSel = m_userlist.InsertItem(0xffff,"");
  126. SaveData();///保存数据
  127. m_userlist.SetItemState(m_nCurrentSel,LVIS_SELECTED|LVIS_FOCUSED,LVIS_SELECTED|LVIS_FOCUSED);
  128. m_userlist.SetHotItem(m_nCurrentSel);
  129. m_userlist.SetFocus();
  130. }
  131. else
  132. AfxMessageBox("请输入用户名");
  133. }
  134. void CUserManagement::OnDelitem() 
  135. {
  136. // TODO: Add your control notification handler code here
  137. m_bAutoSave = FALSE;
  138. if(m_nCurrentSel >= 0)
  139. {
  140. m_userlist.DeleteItem(m_nCurrentSel);
  141. int count = m_userlist.GetItemCount();
  142. if(count <= m_nCurrentSel)
  143. m_nCurrentSel = count-1;
  144. m_pRecordset->Delete(adAffectCurrent);
  145.  
  146. m_pRecordset->MoveNext();
  147. if (!m_pRecordset->adoEOF)
  148.         { 
  149. LoadData();
  150. m_userlist.SetItemState(m_nCurrentSel,LVIS_SELECTED|LVIS_FOCUSED,LVIS_SELECTED|LVIS_FOCUSED);
  151. m_userlist.SetFocus();
  152. }
  153. else
  154. m_cDelItem.EnableWindow(FALSE);;
  155. }
  156. m_bAutoSave = TRUE;
  157. }
  158. void CUserManagement::OnItemchangedUserlist(NMHDR* pNMHDR, LRESULT* pResult) 
  159. {
  160. NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
  161. if(pNMListView->uNewState&LVIS_SELECTED)
  162. {
  163. UpdateData();
  164. SaveData();///保存旧数据
  165. m_nCurrentSel = pNMListView->iItem;
  166. LoadData();///加载新数据
  167. m_cDelItem.EnableWindow();
  168. }
  169. *pResult = 0;
  170. }
  171. void CUserManagement::OnOK() 
  172. {
  173. if(m_bSuccess)
  174. {
  175. m_pRecordset->Update();
  176. m_pRecordset->Close();
  177. }
  178. CDialog::OnOK();
  179. }