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

📄 usermaintenancedlg.cpp

📁 主要介绍vc++6.0的编程过程
💻 CPP
字号:
// UserMaintenanceDlg.cpp : implementation file
//

#include "stdafx.h"
#include "UserMaintenance.h"
#include "UserMaintenanceDlg.h"

#include "UserMasterSet.h"
#include "about.h"

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

/////////////////////////////////////////////////////////////////////////////
// CUserMaintenanceDlg dialog

CUserMaintenanceDlg::CUserMaintenanceDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CUserMaintenanceDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CUserMaintenanceDlg)
	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 CUserMaintenanceDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CUserMaintenanceDlg)
	DDX_Control(pDX, IDC_BTN_CLOSE, m_btnClose);
	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(CUserMaintenanceDlg, CDialog)
	//{{AFX_MSG_MAP(CUserMaintenanceDlg)
	ON_WM_SYSCOMMAND()
	ON_BN_CLICKED(ID_APP_ABOUT, OnAbout)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_LBN_SELCHANGE(IDC_LBX_USERS, OnSelchangeLbxUsers)
	ON_BN_CLICKED(IDC_BTN_CLOSE, OnBtnClose)
	ON_BN_CLICKED(IDC_BTN_SAVE, OnBtnSave)
	ON_BN_CLICKED(IDC_BTN_DELETE, 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::OnAbout()
{
 CAboutDlg().DoModal();
}

void CUserMaintenanceDlg::FillListboxWithUsers()
{
 int iIndex;
 CDatabase db;
 CUserMasterSet* pUserMasterSet = NULL;
 try
 {
  if (db.Open("Visual C++ 6 Bible"))
  {
   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* 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::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 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::OnOK();
}

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++ 6 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::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++ 6 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;
}

⌨️ 快捷键说明

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