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

📄 logindlg.cpp

📁 LibraryManageDM.rar 数据库设计图书馆管理系统
💻 CPP
字号:
// LoginDlg.cpp : implementation file
//

#include "stdafx.h"
#include "LibraryManage.h"
#include "LoginDlg.h"
#include "UserSet.h"
#include "ReaderInfoSet.h"
#include "CommonFunc.h"
// CLoginDlg dialog

const int MaxCount = 3;
IMPLEMENT_DYNAMIC(CLoginDlg, CDialog)

CLoginDlg::CLoginDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CLoginDlg::IDD, pParent)
    , strUserName_(_T(""))
    , strPassword_(_T(""))
    , UserType_(OPERATOR)
    , strReaderName_(_T(""))
	, nErrorCount_(0)
{

}

CLoginDlg::~CLoginDlg()
{
}

void CLoginDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLoginDlg)
    DDX_Control(pDX, IDC_COMBO_USERNAME, ctrlUserName_);
    DDX_Control(pDX, IDC_COMBO_USERTYPE, ctrlUserType_);
    DDX_CBString(pDX, IDC_COMBO_USERNAME, strUserName_);
    DDX_Control(pDX, IDC_EDIT_PASSWORD, ctrlPassword_);
    DDX_Text(pDX, IDC_EDIT_PASSWORD, strPassword_);
    DDX_Text(pDX, IDC_LOGINUSERNAME, strReaderName_);
	DDV_MaxChars(pDX, strReaderName_, 20);
    DDX_Control(pDX, IDC_LOGINUSERNAME, ctrlReaderName_);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CLoginDlg, CDialog)
	//{{AFX_MSG_MAP(CLoginDlg)
    ON_BN_CLICKED(IDOK, OnBnClickedOk)
    ON_CBN_SELCHANGE(IDC_COMBO_USERTYPE, OnCbnSelchangeComboUsertype)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


// CLoginDlg message handlers
BOOL CLoginDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    // TODO:  Add extra initialization here
    ctrlUserType_.SetCurSel(0);
    CUserSet rs ;
    CString strSQL = _T("SELECT * FROM UserInfo");
    if (rs.IsOpen())rs.Close();
    if(rs.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL) && rs.GetRecordCount())
    {
        while(!rs.IsEOF())
        {
            ctrlUserName_.AddString(rs.strUserName_);
            rs.MoveNext();
        }
        rs.Close();
        ctrlUserName_.SetCurSel(0);
    }
    HICON hIcon;
    hIcon = theApp.LoadIcon(IDI_ENTERICON);
    SetIcon(hIcon, FALSE);

    UpdateData(FALSE);
    return TRUE;  // return TRUE unless you set the focus to a control
    // EXCEPTION: OCX Property Pages should return FALSE
}

void CLoginDlg::OnCbnSelchangeComboUsertype()
{
    // TODO: Add your control notification handler code here
    UpdateData(TRUE);
    CString strType;
    ctrlUserType_.GetWindowText(strType);
    if (strType.IsEmpty())return;

    strPassword_.Empty();
    if (strType != _T("管理人员"))
    {
        UserType_ = BORROWER;
        ctrlUserName_.ShowWindow(SW_HIDE);
        GetDlgItem(IDC_LOGINUSERNAME)->ShowWindow(SW_NORMAL);
        GetDlgItem(IDC_LOGINNAME)->SetWindowText(_T("姓名"));
        GetDlgItem(IDC_IDPASS)->SetWindowText(_T("编号"));
    }
    else
    {
        ctrlUserName_.SetCurSel(0);
        ctrlUserName_.ShowWindow(SW_NORMAL);
        GetDlgItem(IDC_LOGINUSERNAME)->ShowWindow(SW_HIDE);
        GetDlgItem(IDC_LOGINNAME)->SetWindowText(_T("用户名"));
        GetDlgItem(IDC_IDPASS)->SetWindowText(_T("密码"));
        UserType_ = OPERATOR;
    }
    UpdateData(FALSE);
}

void CLoginDlg::OnBnClickedOk()
{
    // TODO: Add your control notification handler code here
    CUserSet usRS;
    CReaderInfoSet rdRS;
    CString strSQL,strText;
    UpdateData(TRUE);
    if (UserType_ == OPERATOR)
    {
        //检查用户名是否输入
        if(strUserName_.IsEmpty())
        {
            MessageBox(_T("请输入用户名!"), theApp.strSystemName);
            ctrlUserName_.SetFocus();
            return;
        }
        //检查密码是否输入
        if(strPassword_.IsEmpty())
        {
            MessageBox(_T("请输入密码!"), theApp.strSystemName);
            ctrlPassword_.SetFocus();
            return;
        }
        strSQL.Format(_T("SELECT * FROM UserInfo WHERE UserName = '%s' AND Password ='%s'"),
            strUserName_, Encrypt(strPassword_));

        if (usRS.IsOpen())usRS.Close();
        if(!usRS.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
        {
            MessageBox(_T("打开数据库失败!"), _T("数据库错误"), MB_OK);
            return ;
        }	
        if(usRS.GetRecordCount()==0)
        {
            usRS.Close();
            MessageBox(_T("密码错误,请重新输入!"), theApp.strSystemName);
			if (++nErrorCount_ >= MaxCount)
			{
				MessageBox(_T("已经输错超过3次了!"), theApp.strSystemName);
				CDialog::OnCancel();
				return;
			}
            strPassword_.Empty();
            ctrlPassword_.SetFocus();
            UpdateData(FALSE);
        }
        else
        {
            theApp.SetAdmin(usRS.bAdmin_);
            theApp.SetOperator(usRS.strUserName_);
            theApp.SetUserName(usRS.strUserName_);
            theApp.SetUserType(UserType_);
            usRS.Close();
            CDialog::OnOK();
        }
    }
    else
    {
        //检查姓名是否输入
        if(strReaderName_.IsEmpty())
        {
            MessageBox(_T("请输入姓名!"), theApp.strSystemName);
            GetDlgItem(IDC_LOGINUSERNAME)->SetFocus();
            return;
        }
        //检查用户ID是否输入
        if(strPassword_.IsEmpty())
        {
            MessageBox(_T("请输入您的ID!"), theApp.strSystemName);
            ctrlPassword_.SetFocus();
            return;
        }
        strSQL.Format(_T("SELECT * FROM ReaderInfo WHERE ReaderID = '%s' AND ReaderName = '%s'"),
            strPassword_, strReaderName_);

        if (rdRS.IsOpen())rdRS.Close();
        if(!rdRS.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
        {
            MessageBox(_T("打开数据库失败!"), _T("数据库错误"), MB_OK);
            return ;
        }	
        if(rdRS.GetRecordCount()==0)
        {
            rdRS.Close();
            MessageBox(_T("用户名或者ID错误,请重新输入!"), theApp.strSystemName);
            if (++nErrorCount_ >= MaxCount)
			{
				MessageBox(_T("已经输错超过3次了!"), theApp.strSystemName);
				CDialog::OnCancel();
				return;
			}
            strReaderName_.Empty();
            strPassword_.Empty();
            ctrlReaderName_.SetFocus();
            UpdateData(FALSE);
        }
        else
        {
            theApp.SetAdmin(FALSE);
            theApp.SetOperator(strPassword_);
            theApp.SetUserName(strReaderName_);
            theApp.SetUserType(UserType_);
            rdRS.Close();
            CDialog::OnOK();
        }
    }
}

⌨️ 快捷键说明

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