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

📄 userinfodlg.cpp

📁 用于网络管理统计的
💻 CPP
字号:
// userInfoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CustomerManage.h"
#include "userInfoDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CuserInfoDlg dialog


CuserInfoDlg::CuserInfoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CuserInfoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CuserInfoDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	bIsModify = false;
	bIsAdd = false;
	sID = "";
}


void CuserInfoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CuserInfoDlg)
	DDX_Control(pDX, IDC_LIST_USER_INFO, m_list);
	DDX_Control(pDX, IDC_DELETE_USER, m_ok);
	DDX_Control(pDX, IDCANCEL, m_cancel);
	DDX_Control(pDX, IDC_BUTTON_USER_SAVE, m_save);
	DDX_Control(pDX, IDC_BUTTON_USER_MODIFY, m_modify);
	DDX_Control(pDX, IDC_BUTTON_USER_ADD, m_add);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CuserInfoDlg, CDialog)
	//{{AFX_MSG_MAP(CuserInfoDlg)
	ON_WM_PAINT()
	ON_NOTIFY(NM_CLICK, IDC_LIST_USER_INFO, OnClickListUserInfo)
	ON_BN_CLICKED(IDC_BUTTON_USER_MODIFY, OnButtonUserModify)
	ON_BN_CLICKED(IDC_BUTTON_USER_ADD, OnButtonUserAdd)
	ON_BN_CLICKED(IDC_BUTTON_USER_SAVE, OnButtonUserSave)
	ON_BN_CLICKED(IDC_DELETE_USER, OnDeleteUser)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CuserInfoDlg message handlers

void CuserInfoDlg::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	CRect rcClient;
	GetClientRect(&rcClient);
	bar.DrawLeft(&dc,CRect(0,0,10,rcClient.bottom));
	bar.DrawTop(&dc,CRect(0,0,rcClient.right,10));
	bar.DrawRight(&dc,CRect(rcClient.right-10,0,rcClient.right,rcClient.bottom));
	bar.DrawBottom(&dc,CRect(0,rcClient.bottom-10,rcClient.right,rcClient.bottom));	
	// Do not call CDialog::OnPaint() for painting messages
}

BOOL CuserInfoDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	SetCtrlBkColor();
	InitialListView();
	InsertData();
	InsertComPopedom();
	SetCtrlStatus(false);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
void CuserInfoDlg::SetCtrlBkColor()
{
	m_save.SetActiveBgColor (RGB(162,189,255));
	m_save.SetInactiveBgColor (RGB(162,189,255));
	m_ok.SetActiveBgColor (RGB(162,189,255));
	m_ok.SetInactiveBgColor (RGB(162,189,255));
	m_add.SetActiveBgColor (RGB(162,189,255));
	m_add.SetInactiveBgColor (RGB(162,189,255));
	m_cancel.SetActiveBgColor (RGB(162,189,255));
	m_cancel.SetInactiveBgColor (RGB(162,189,255));
	m_modify.SetActiveBgColor (RGB(162,189,255));
	m_modify.SetInactiveBgColor (RGB(162,189,255));
    m_save.EnableWindow(false);
}
void CuserInfoDlg::InsertData()
{
	int Index = 0;
	CString strSQL;
	CString sID;
	_RecordsetPtr m_pADOSet;
	try
	{
		m_pADOSet.CreateInstance(__uuidof(Recordset));
		strSQL = "select * from Users";
		m_pADOSet = theApp.ADOConn->Execute(strSQL.AllocSysString(),NULL,adCmdText);
		while(!m_pADOSet->adoEOF)
		{
			Index = m_list.GetItemCount();
			m_list.InsertItem(Index,"");
			m_list.SetItemText(Index,0,(LPCSTR)(_bstr_t)m_pADOSet->GetCollect("UserName"));
			if(m_pADOSet->GetCollect("Manager").boolVal)
			{
				m_list.SetItemText(Index,1,"管理员");
			}
			else
			{
				m_list.SetItemText(Index,1,"普通管理员");
			}
			sID.Format("%d",m_pADOSet->GetCollect("ID").iVal);
			m_list.SetItemText(Index,2,sID);
			m_pADOSet->MoveNext();
		}
		m_pADOSet->Close();
	}
	catch(_com_error e)
	{
		MessageBox(e.Description(),MB_OK);
		return;
	}
}
void CuserInfoDlg::InitialListView()
{
    ListView_SetExtendedListViewStyle(m_list.m_hWnd,
             LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES|
             LVS_EX_FLATSB|LVS_EX_HEADERDRAGDROP);
	CRect rect;
	m_list.GetClientRect(&rect);
	rect.NormalizeRect();
	m_list.InsertColumn(0,"用户姓名",LVCFMT_CENTER,(int)rect.Width()/2);
	m_list.InsertColumn(1,"用户权限",LVCFMT_LEFT,(int)rect.Width()/2);
    m_list.InsertColumn(2,"ID",LVCFMT_LEFT,0);
}
void CuserInfoDlg::InsertComPopedom()
{
	int Index = 0;
	CComboBox *pBox = (CComboBox*)GetDlgItem(IDC_COMBO_POPEDOM);
	Index = pBox->AddString("普通管理员");
	pBox->SetItemData(Index,0);
	Index = pBox->AddString("管理员");
	pBox->SetItemData(Index,1);
	pBox->SetCurSel(0);
}
void CuserInfoDlg::SetCtrlStatus(BOOL bStatus)
{
	GetDlgItem(IDC_EDIT_USER_NAME)->EnableWindow(bStatus);
	GetDlgItem(IDC_EDIT_USER_PASSWORD)->EnableWindow(bStatus);
	GetDlgItem(IDC_COMBO_POPEDOM)->EnableWindow(bStatus);
}

void CuserInfoDlg::OnClickListUserInfo(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	int Index = 0;
	Index = m_list.GetSelectionMark();
    GetDlgItem(IDC_EDIT_USER_NAME)->SetWindowText(m_list.GetItemText(Index,0));
	GetDlgItem(IDC_COMBO_POPEDOM)->SetWindowText(m_list.GetItemText(Index,1));
	sID = m_list.GetItemText(Index,2);
	*pResult = 0;
}

void CuserInfoDlg::OnButtonUserModify() 
{
	// TODO: Add your control notification handler code here
	bIsAdd = false;
	bIsModify = true;
	SetCtrlStatus(true);
	GetDlgItem(IDC_BUTTON_USER_SAVE)->EnableWindow(true);
}

void CuserInfoDlg::OnButtonUserAdd() 
{
	// TODO: Add your control notification handler code here
	bIsModify = false;
	bIsAdd = true;
	SetCtrlStatus(true);
	GetDlgItem(IDC_EDIT_USER_NAME)->SetWindowText("");
	GetDlgItem(IDC_EDIT_USER_PASSWORD)->SetWindowText("");
	GetDlgItem(IDC_BUTTON_USER_SAVE)->EnableWindow(true);
}

void CuserInfoDlg::OnButtonUserSave() 
{
	// TODO: Add your control notification handler code here
	if(bIsModify)
	{
		CString selectUserName;
	    GetDlgItem(IDC_EDIT_USER_NAME)->GetWindowText(selectUserName);
	    CComboBox *pBox = (CComboBox*)GetDlgItem(IDC_COMBO_POPEDOM);
	    if(sID == "")
		{
		    MessageBox("请你选择你要修改的用户",MB_OK);
		    return;
		}
	    if(theApp.m_userinfo.Popedom == 0)
		{
		    if(selectUserName != theApp.m_userinfo.UserName)
			{
			    MessageBox("你没有权限修改别的用户",MB_OK);
			    return;
			}
		}
    	else
		{
		    CString strSQL;
			int IndexID = 0;
			IndexID = pBox->GetItemData(pBox->GetCurSel());
		    strSQL.Format("UpDate Users Set UserName='%s',Manager='%d' where ID=%s",\
			              selectUserName,IndexID,sID);
			theApp.m_userinfo.UserName = selectUserName;
			theApp.m_userinfo.Popedom = IndexID;
		    theApp.ADOConn->Execute(strSQL.AllocSysString(),NULL,adCmdText);
	    	sID = "";
			CString sPopedom;
			if(IndexID == 0)
                 sPopedom = "普通管理员";
			else
				sPopedom = "管理员";
			m_list.SetItemText(m_list.GetSelectionMark(),0,selectUserName);
			m_list.SetItemText(m_list.GetSelectionMark(),1,sPopedom);
		    MessageBox("修改成功!!",MB_OK);
		}
	}
	else
	{
		CString sName;
		CString sPassword;
		GetDlgItem(IDC_EDIT_USER_NAME)->GetWindowText(sName);
		GetDlgItem(IDC_EDIT_USER_PASSWORD)->GetWindowText(sPassword);
		CComboBox *pBox = (CComboBox*)GetDlgItem(IDC_COMBO_POPEDOM);
		if(sName == "")
		{
			MessageBox("用户名不能为空",MB_OK);
			return;
		}
		if(sPassword == "")
		{
			MessageBox("密码不能为空",MB_OK);
			return;
		}
		CString strSQL;
		_RecordsetPtr m_pADOSet;
		try
		{
			m_pADOSet.CreateInstance(__uuidof(Recordset));
            strSQL.Format("select * from Users where UserName='%s'",sName);
			m_pADOSet = theApp.ADOConn->Execute(strSQL.AllocSysString(),NULL,adCmdText);
			if(!m_pADOSet->adoEOF)
			{
				MessageBox("此用户名已存在,请你选择别的",MB_OK);
				m_pADOSet->Close();
				return;
			}
		    strSQL.Format("Insert into Users(UserName,Pword,Manager) Values('%s','%s',%d)",sName,sPassword,pBox->GetItemData(pBox->GetCurSel()));
	    	theApp.ADOConn->Execute(strSQL.AllocSysString(),NULL,adCmdText);
		}
		catch(_com_error e)
		{
			MessageBox(e.Description(),MB_OK);
			return;
		}
	}
}

void CuserInfoDlg::OnDeleteUser() 
{
	// TODO: Add your control notification handler code here
	CString sName = "";
	GetDlgItem(IDC_EDIT_USER_NAME)->GetWindowText(sName);
	if(sName == "")
	{
		MessageBox("请你选择要删除的记录",MB_OK);
		return;
	}
	if(MessageBox("你真的要删除吗?","提示",MB_ICONEXCLAMATION|MB_YESNO) == IDYES)
	{
		CString strSQL;
		int Index = 0;
		Index = m_list.GetSelectionMark();
		strSQL.Format("delete from Users where ID=%s",sID);
		try
		{
			theApp.ADOConn->Execute(strSQL.AllocSysString(),NULL,adCmdText);
		}
		catch(_com_error e)
		{
			MessageBox(e.Description(),MB_OK);
			return;
		}
		m_list.DeleteItem(Index);
		GetDlgItem(IDC_EDIT_USER_NAME)->SetWindowText("");
		MessageBox("删除成功",MB_OK);
	}
}

⌨️ 快捷键说明

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