📄 daousermaintenancedlg.cpp
字号:
// DaoUserMaintenanceDlg.cpp : implementation file
//
#include "stdafx.h"
#include "DaoUserMaintenance.h"
#include "DaoUserMaintenanceDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#include "UserMaster.h"
// 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()
// CDaoUserMaintenanceDlg dialog
CDaoUserMaintenanceDlg::CDaoUserMaintenanceDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDaoUserMaintenanceDlg::IDD, pParent)
, m_strUserID(_T(""))
, m_strUserName(_T(""))
, m_iStatus(0)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CDaoUserMaintenanceDlg::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_Text(pDX, IDC_EDIT1, m_strUserID);
DDX_Text(pDX, IDC_EDIT2, m_strUserName);
DDX_Text(pDX, IDC_EDIT3, m_iStatus);
}
BEGIN_MESSAGE_MAP(CDaoUserMaintenanceDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BUTTON4, OnBnClickedButton4)
ON_BN_CLICKED(IDOK, OnBnClickedOk)
ON_LBN_SELCHANGE(IDC_LIST1, OnLbnSelchangeList1)
ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedButton2)
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
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, 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 CDaoUserMaintenanceDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CDaoUserMaintenanceDlg::OnBnClickedButton4()
{
CAboutDlg().DoModal();
}
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 CDaoUserMaintenanceDlg::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");
}
}
void CDaoUserMaintenanceDlg::FillListboxWithUsers()
{
try
{
int iIndex;
CUserMaster rs;
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::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 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
{
CUserMaster rs;
db.Open(rs.GetDefaultDBName());
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::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 CDaoUserMaintenanceDlg::DestroyWindow()
{
CDialog::OnDestroy();
CUser* pUser;
for (int i = 0; i < m_lbxUsers.GetCount(); i++)
{
pUser = (CUser*)m_lbxUsers.GetItemData(i);
if (pUser)
{
delete pUser;
}
}
return CDialog::DestroyWindow();
}
BOOL CDaoUserMaintenanceDlg::DeleteUser(CUser* pUser)
{
BOOL bSuccess = FALSE;
CDaoDatabase db;
try
{
CUserMaster rs;
db.Open(rs.GetDefaultDBName());
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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -