📄 usermaintenancedlg.cpp
字号:
// UserMaintenanceDlg.cpp : implementation file
//
#include "stdafx.h"
#include "UserMaintenance.h"
#include "UserMaintenanceDlg.h"
#include "UserMasterSet.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedOk();
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
ON_BN_CLICKED(IDOK, OnBnClickedOk)
END_MESSAGE_MAP()
// CUserMaintenanceDlg dialog
CUserMaintenanceDlg::CUserMaintenanceDlg(CWnd* pParent /*=NULL*/)
: CDialog(CUserMaintenanceDlg::IDD, pParent)
, m_strUserID(_T(""))
, m_strUserName(_T(""))
, m_iStatus(0)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CUserMaintenanceDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST1, m_lbxUsers);
DDX_Control(pDX, IDOK, m_btnSave);
DDX_Control(pDX, IDC_BUTTON2, m_btnDelete);
DDX_Control(pDX, IDCANCEL, m_btnClose);
DDX_Text(pDX, IDC_EDIT1, m_strUserID);
DDX_Text(pDX, IDC_EDIT2, m_strUserName);
DDX_Text(pDX, IDC_EDIT3, m_iStatus);
}
BEGIN_MESSAGE_MAP(CUserMaintenanceDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BUTTON4, OnBnClickedButton4)
ON_LBN_SELCHANGE(IDC_LIST1, OnLbnSelchangeList1)
ON_BN_CLICKED(IDOK, OnBnClickedOk)
ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedButton2)
END_MESSAGE_MAP()
// CUserMaintenanceDlg message handlers
BOOL CUserMaintenanceDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
FillListboxWithUsers();
InitControls();
return TRUE; // return TRUE unless you set the focus to a control
}
void CUserMaintenanceDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CUserMaintenanceDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CUserMaintenanceDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CAboutDlg::OnBnClickedOk()
{
CWaitCursor wait;
CString strUrl = "http://www.thecodechannel.com/redirect.asp?u=/&s=vcnb";
if (32 >= (int)ShellExecute(NULL, "open", strUrl, NULL, NULL, SW_SHOWNORMAL))
{
AfxMessageBox("::ShellExecute failed to open this link!");
}
}
void CUserMaintenanceDlg::OnBnClickedButton4()
{
CAboutDlg().DoModal();
}
void CUserMaintenanceDlg::FillListboxWithUsers()
{
int iIndex;
CDatabase db;
CUserMasterSet* pUserMasterSet = NULL;
try
{
pUserMasterSet = new CUserMasterSet();
pUserMasterSet->Open();
while (!pUserMasterSet->IsEOF())
{
CUser* pUser = new CUser();
pUser->m_strUserID = pUserMasterSet->m_sUserID;
pUser->m_strUserName =
pUserMasterSet->m_sUserName;
pUser->m_iStatus = pUserMasterSet->m_iStatus;
// Convert from the record set UNICODE
// string to a form acceptable to the
// CListBox::AddString function.
CString str(pUserMasterSet->m_sUserName);
// Add the user name to the list box.
iIndex = m_lbxUsers.AddString(str);
m_lbxUsers.SetItemData(iIndex, (DWORD)pUser);
pUserMasterSet->MoveNext();
}
pUserMasterSet->Close();
delete pUserMasterSet;
}
catch(CDBException* pe)
{
AfxMessageBox(pe->m_strError);
if (pUserMasterSet)
{
if (pUserMasterSet->IsOpen())
{
pUserMasterSet->Close();
}
delete pUserMasterSet;
}
if (db.IsOpen())
{
db.Close();
}
pe->Delete();
}
}
void CUserMaintenanceDlg::InitControls()
{
m_lbxUsers.SetCurSel(-1);
m_strUserID = "";
m_strUserName = "";
m_iStatus = -1;
m_btnSave.EnableWindow(FALSE);
m_btnDelete.EnableWindow(FALSE);
m_btnClose.EnableWindow(TRUE);
m_lbxUsers.SetFocus();
UpdateData(FALSE);
}
void CUserMaintenanceDlg::OnLbnSelchangeList1()
{
int iIndex;
CUser* pUser;
if (GetSelectedUser(&iIndex, &pUser))
{
m_strUserID = pUser->m_strUserID;
m_strUserName = pUser->m_strUserName;
m_iStatus = pUser->m_iStatus;
m_btnSave.EnableWindow(TRUE);
m_btnDelete.EnableWindow(TRUE);
UpdateData(FALSE);
}
}
BOOL CUserMaintenanceDlg::GetSelectedUser(
int* piIndex, CUser** ppUser)
{
BOOL bSuccess = FALSE;
int iIndex;
if (LB_ERR != (iIndex = m_lbxUsers.GetCurSel()))
{
*piIndex = iIndex;
*ppUser = (CUser*)m_lbxUsers.GetItemData(iIndex);
bSuccess = TRUE;
}
return bSuccess;
}
void CUserMaintenanceDlg::OnBnClickedOk()
{
int iCurrIndex;
CUser* pUser = NULL;
if (GetSelectedUser(&iCurrIndex, &pUser))
{
ASSERT(pUser);
if (pUser)
{
UpdateData();
CString strPrevUserID;
strPrevUserID = m_strUserID;
CString strPrevUserName;
strPrevUserName = m_strUserName;
int iPrevStatus = m_iStatus;
pUser->m_strUserID = m_strUserID;
pUser->m_strUserName = m_strUserName;
pUser->m_iStatus = m_iStatus;
if (SaveUser(pUser))
{
if (LB_ERR == m_lbxUsers.DeleteString(iCurrIndex))
{
AfxMessageBox("The User ID was Saved, "
"but the previous User ID could not be "
"removed from the listbox.");
}
else
{
int iNewIndex = m_lbxUsers.AddString(
pUser->m_strUserName);
if ((LB_ERR == iNewIndex) || (LB_ERRSPACE ==
iNewIndex))
{
AfxMessageBox("The User ID was Saved, but "
"the new User ID could not "
"be added to the listbox.");
}
if (LB_ERR == m_lbxUsers.SetItemData(iNewIndex,
(DWORD)pUser))
{
AfxMessageBox("SetItemData returned LB_ERR. "
"This will probably cause "
"serious problems if you attempt to update "
"or delete this item from the listbox");
}
}
InitControls();
UpdateData(FALSE);
}
else
{
pUser->m_strUserID = strPrevUserID;
pUser->m_strUserName = strPrevUserName;
pUser->m_iStatus = iPrevStatus;
AfxMessageBox("SaveUser failed");
}
}
}
else
{
// should never get here because of
// enabling/disabling button on lbx selection
AfxMessageBox("You must first select a "
"User ID to Save");
}
}
BOOL CUserMaintenanceDlg::SaveUser(CUser* pUser)
{
BOOL bSuccess = FALSE;
CDatabase db;
try
{
if (db.Open("VCNet Bible"))
{
CString strSQL = CString("UPDATE UserMaster SET ");
strSQL += CString("sUserName = '") +
pUser->m_strUserName + CString("', ");
strSQL += CString("iStatus = ");
char szStatus[10];
itoa(pUser->m_iStatus, szStatus, 10);
strSQL += szStatus;
strSQL += CString(" WHERE sUserID = ");
strSQL += CString("'") + pUser->m_strUserID +
CString("'");
db.ExecuteSQL(strSQL);
bSuccess = TRUE;
}
}
catch(CDBException* pe)
{
AfxMessageBox(pe->m_strError);
if (db.IsOpen())
{
db.Close();
}
pe->Delete();
}
return bSuccess;
}
void CUserMaintenanceDlg::OnBnClickedButton2()
{
int iIndex;
CUser* pUser = NULL;
if (GetSelectedUser(&iIndex, &pUser))
{
ASSERT(pUser);
if (pUser)
{
UpdateData();
CString strMsg;
strMsg = "Are you sure that you want to delete "
"the User ";
strMsg += "'";
strMsg += pUser->m_strUserName;
strMsg += "'?";
if (IDYES == AfxMessageBox(strMsg, MB_YESNOCANCEL))
{
if (DeleteUser(pUser))
{
if (LB_ERR == m_lbxUsers.DeleteString(iIndex))
{
AfxMessageBox("The User was successfully "
"Deleted, but there was an error in "
"removing it from the listbox.");
}
InitControls();
UpdateData(FALSE);
}
else
{
AfxMessageBox("DeleteUser failed");
}
}
}
}
else
{
// should never get here because of
// enabling/disabling button on lbx selection
AfxMessageBox("You must first select a User "
"ID to Delete");
}
}
BOOL CUserMaintenanceDlg::DeleteUser(CUser* pUser)
{
BOOL bSuccess = FALSE;
CDatabase db;
try
{
if (db.Open("VCNet Bible"))
{
CString strSQL = CString("DELETE FROM UserMaster "
"WHERE ");
strSQL += CString("sUserID = '") +
pUser->m_strUserID + CString("'");
db.ExecuteSQL(strSQL);
bSuccess = TRUE;
}
}
catch(CDBException* pe)
{
AfxMessageBox(pe->m_strError);
if (db.IsOpen())
{
db.Close();
}
}
return bSuccess;
}
BOOL CUserMaintenanceDlg::DestroyWindow()
{
CUser* pUser;
int nUsers = m_lbxUsers.GetCount();
for (int i = 0; i < nUsers; i++)
{
pUser=
(CUser*)m_lbxUsers.GetItemData(i);
if (pUser)
{
delete pUser;
}
}
return CDialog::DestroyWindow();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -