📄 logindlg.cpp
字号:
// LoginDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Login.h"
#include "LoginDlg.h"
#include "UserData.h"
#include "MD5.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CLoginDlg dialog
CLoginDlg::CLoginDlg(CWnd* pParent /*=NULL*/)
: CDialog(CLoginDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CLoginDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CLoginDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CLoginDlg)
DDX_Control(pDX, IDC_UserName, m_UserName);
DDX_Control(pDX, IDC_Password, m_Password);
//}}AFX_DATA_MAP
//控制用户名最多输入20个字符
m_UserName.SetLimitText(20);
//控制用户密码最多输入40个字符
m_Password.SetLimitText(40);
}
BEGIN_MESSAGE_MAP(CLoginDlg, CDialog)
//{{AFX_MSG_MAP(CLoginDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CLOSE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLoginDlg message handlers
BOOL CLoginDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
//初始化m_uersdata
m_userdata = new CUserData;
//初始化登录次数
Count=1;
return TRUE; // return TRUE unless you set the focus to a control
}
// 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 CLoginDlg::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 CLoginDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CLoginDlg::OnOK()
{
// TODO: Add extra validation here
CString Password,strPassword;
CString UserName,StrTemp;
UpdateData(TRUE);
//实例化加密解密算法类型
CMD5* MyEncrypt = new CMD5;
//获取用户输入的用户名
m_UserName.GetWindowText(StrTemp);
//加密用户名
UserName=MyEncrypt->MD5_Algorithm(StrTemp);
//调用GetPassword函数判断数据库中是否有用户输入的用户名
//如果有则返回与用户名相对应的用户密码
if(m_userdata->GetPassword(&UserName,&strPassword))
{
//获取用户输入的用户密码
m_Password.GetWindowText(StrTemp);
//加密用户密码
Password=(CString)MyEncrypt->MD5_Algorithm(StrTemp);
//和数据库中密码比较,得到登录密码
if(Password.CompareNoCase(strPassword)==0)
{
MessageBox("登录系统成功","系统登录",MB_OK|MB_ICONWARNING);
CDialog::OnOK();
UpdateData(FALSE);
}
else
{
//如果登录次数小于3,程序关闭
if (Count<3)
{
MessageBox("密码不正确","系统登录错误",MB_OK|MB_ICONWARNING);
Count++; //登录次数记录
UpdateData(FALSE);
}
else
{
MessageBox("登录次数过多,系统关闭","系统登录错误",MB_OK|MB_ICONWARNING);
UpdateData(FALSE);
CDialog::OnOK();
}
}
}
else
{
//如果登录次数小于3,程序关闭
if (Count<3)
{
MessageBox("非法用户名","系统登录错误",MB_OK|MB_ICONWARNING);
Count++; //登录次数记录
UpdateData(FALSE);
}
else
{
MessageBox("登录次数过多,系统关闭","系统登录错误",MB_OK|MB_ICONWARNING);
UpdateData(FALSE);
CDialog::OnOK();
}
}
}
void CLoginDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
//释放 m_userdata
delete m_userdata;
CDialog::OnClose();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -