MFC多文档登录注册连接mysql数据库-登陆界面在主界面之前

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_29503183/article/details/81257983

1.新建一个多文档程序BusSim,在资源中新建登录DIALOG,其ID为IDD_LOGIN,其界面如下图所示:
登录界面
用户名编辑框设置其ID为IDC_USERNAME,密码框,即新建编辑框控件,在属性中将Password由false修改为true,设置其ID为IDC_PASSWORD。
属性页面
登录按钮的ID为IDC_LOGIN,注册按钮的ID为:IDC_LOGIN_REGISTER
再新建注册DIALOG,其ID为IDD_REGISTER,其界面如下图所示:
注册界面
2.选择登陆界面给该对话框添加类CLogin
添加类
再给用户名和密码的编辑框添加变量
选中对话框右击鼠标选择新建类向导,类向导中选择成员变量那一栏,然后选择IDC_USERNAME,IDC_PASSWORD分别添加变量value->CString->m_n_Username,value->CString->m_n_Password。
具体添加过程如下图所示:
类向导
添加变量
3.给登录按钮添加响应事件,有两种方法:
a.双击登录按钮即可
b.选中登录按钮右击鼠标选择添加事件处理程序即可
4.登陆界面实现代码:
Login.h

#pragma once
#include "afxwin.h"
#include "ToString.h"

// CLogin 对话框

class CLogin : public CDialogEx
{
    DECLARE_DYNAMIC(CLogin)

public:
    CLogin(CWnd* pParent = NULL);   // 标准构造函数
    virtual ~CLogin();

// 对话框数据
    enum { IDD = IDD_LOGIN };

protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

    DECLARE_MESSAGE_MAP()
public:
    ToString tostr;
    CEdit m_username;
    CString m_n_Username;
    CEdit m_n_password;
    CString m_n_Password;
    afx_msg void OnBnClickedLogin();
    afx_msg void OnBnClickedLoginRegister();
    afx_msg void OnBnClickedCancel();
    afx_msg void OnBnClickedBtnModify();
};

Login.cpp : 实现文件

//

#include "stdafx.h"
#include "BusSim.h"
#include "Login.h"
#include "Register.h"
#include "afxdialogex.h"
#include "Mysql.h"
#include "ToString.h"//自己设置的字符串转换类
#include "SetDefine.h"




// CLogin 对话框

IMPLEMENT_DYNAMIC(CLogin, CDialogEx)

CLogin::CLogin(CWnd* pParent /*=NULL*/)
    : CDialogEx(CLogin::IDD, pParent)
    , m_n_Username(_T(""))
    , m_n_Password(_T(""))
{

}

CLogin::~CLogin()
{
}

void CLogin::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_USERNAME, m_username);
    DDX_Text(pDX, IDC_USERNAME, m_n_Username);
    DDX_Control(pDX, IDC_PASSWORD, m_n_password);
    DDX_Text(pDX, IDC_PASSWORD, m_n_Password);
}


BEGIN_MESSAGE_MAP(CLogin, CDialogEx)
    ON_BN_CLICKED(IDC_LOGIN, &CLogin::OnBnClickedLogin)
    ON_BN_CLICKED(IDC_LOGIN_REGISTER, &CLogin::OnBnClickedLoginRegister)
    ON_BN_CLICKED(IDCANCEL, &CLogin::OnBnClickedCancel)
    ON_BN_CLICKED(IDC_BTN_MODIFY, &CLogin::OnBnClickedBtnModify)
END_MESSAGE_MAP()

// CLogin 消息处理程序

void CLogin::OnBnClickedLogin()
{
    // TODO: ÔÚ´ËÌí¼Ó¿Ø¼þ֪ͨ´¦Àí³ÌÐò´úÂë
    UpdateData();
    GetDlgItem(IDC_USERNAME)->GetWindowText(m_n_Username);
    GetDlgItem(IDC_PASSWORD)->GetWindowText(m_n_Password);

    if(m_n_Username.IsEmpty()||m_n_Password.IsEmpty())
    {
        MessageBox(_T("用户名或密码不能为空!"),_T("用户登录信息"));
        //m_n_Username.Empty();   //清空编辑框

       // m_n_Password.Empty();   //清空编辑框

        UpdateData(FALSE);
        return;
    }

    char *sql = tostr.tostring(m_n_Username);//字符串类型转换

    char  sqlstr[1005] = "select Password from tb_register where user_name =\"";
    strcat(sqlstr,sql);
    strcat(sqlstr,"\"");
    Mysql *vsmysql = new Mysql();
    char *Msg=""; char *Msg1 = "";
    vsmysql->Connmysql(HOST,PORT,DBNAME,USER,PASSWD,CHARSET,Msg);
    string res = vsmysql->SelectData(sqlstr,1,Msg1);
    int len1 = res.length();
    std::string STDStr( CW2A( m_n_Password.GetString() ) );

    if(res.substr(0,len1-2)==STDStr)//判断该用户名对应的密码是否与数据库中的密码一致
    {
        MessageBox(_T("登陆成功!"),_T("提示"));
        CDialog::OnOK();
    }
    else
    {   
        MessageBox(_T("用户名或密码不正确!"),_T("提示"),MB_OK|MB_ICONWARNING);
    }
    vsmysql->CloseMysqlconn();
    delete(vsmysql);

}


void CLogin::OnBnClickedLoginRegister()
{
    // TODO: ÔÚ´ËÌí¼Ó¿Ø¼þ֪ͨ´¦Àí³ÌÐò´úÂë
    CRegister dlg;
    dlg.DoModal();
}





void CLogin::OnBnClickedCancel()
{
    // TODO: ÔÚ´ËÌí¼Ó¿Ø¼þ֪ͨ´¦Àí³ÌÐò´úÂë
    CDialogEx::OnCancel();
}

ToString.h

#pragma once
#include<string>
using namespace std;
class ToString
{
public:
    ToString(void);
    ~ToString(void);
    char * tostring(CString cableid);
    wchar_t * toLPCT(string str);
    void split(CString boss);
    void clear();
};

//字符串转换实现代码
ToString.cpp

#include "StdAfx.h"
#include "ToString.h"
#include <string>
using namespace std;


ToString::ToString(void)
{
}


ToString::~ToString(void)
{
}

char * ToString::tostring(CString cableid)
{
    int len =WideCharToMultiByte(CP_ACP,0,cableid,-1,NULL,0,NULL,NULL);  
    char *sql = new char[len +1];  
    WideCharToMultiByte(CP_ACP,0,cableid,-1,sql,len,NULL,NULL );
    return sql;
}

wchar_t * ToString::toLPCT(string str)
{
    size_t size = str.length();
    wchar_t *buffer = new wchar_t[size + 1];
    MultiByteToWideChar(CP_ACP, 0, str.c_str(), size, buffer, size * sizeof(wchar_t));
    buffer[size] = '\0';
    return buffer;
}


void ToString::split(CString boss)
{


}


void ToString::clear()
{

}

5.注册界面的新建类与添加变量的过程与登录界面的过程一致,请参考2,3的过程

第一次制作注册界面,如有不对或者有更为简洁方便的方法,欢迎大家指正。

注册界面主要实现代码为:
Register.h

#pragma once
#include "afxwin.h"
#include "ToString.h"

// CRegister 对话框

class CRegister : public CDialogEx
{
    DECLARE_DYNAMIC(CRegister)

public:
    CRegister(CWnd* pParent = NULL);   // 标准构造函数
    virtual ~CRegister();

// 对话框数据
    enum { IDD = IDD_REGISTER };

protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

    DECLARE_MESSAGE_MAP()
public:
    ToString tostr;
    CEdit m_n_register_username;
    CEdit m_n_register_password;
    CEdit m_n_register_confirm;
    CEdit m_n_register_name;
    CEdit m_n_register_department;
    CEdit m_n_register_company;
    CEdit m_n_register_phonenumber;
    CEdit m_n_register_email;
    CString m_n_registerusername;
    CString m_n_registerpassword;
    CString m_n_registerconfirm;
    CString m_n_registername;
    CString m_n_registerdepartment;
    CString m_n_registercompany;
    CString m_n_registerphone;
    CString m_n_registeremail;
    afx_msg void OnBnClickedRegister();
    void save();
    CString id;
};

Register.cpp : 实现文件

// Register.cpp : 实现文件
//

#include "stdafx.h"
#include "BusSim.h"
#include "Register.h"
#include "afxdialogex.h"
#include "Mysql.h"
#include "ToString.h"
#include"SetDefine.h"
#include "Login.h"
#include <string>
#include <regex>

// CRegister 对话框

IMPLEMENT_DYNAMIC(CRegister, CDialogEx)

CRegister::CRegister(CWnd* pParent /*=NULL*/)
    : CDialogEx(CRegister::IDD, pParent)
    , m_n_registerusername(_T(""))
    , m_n_registerpassword(_T(""))
    , m_n_registerconfirm(_T(""))
    , m_n_registername(_T(""))
    , m_n_registerdepartment(_T(""))
    , m_n_registercompany(_T(""))
    , m_n_registerphone(_T(""))
    , m_n_registeremail(_T(""))
    , id(_T(""))
{

}

CRegister::~CRegister()
{
}

void CRegister::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_REGISTER_USERNAME, m_n_register_username);
    DDX_Control(pDX, IDC_REGISTER_PASSWORD, m_n_register_password);
    DDX_Control(pDX, IDC_REGISTER_CONFIRM, m_n_register_confirm);
    DDX_Control(pDX, IDC_REGISTER_NAME, m_n_register_name);
    DDX_Control(pDX, IDC_REGISTER_DEPARTMENT, m_n_register_department);
    DDX_Control(pDX, IDC_REGISTER_COMPANY, m_n_register_company);
    DDX_Control(pDX, IDC_REGISTER_PHONENUMBER, m_n_register_phonenumber);
    DDX_Control(pDX, IDC_REGISTER_EMAIL, m_n_register_email);
    DDX_Text(pDX, IDC_REGISTER_USERNAME, m_n_registerusername);
    DDX_Text(pDX, IDC_REGISTER_PASSWORD, m_n_registerpassword);
    DDX_Text(pDX, IDC_REGISTER_CONFIRM, m_n_registerconfirm);
    DDX_Text(pDX, IDC_REGISTER_NAME, m_n_registername);
    DDX_Text(pDX, IDC_REGISTER_DEPARTMENT, m_n_registerdepartment);
    DDX_Text(pDX, IDC_REGISTER_COMPANY, m_n_registercompany);
    DDX_Text(pDX, IDC_REGISTER_PHONENUMBER, m_n_registerphone);
    DDX_Text(pDX, IDC_REGISTER_EMAIL, m_n_registeremail);
    DDX_Text(pDX, IDC_EDIT1, id);
}


BEGIN_MESSAGE_MAP(CRegister, CDialogEx)
    ON_BN_CLICKED(IDC_REGISTER, &CRegister::OnBnClickedRegister)
END_MESSAGE_MAP()


// CRegister 消息处理程序


void CRegister::OnBnClickedRegister()
{
    // TODO: ÔÚ´ËÌí¼Ó¿Ø¼þ֪ͨ´¦Àí³ÌÐò´úÂë
    save();
    ToString * tos = new ToString();
    char sqlstr[1005];//submit data to database
    char *sql[205];//string pointer array
    sql[0] = "insert into tb_register(id,user_name,Password,recertain,name,department,company,phonenumber,Email) values(\"";
    sql[1] = tos->tostring(id);
    delete(tos);
    sql[2] = "\",\"";
    int len =WideCharToMultiByte(CP_ACP,0,m_n_registerusername,-1,NULL,0,NULL,NULL);  
    sql[3] =new char[len +1];  
    WideCharToMultiByte(CP_ACP,0,m_n_registerusername,-1,sql[3],len,NULL,NULL );
    sql[4] = "\",\"";
    len =WideCharToMultiByte(CP_ACP,0,m_n_registerpassword,-1,NULL,0,NULL,NULL);
    sql[5] = new char[len+1];
    WideCharToMultiByte(CP_ACP,0,m_n_registerpassword,-1,sql[5],len,NULL,NULL );
    sql[6] = "\",\"";
    len =WideCharToMultiByte(CP_ACP,0,m_n_registerconfirm,-1,NULL,0,NULL,NULL);
    sql[7] = new char[len+1];
    WideCharToMultiByte(CP_ACP,0,m_n_registerconfirm,-1,sql[7],len,NULL,NULL );
    sql[8] = "\",\"";
    len =WideCharToMultiByte(CP_ACP,0,m_n_registername,-1,NULL,0,NULL,NULL);
    sql[9] = new char[len+1];
    WideCharToMultiByte(CP_ACP,0,m_n_registername,-1,sql[9],len,NULL,NULL );
    sql[10] = "\",\"";
    len =WideCharToMultiByte(CP_ACP,0,m_n_registerdepartment,-1,NULL,0,NULL,NULL);
    sql[11] = new char[len+1];
    WideCharToMultiByte(CP_ACP,0,m_n_registerdepartment,-1,sql[11],len,NULL,NULL );
    sql[12] = "\",\"";
    len =WideCharToMultiByte(CP_ACP,0,m_n_registercompany,-1,NULL,0,NULL,NULL);
    sql[13] = new char[len+1];
    WideCharToMultiByte(CP_ACP,0,m_n_registercompany,-1,sql[13],len,NULL,NULL );
    sql[14] = "\",\"";
    len =WideCharToMultiByte(CP_ACP,0,m_n_registerphone,-1,NULL,0,NULL,NULL);
    sql[15] = new char[len+1];
    WideCharToMultiByte(CP_ACP,0,m_n_registerphone,-1,sql[15],len,NULL,NULL );
    sql[16] = "\",\"";
    len =WideCharToMultiByte(CP_ACP,0,m_n_registeremail,-1,NULL,0,NULL,NULL);
    sql[17] = new char[len+1];
    WideCharToMultiByte(CP_ACP,0,m_n_registeremail,-1,sql[17],len,NULL,NULL );
    sql[18] = "\")";
    strcpy(sqlstr,sql[0]);
    for(int i=1;i<19;i++)
    {
        strcat(sqlstr,sql[i]);
    }


    /******************判断用户名字母开头,允许5-16位,允许字母数字下划线*******************/
    regex repPattern3("^[a-zA-Z][a-zA-Z0-9_]{4,15}$",regex_constants::extended);
    // 声明匹配结果变量
    match_results<string::const_iterator> rerResult3;
    // 定义待匹配的字符串
    std::string STDStr3( CW2A( m_n_registerusername.GetString() ) );
    // 进行匹配
    bool bValid3 = regex_match(STDStr3, rerResult3, repPattern3);

    /******************判断密码密码必须由6-16个英文字母和数字的字符串组成*******************/
    regex repPattern2("^[A-Za-z]+[0-9]+[A-Za-z0-9]*|[0-9]+[A-Za-z]+[A-Za-z0-9]*$",regex_constants::extended);
    // 声明匹配结果变量
    match_results<string::const_iterator> rerResult2;
    // 定义待匹配的字符串
    std::string STDStr2( CW2A( m_n_registerpassword.GetString() ) );
    // 进行匹配
    bool bValid2 = regex_match(STDStr2, rerResult2, repPattern2);


    /******************判断姓名是否为汉字******************/
    regex repPattern4("^([\xe4-\xe9][\x80-\xbf]{2}){2,4}$",regex_constants::extended);
    // 声明匹配结果变量
    match_results<string::const_iterator> rerResult4;
    // 定义待匹配的字符串
    std::string STDStr4( CW2A( m_n_registername.GetString() ) );
    // 进行匹配
    bool bValid4 = regex_match(STDStr4, rerResult4, repPattern4);

    /******************判断手机号是否为有效及长度*******************/
    regex repPattern1("^[1][358][0-9]{9}$",regex_constants::extended);
    // 声明匹配结果变量
    match_results<string::const_iterator> rerResult;
    // 定义待匹配的字符串
    std::string STDStr1( CW2A( m_n_registerphone.GetString() ) );
    // 进行匹配
    bool bValid1 = regex_match(STDStr1, rerResult, repPattern1);


    /***************判断邮箱格式是否正确*****************/
    // 定义一个正则表达式 , 4~23 位数字和字母的组合
    regex repPattern("([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)",regex_constants::extended);
    // 声明匹配结果变量
    match_results<string::const_iterator> rerResult1;
    // 定义待匹配的字符串
    std::string STDStr( CW2A( m_n_registeremail.GetString() ) );
    // 进行匹配
    bool bValid = regex_match(STDStr, rerResult1, repPattern);


    Mysql *mysql = new Mysql();
    char *Msg = "";  char *Msg1="";
    if(mysql->Connmysql(HOST,PORT,DBNAME,USER,PASSWD,CHARSET,Msg)==1)
    {
        return ;
    }
    if(mysql->InsertData(sqlstr,Msg1)==1)
    {

        MessageBox(_T("注册失败"),_T("提示"),MB_OK|MB_ICONWARNING); 
    }
    else
    {
        Mysql *vsmysql = new Mysql();
        char *Msg1=""; char *Msg2 = "";
        vsmysql->Connmysql(HOST,PORT,DBNAME,USER,PASSWD,CHARSET,Msg);
        char *sql = tostr.tostring(m_n_registerusername);
        char  sqlstr1[1005] = "select user_name from tb_register \"";
        string res = vsmysql->SelectData(sqlstr1,1,Msg2);
        int len1 = res.length();
        std::string STDStr( CW2A( m_n_registerusername.GetString() ) );

        if(res.substr(0,len1-2)==STDStr)
        {
            MessageBox(_T("该用户名已被注册!"),_T("提示"),MB_OK|MB_ICONWARNING);
            m_n_registerusername.Empty();
        }
        else if(m_n_registerpassword!=m_n_registerconfirm)  
        {
            MessageBox(_T("两次输入的密码不一致"),_T("提示"),MB_OK|MB_ICONWARNING);
            m_n_registerpassword.Empty();
            m_n_registerconfirm.Empty();
        }
        else if(!bValid3)
        {
        // 匹配成功
            MessageBox(_T("用户名格式不正确"),_T("提示"),MB_OK|MB_ICONWARNING); 
            m_n_registerusername.Empty();
        }
        else if(!bValid2)
        {
        // 匹配成功
            MessageBox(_T("密码格式不正确"),_T("提示"),MB_OK|MB_ICONWARNING); 
            m_n_registerpassword.Empty();
        }
        else if(!bValid4)
        {
        // 匹配成功
            MessageBox(_T("名字不为汉字"),_T("提示"),MB_OK|MB_ICONWARNING); 
            m_n_registername.Empty();
        }
        else if(!bValid1)
        {
            MessageBox(_T("无效手机号码"),_T("提示"),MB_OK|MB_ICONWARNING); 
            m_n_registerphone.Empty();
        }
        else if (!bValid)
        {
            MessageBox(_T("无效邮箱"),_T("提示"),MB_OK|MB_ICONWARNING); 
            m_n_registeremail.Empty();
        }
        else
        {
            CLogin dlg;
            MessageBox(_T("注册成功"),_T("提示")); 
            CDialog::OnOK();
        }
    }
    mysql->CloseMysqlconn();
    delete(mysql);
}
void CRegister::save()
{
    m_n_register_username.GetWindowTextW(m_n_registerusername);
    m_n_register_password.GetWindowTextW(m_n_registerpassword);
    m_n_register_confirm.GetWindowTextW(m_n_registerconfirm);
    m_n_register_name.GetWindowTextW(m_n_registername);
    m_n_register_department.GetWindowTextW(m_n_registerdepartment);
    m_n_register_company.GetWindowTextW(m_n_registercompany);
    m_n_register_phonenumber.GetWindowTextW(m_n_registerphone);
    m_n_register_email.GetWindowTextW(m_n_registeremail);
}

6.实现登录界面在主界面之前在该程序的MainFrm.cpp文件中,添加#include “Login.h”头文件

在该类的OnCream函数中添加以下代码即可

CLogin dlg;
dlg.DoModal();

登陆界面
希望我自己的总结对大家有用,如有不懂欢迎留言。

展开阅读全文

没有更多推荐了,返回首页