C# 实现简单仿QQ登陆注册功能
闲来没事,想做一个仿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# 实现简单仿QQ登陆注册功能的更多相关文章
- Android Studio实现登陆注册功能之手机号验证
我们平常写的登陆注册功能,就是很普通的注册一个账号,设置密码,然后登录.这次,想写一个与之前稍微不一样的登陆注册界面,于是想到了手机号验证的方式. 现在我们市面上出现的很多app,都是采用的手机号注册 ...
- JS简单仿QQ聊天工具的制作
刚接触JS,对其充满了好奇,利用刚学到的一点知识,写了一个简单的仿QQ聊天的东西,其中还有很多的不足之处,有待慢慢提高. 功能:1.在输入框中输入内容,点击发送,即可在上方显示所输入内容. 2.点击‘ ...
- Android学习之仿QQ側滑功能的实现
如今项目越来越多的应用了滑动删除的功能,Android本来遵循的是长按删除,IOS定制的是滑动删除,不可否认滑动删除确实在客户体验上要好一点,所以看了非常多关于仿QQ滑动删除的样例,还是感觉代码家的A ...
- SpringBoot写一个登陆注册功能,和期间走的坑
文章目录 前言 1. 首先介绍项目的相关技术和工具: 2. 首先创建项目 3. 项目的结构 3.1实体类: 3.2 Mapper.xml 3.3 mapper.inteface 3.4 Service ...
- Apicloud_(模板)登陆注册功能模板
项目已托管到Github上 传送门 不需要使用任何图片资源,需要用到SHA1.js库文件, Apicloud_(接口验证)用户注册头部信息X-APICloud-AppKey生成 传送门 项目全代码放到 ...
- web_01Java ee实现登陆注册功能
Web Web_01版本: 实现功能 用户注册 用户登录 设计内容 数据库:mysql 服务器: tomact7 配置 : xml 页面 : jsp+html/css *重点: 数据库相关: 数据库操 ...
- Python学习笔记_02:使用Tkinter连接MySQL数据库实现登陆注册功能
1 环境搭建 1.1 Python安装 1.2 MySQL环境搭建 1.3安装MySQLdb 2 具体实现 2.1 登陆界面 2.2 注册界面 2.3 具体实现部分代码 1 环境搭建 1.1 P ...
- 安卓listView实现下拉刷新上拉加载滑动仿QQ的删除功能
大家对这些功能都是看的多了,然后对上拉刷新和下拉加载的原理都是非常清楚的,所以实现这功能其实也就是为了让大家能够从众多的同行们来进行比较学习而已,虽然即使是这样,但是面试的时候面试官还是会问你上拉和下 ...
- jsp+servlet+mysql简单实现用户登陆注册
原码,项目中遇到的错误,解决方法,文章最后有链接可以获取 项目简介 *有的网友说在修改和删除时会触发error,建议各位不要去把用户名命名为中文! 功能描述 登陆,注册,用户一览表,修改,删除,添加, ...
随机推荐
- Python之路番外(第二篇):PYTHON基本数据类型和小知识点
一.基础小知识点 1.如果一行代码过长,可以用续行符 \换行书写 例子 if (signal == "red") and \ (car == "moving") ...
- PHP TP 生成二维码
vendor('phpqrcode.phpqrcode'); $value = "http://www.baidu.com";//二维码内容 $errorCorrectionLev ...
- PHP5.3的编译扩展
./configure --prefix=/usr/local/php --enable-fastcgi --enable-zip --enable-fpm --enable-gd-native-tt ...
- vue动态路由配置,vue路由传参
动态路由: 当我们很多个页面或者组件都要被很多次重复利用的时候,我们的路由都指向同一个组件,这时候从不同组件进入一个"共用"的组件,并且还要传参数,渲染不同的数据 这就要用到动态路 ...
- fastcgi协议解析(nginx)
请求NGINX ->[ {(post data) +> (NGX_HTTP_FASTCGI_STDIN)} * N +> {(environment variables) +> ...
- CSS学习笔记:盒子模型
盒子模型(CSS basic box model):When laying out a document, the browser's rendering engine represents each ...
- linux和普通文本的换行问题
情景一: 普通文本 vim操作换行 :%s#xxx#\n#g 情景二: linux环境换行 vim :%s#xxx#\r#g
- Python常见错误:IndexError: list index out of range
用python写脚本查询字典时,在遍历字典时循环到某一项时老是报错 出现这种错误有两种情况: 第1种可能情况 list[index]index超出范围 第2种可能情况 list是空值就会出现 In ...
- swift-基础语法2
一.整形 :有符号和无符号类型 有符号类型:Int ,Int8 ,Int32,Int64 无符号类型: UInt ,UInt8 UInt32,UInt64 注意点:如果你的开发环境是32位,那么Int ...
- 2018.12.31 NOIP训练 偶数个5(简单数论)
传送门 对于出题人zxyoizxyoizxyoi先%\%%为敬题目需要龟速乘差评. 题意简述:5e55e55e5组数据,给出n,请你求出所有n位数中有偶数个5的有多少,n≤1e18n\le1e18n≤ ...