C# 实现简单仿QQ登陆注册功能
程序员文章站
2022-12-25 20:31:15
闲来没事,想做一个仿QQ登陆注册的winform,于是利用工作之余,根据自己的掌握和查阅的资料,历时4天修改完成,新手水平,希望和大家共同学习进步,有不同见解希望提出! 废话不多说,进入正题: 先来看看我绘制的界面: 运用的CSkin控件完成的绘制,cskin和vs自带的控件其实差别不大,只是csk ......
闲来没事,想做一个仿QQ登陆注册的winform,于是利用工作之余,根据自己的掌握和查阅的资料,历时4天修改完成,新手水平,希望和大家共同学习进步,有不同见解希望提出!
废话不多说,进入正题:
先来看看我绘制的界面:
运用的CSkin控件完成的绘制,cskin和vs自带的控件其实差别不大,只是cskin美化更好一点,此外,cskin的验证码控件(skincode)很不错
再来看看代码:
public partial class Login : CCSkinMain { public Login() { InitializeComponent(); //ControlBox = false; //取消最大化 MaximizeBox = false; panel1.Visible = false; txtName.SkinTxt.TextChanged += SkinTxt_TextChanged; connectString = @"Data Source=E:\Works\Visual Studio 2017\Projects\SuiBianWanWan\SuiBianWanWan\bin\Debug\suibianwanwan.db;Pooling=true;FailIfMissing=false"; conn = new SQLiteConnection(connectString); conn.Open(); } private void SkinTxt_TextChanged(object sender, EventArgs e) { txtPassWord.Text = ""; skinCheckBox1.Checked = false; skinCheckBox2.Checked = false; } string connectString = null; SQLiteConnection conn = null; //获取Configuration对象 //这里得到的是exe.config文件的内容,不是app.config Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath); string name = ""; string passWord = ""; string sign1 = ""; string sign2 = ""; string pic = ""; //根据进行的设置更新config保存的数据 public void AccessAppSetting(string name,string passsWord,string sign1,string sign2,string pic) { //删除<add>元素 config.AppSettings.Settings.Remove("name"); config.AppSettings.Settings.Remove("passWord"); config.AppSettings.Settings.Remove("sign1"); config.AppSettings.Settings.Remove("sign2"); config.AppSettings.Settings.Remove("pic"); //增加<add>元素 config.AppSettings.Settings.Add("name", name); config.AppSettings.Settings.Add("passWord", passsWord); config.AppSettings.Settings.Add("sign1",sign1); config.AppSettings.Settings.Add("sign2", sign2); config.AppSettings.Settings.Add("pic", pic); //一定要记得保存,写不带参数的config.save()也可以 config.Save(ConfigurationSaveMode.Modified); //刷新,否则程序读取的还是之前的值(可能已经装进内存) ConfigurationManager.RefreshSection("appSettings"); } //密码加密 public string getMD5(string s) { MD5 mD5 = MD5.Create(); byte[] buffer = Encoding.GetEncoding("gbk").GetBytes(s); byte[] Md5Buffer = mD5.ComputeHash(buffer); string str = ""; for (int i = 0; i < Md5Buffer.Length; i++) { str = str + Md5Buffer[i].ToString(); } return str; } private void Form1_Load(object sender, EventArgs e) { //重绘头像框 变圆形 picturebox GraphicsPath gp = new GraphicsPath(); gp.AddEllipse(skinPictureBox1.ClientRectangle); Region region = new Region(gp); skinPictureBox1.Region = region; gp.Dispose(); region.Dispose(); name = config.AppSettings.Settings["name"].Value; passWord = config.AppSettings.Settings["passWord"].Value; sign1 = config.AppSettings.Settings["sign1"].Value; sign2 = config.AppSettings.Settings["sign2"].Value; pic = config.AppSettings.Settings["pic"].Value; if (!string.IsNullOrEmpty(name)) { txtName.Text = name; txtPassWord.Text = passWord; skinPictureBox1.ImageLocation = pic; skinCheckBox1.Checked = sign1.Trim() == "1" ? true : false; skinCheckBox2.Checked = sign2.Trim() == "2" ? true : false; } if (skinCheckBox1.Checked) btnLogin_Click(null,null); } private void btnLogin_Click(object sender, EventArgs e) { //登录验证 SQLiteCommand cmd = new SQLiteCommand(); cmd.Connection = conn; cmd.CommandText = "select * from userMessage where username =" + txtName.Text; try { SQLiteDataReader dr = cmd.ExecuteReader(); DataTable dt = new DataTable(); dt.Load(dr); string username = dt.Rows[0]["username"].ToString(); string password = dt.Rows[0]["password"].ToString(); if (string.IsNullOrEmpty(txtName.Text)) { skinLabel6.Visible = true; } else if (string.IsNullOrEmpty(txtPassWord.Text)) { skinLabel7.Visible = true; } else { if (skinCheckBox2.Checked && sign2 == "") { if (getMD5(txtPassWord.Text) == password) { MessageBoxEx.Show("登陆成功"); if(skinCheckBox1.Checked) AccessAppSetting(txtName.Text, getMD5(txtPassWord.Text), "1", "2", skinPictureBox1.ImageLocation); else AccessAppSetting(txtName.Text, getMD5(txtPassWord.Text), "", "2", skinPictureBox1.ImageLocation); } else MessageBoxEx.Show("用户名或密码错误,请重新登陆"); } else if (!skinCheckBox2.Checked && sign2 == "") { if(getMD5(txtPassWord.Text) == password) { MessageBoxEx.Show("登陆成功"); AccessAppSetting(txtName.Text, "", "", "", skinPictureBox1.ImageLocation); } else MessageBoxEx.Show("用户名或密码错误,请重新登陆"); } else if(sign2 != "" && skinCheckBox2.Checked) { if(txtName.Text == name && txtPassWord.Text == password) { MessageBoxEx.Show("登陆成功"); if(skinCheckBox1.Checked) AccessAppSetting(txtName.Text, txtPassWord.Text, "1", "2", skinPictureBox1.ImageLocation); else AccessAppSetting(txtName.Text, txtPassWord.Text, "", "2", skinPictureBox1.ImageLocation); } else if(txtName.Text != name && getMD5(txtPassWord.Text) == password) { MessageBoxEx.Show("登陆成功"); if(skinCheckBox1.Checked) AccessAppSetting(txtName.Text, getMD5(txtPassWord.Text), "1", "2", skinPictureBox1.ImageLocation); else AccessAppSetting(txtName.Text, getMD5(txtPassWord.Text), "", "2", skinPictureBox1.ImageLocation); } else MessageBoxEx.Show("用户名或密码错误,请重新登陆"); } else if (sign2 != "" && !skinCheckBox2.Checked) { if (txtName.Text == name && txtPassWord.Text == password) { MessageBoxEx.Show("登陆成功"); AccessAppSetting(txtName.Text, "", "", "", skinPictureBox1.ImageLocation); } else if (txtName.Text != name && getMD5(txtPassWord.Text) == password) { MessageBoxEx.Show("登陆成功"); AccessAppSetting(txtName.Text, "", "", "2", skinPictureBox1.ImageLocation); } else MessageBoxEx.Show("用户名或密码错误,请重新登陆"); } } } catch (Exception) { MessageBoxEx.Show("用户名不存在,请前往注册"); txtName.Text = ""; txtPassWord.Text = ""; skinCheckBox1.Checked = false; skinCheckBox2.Checked = false; } } //关于焦点的一些处理 private void Form1_Click(object sender, EventArgs e) { panel1.Visible = false; if (!string.IsNullOrEmpty(txtName.Text)) { skinLabel6.Visible = false; } if (!string.IsNullOrEmpty(txtPassWord.Text)) { skinLabel7.Visible = false; } } private void txtPassWord_MouseEnter(object sender, EventArgs e) { if (!string.IsNullOrEmpty(txtName.Text)) { skinLabel6.Visible = false; } } private void txtName_Validated(object sender, EventArgs e) { if (!string.IsNullOrEmpty(txtName.Text)) { skinLabel6.Visible = false; } } private void txtPassWord_Validated(object sender, EventArgs e) { if (!string.IsNullOrEmpty(txtPassWord.Text)) { skinLabel7.Visible = false; } } //自动登录 private void skinCheckBox1_CheckedChanged(object sender, EventArgs e) { if (skinCheckBox1.Checked==true) { skinCheckBox2.Checked = true; } } //在线状态下拉实现 private void btnState_Click(object sender, EventArgs e) { panel1.Visible = true; } private void ToolStripMenuItem0_Click(object sender, EventArgs e) { btnState.BaseColor = Color.Green; panel1.Visible = false; } private void toolStripMenuItem1_Click(object sender, EventArgs e) { btnState.BaseColor = Color.Red; panel1.Visible = false; } private void toolStripMenuItem2_Click(object sender, EventArgs e) { btnState.BaseColor = Color.Gray; panel1.Visible = false; } //换头像 private void skinPictureBox1_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = "选择头像"; ofd.Multiselect = false; ofd.InitialDirectory = @"E:\"; ofd.Filter = "图片|*.jpg"; ofd.ShowDialog(); string path = ofd.FileName; skinPictureBox1.ImageLocation = path; } //注册页面 private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { Regist regist = new Regist(); Hide(); regist.Show(); } private void Login_FormClosed(object sender, FormClosedEventArgs e) { Application.Exit(); } }
public partial class Regist : CCSkinMain { public Regist() { InitializeComponent(); MaximizeBox = false; connectString = @"Data Source=E:\Works\Visual Studio 2017\Projects\SuiBianWanWan\SuiBianWanWan\bin\Debug\suibianwanwan.db;Pooling=true;FailIfMissing=false"; conn = new SQLiteConnection(connectString); conn.Open(); } string connectString = null; SQLiteConnection conn = null; //密码加密 public string getMD5(string s) { MD5 mD5 = MD5.Create(); byte[] buffer = Encoding.GetEncoding("gbk").GetBytes(s); byte[] Md5Buffer = mD5.ComputeHash(buffer); string str = ""; for (int i = 0; i < Md5Buffer.Length; i++) { str = str + Md5Buffer[i].ToString(); } return str; } private void btnRegist_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtName.Text)) { skinLabel6.Visible = true; } if (string.IsNullOrEmpty(txtPassWord.Text)) { skinLabel4.Visible = true; } string skinCode = skinCode1.CodeStr; if (txtCheck.Text == skinCode) { MessageBoxEx.Show("恭喜你注册成功!"); Hide(); Login login = new Login(); login.Show(); } else { MessageBoxEx.Show("验证码错误,请重新输入"); } try { SQLiteCommand cmd = new SQLiteCommand(); cmd.Connection = conn; string password = getMD5(txtPassWord.Text); cmd.CommandText = "insert into userMessage values ('" + txtName.Text + "','" + password + "','" + txtPhone.Text + "')"; cmd.ExecuteNonQuery(); } catch (Exception) { MessageBoxEx.Show("用户名已存在"); } } private void txtName_Validated(object sender, EventArgs e) { if(!string.IsNullOrEmpty(txtName.Text)) skinLabel6.Visible = false; else skinLabel6.Visible = true; } private void txtPassWord_Validated(object sender, EventArgs e) { if (!string.IsNullOrEmpty(txtPassWord.Text)) skinLabel4.Visible = false; else skinLabel4.Visible = true; } private void txtPhone_Validated(object sender, EventArgs e) { if (!string.IsNullOrEmpty(txtPhone.Text)) skinLabel5.Visible = false; else skinLabel5.Visible = true; } private void Regist_FormClosed(object sender, FormClosedEventArgs e) { Login login = new Login(); login.Show(); } }
这里说一下数据库我用的SQLite,在此之前我也没有用过sqlite数据库,只知道是文件型数据库,我也是边学边用,发现其实挺好用的,十分方便,我用数据库可视化工具是SQLite Expert Personal,这里提一下,用sqlite数据库进行建表时,针对字符串类型最好用text类型,不要用varchar
最后,说一下我记住密码的方式,我用的是利用App.config 配置文件保存密码的方式来记录的,在winform加载的时候去读取config配置文件,判断是否记住了密码
好了,大概就是这些吧,希望给有兴趣的你提供了帮助,也欢迎大家一起探讨!!
推荐阅读
-
C#仿密保卡功能的简单实现代码
-
C#仿密保卡功能的简单实现代码
-
C# 实现简单仿QQ登陆注册功能
-
jsp+java servlet实现简单用户登录和注册页面(连接数据库,登录页面包含验证码,两周内免登陆等功能)
-
JSP+JDBC+Servlet--实现简单登陆注册功能
-
python的简单的登陆和注册功能实现
-
python的简单的登陆和注册功能实现
-
C# 实现简单仿QQ登陆注册功能
-
基于MVC架构的简单javaweb登陆注册功能实现(jsp + servlet + jdbc + mysql)
-
jsp+java servlet实现简单用户登录和注册页面(连接数据库,登录页面包含验证码,两周内免登陆等功能)