📄 usermaintenancedlg.cpp
字号:
// UserMaintenanceDlg.cpp : implementation file
//
#include "stdafx.h"
#include "UserMaintenance.h"
#include "UserMaintenanceDlg.h"
#include "UserMasterSet.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CUserMaintenanceDlg dialog
CUserMaintenanceDlg::CUserMaintenanceDlg(CWnd* pParent /*=NULL*/)
: CDialog(CUserMaintenanceDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CUserMaintenanceDlg)
m_strUserName = _T("");
m_strUserID = _T("");
m_iStatus = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CUserMaintenanceDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CUserMaintenanceDlg)
DDX_Control(pDX, IDC_BTNSAVE, m_btnSave);
DDX_Control(pDX, IDC_BTNDELETE, m_btnDelete);
DDX_Control(pDX, IDC_BTNCLOSE, m_btnClose);
DDX_Control(pDX, IDC_LBXUSERS, m_lbxUsers);
DDX_Text(pDX, IDC_EDTUSERNAME, m_strUserName);
DDX_Text(pDX, IDC_EDTUSERID, m_strUserID);
DDX_Text(pDX, IDC_EDTSTATUS, m_iStatus);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CUserMaintenanceDlg, CDialog)
//{{AFX_MSG_MAP(CUserMaintenanceDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_LBN_SELCHANGE(IDC_LBXUSERS, OnSelchangeLbxusers)
ON_BN_CLICKED(IDC_BTNCLOSE, OnBtnclose)
ON_BN_CLICKED(IDC_BTNSAVE, OnBtnsave)
ON_BN_CLICKED(IDC_BTNDELETE, OnBtndelete)
//}}AFX_MSG_MAP
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
// TODO: Add extra initialization here
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, (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 to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CUserMaintenanceDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CUserMaintenanceDlg::FillListboxWithUsers()
{
int iIndex;
CDatabase db;
CUserMasterSet* pUserMasterSet = NULL;
TRY
{
if (db.Open("Visual C++ Bible - Chapter 17"))
{
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;
iIndex = m_lbxUsers.AddString(pUserMasterSet->m_sUserName);
m_lbxUsers.SetItemData(iIndex, (DWORD)pUser);
pUserMasterSet->MoveNext();
}
pUserMasterSet->Close();
delete pUserMasterSet;
db.Close();
}
}
CATCH(CDBException, p)
{
AfxMessageBox(p->m_strError);
if (pUserMasterSet)
{
if (pUserMasterSet->IsOpen())
{
pUserMasterSet->Close();
}
delete pUserMasterSet;
}
if (db.IsOpen())
{
db.Close();
}
}
END_CATCH
}
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::OnSelchangeLbxusers()
{
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);
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::OnBtnclose()
{
CDialog::OnCancel();
}
void CUserMaintenanceDlg::OnBtnsave()
{
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("Visual C++ Bible - Chapter 17"))
{
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, p)
{
AfxMessageBox(p->m_strError);
if (db.IsOpen())
{
db.Close();
}
}
END_CATCH
return bSuccess;
}
void CUserMaintenanceDlg::OnBtndelete()
{
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("Visual C++ Bible - Chapter 17"))
{
CString strSQL = CString("DELETE FROM UserMaster WHERE ");
strSQL += CString("sUserID = '") + pUser->m_strUserID + CString("'");
db.ExecuteSQL(strSQL);
bSuccess = TRUE;
}
}
CATCH(CDBException, p)
{
AfxMessageBox(p->m_strError);
if (db.IsOpen())
{
db.Close();
}
}
END_CATCH
return bSuccess;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -