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,建议各位不要去把用户名命名为中文! 功能描述 登陆,注册,用户一览表,修改,删除,添加, ...
随机推荐
- 2019,UI设计师必备神器
2019年将会是你全新起航的一年,相信你已经制定了很多规划,正在开启第一步的推动. 作为对UI设计师更大程度的支持,今天特意为你分享一款释放你双手的设计神器.让你可以把时间和精力投入到设计本身,这 ...
- Sophus libSophus.so
在编译包含Sophus的源文件的时候,出现如下错误 ../lib/libmyslam.so: undefined reference to `Sophus::SO3::SO3(double, doub ...
- STL基础1:vector
#include <iostream> #include <vector> #include <algorithm> #include <numeric> ...
- openssl pem密钥文件rsa加密解密例子
准备工作 命令行加密解密,用与比对代码中的算法和命令行的算法是否一致 C:\openssl_test>openssl rsautl -encrypt -in data.txt -inkey pu ...
- JavaScript的基础篇
一.JavaScript的引入方式 1)js的引用方式 <body> <!--引入方式一,直接编写--> <script> alert("hello wo ...
- 使用kbmmw smart service 属性时的一个注意事项
kbmmw 5.0 以后支持smart service, 这个用起来非常方便,kbmmw 通过 定制属性来简化编程,可以参考我以前的文章.但是这个意味着使用单元引用一定要小心, 否则出了问题,都不知道 ...
- 2018.06.26「TJOI2018」数学计算(线段树)
描述 小豆现在有一个数 xxx ,初始值为 111 . 小豆有 QQQ 次操作,操作有两种类型: 111 $ m$ : x=x×mx=x×mx=x×m ,输出 xxx modmodmod MMM : ...
- i2c_client的生成
网上很多文档都是介绍源码,包括i2c_client结构体的源码都有贴出,看上去似乎需要手动写该结构体,但实际上,i2c_client的生成是用如下方法. \arch\arm\mach-omap2/bo ...
- 学以致用十三-----Centos7.2+python3+YouCompleteMe成功历程
历经几天的摸索,趟过几趟坑之后,终于完成YouCompleteMe的安装配置. 今天同样是个不能忘记的日子,国耻日,勿忘国耻.(9.18) 服务器安装好,基本配置配置好后,开始安装. ======== ...
- 1.gil全局解释器锁, 2. 死锁与递归锁 3. 信号量 4. Event事件 5. 线程queue
gil本质就是一把互斥锁,相当于执行权限,每个进程都会存在一把gil,同一进程内的多个线程必须抢到gil 之后才能使用cpython解释器来执行自己的代码,同一进程下的多线程不能并行,但可以实现并发 ...