📄 daousermaintenancedlg.cpp
字号:
// DaoUserMaintenanceDlg.cpp : implementation file
//
#include "stdafx.h"
#include "DaoUserMaintenance.h"
#include "DaoUserMaintenanceDlg.h"
#include "DaoUserMasterSet.h"
#include "about.h"
#define DEMO_DB _T("..\\..\\Chap26\\Database\\vc6bib.mdb")
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDaoUserMaintenanceDlg dialog
CDaoUserMaintenanceDlg::CDaoUserMaintenanceDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDaoUserMaintenanceDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDaoUserMaintenanceDlg)
m_strUserID = _T("");
m_strUserName = _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 CDaoUserMaintenanceDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDaoUserMaintenanceDlg)
DDX_Control(pDX, IDC_BTN_DELETE, m_btnDelete);
DDX_Control(pDX, IDC_BTN_SAVE, m_btnSave);
DDX_Control(pDX, IDC_LBX_USERS, m_lbxUsers);
DDX_Text(pDX, IDC_EDT_USERID, m_strUserID);
DDX_Text(pDX, IDC_EDT_USERNAME, m_strUserName);
DDX_Text(pDX, IDC_EDT_STATUS, m_iStatus);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDaoUserMaintenanceDlg, CDialog)
//{{AFX_MSG_MAP(CDaoUserMaintenanceDlg)
ON_WM_SYSCOMMAND()
ON_BN_CLICKED(ID_APP_ABOUT, OnAbout)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTN_SAVE, OnBtnSave)
ON_BN_CLICKED(IDC_BTN_DELETE, OnBtnDelete)
ON_LBN_SELCHANGE(IDC_LBX_USERS, OnSelchangeLbxUsers)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDaoUserMaintenanceDlg message handlers
BOOL CDaoUserMaintenanceDlg::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 CDaoUserMaintenanceDlg::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 CDaoUserMaintenanceDlg::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 CDaoUserMaintenanceDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CDaoUserMaintenanceDlg::OnAbout()
{
CAboutDlg().DoModal();
}
void CDaoUserMaintenanceDlg::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");
}
}
void CDaoUserMaintenanceDlg::FillListboxWithUsers()
{
int iIndex;
CDaoDatabase db;
CDaoUserMasterSet rs;
try
{
rs.Open();
while (!rs.IsEOF())
{
CUser* pUser = new CUser();
pUser->m_strUserID = rs.m_sUserID;
pUser->m_strUserName = rs.m_sUserName;
pUser->m_iStatus = rs.m_iStatus;
iIndex = m_lbxUsers.AddString(rs.m_sUserName);
m_lbxUsers.SetItemData(iIndex, (DWORD)pUser);
rs.MoveNext();
}
}
catch(CDaoException* pe)
{
AfxMessageBox(pe->m_pErrorInfo->m_strDescription,
MB_ICONEXCLAMATION );
}
}
void CDaoUserMaintenanceDlg::InitControls()
{
m_lbxUsers.SetCurSel(-1);
m_strUserID = "";
m_strUserName = "";
m_iStatus = -1;
m_btnSave.EnableWindow(FALSE);
m_btnDelete.EnableWindow(FALSE);
m_lbxUsers.SetFocus();
UpdateData(FALSE);
}
void CDaoUserMaintenanceDlg::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);
}
}
BOOL CDaoUserMaintenanceDlg::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;
}
BOOL CDaoUserMaintenanceDlg::SaveUser(CUser* pUser)
{
BOOL bSuccess = FALSE;
CDaoDatabase db;
try
{
db.Open(DEMO_DB);
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.Execute(strSQL);
bSuccess = TRUE;
}
catch(CDaoException* pe)
{
AfxMessageBox(pe->m_pErrorInfo->m_strDescription,
MB_ICONEXCLAMATION );
pe->Delete();
}
return bSuccess;
}
void CDaoUserMaintenanceDlg::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 CDaoUserMaintenanceDlg::DeleteUser(CUser* pUser)
{
BOOL bSuccess = FALSE;
CDaoDatabase db;
try
{
db.Open(DEMO_DB);
CString strSQL;
strSQL.Format("DELETE FROM UserMaster WHERE "
"sUserID = '%s'", pUser->m_strUserID);
db.Execute(strSQL);
bSuccess = TRUE;
}
catch(CDaoException* pe)
{
AfxMessageBox(pe->m_pErrorInfo->m_strDescription,
MB_ICONEXCLAMATION );
pe->Delete();
}
return bSuccess;
}
void CDaoUserMaintenanceDlg::OnDestroy()
{
CDialog::OnDestroy();
CUser* pUser;
for (int i = 0; i < m_lbxUsers.GetCount(); i++)
{
pUser = (CUser*)m_lbxUsers.GetItemData(i);
if (pUser)
{
delete pUser;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -