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配置文件,判斷是否記住了密碼

 

好了,大概就是這些吧,希望給有興趣的你提供了幫助,也歡迎大家一起探討!!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM