一个C#的登录程序windows窗体应用(详细注释版)

  @全体成员:提供据体图布局和源代码供大家参考,如有不理解的地方可以下载压缩包体验。
  个人原创,仅供参考。
  压缩包地址:https://download.csdn.net/download/ganyonjie/11431098 欢迎下载

具体布局如下:

一个C#的登录程序windows窗体应用(详细注释版)_第1张图片

源代码如下:

using System;
using System.IO;
using System.Windows.Forms;

namespace Game
{
    public partial class SignIn : Form
    {
        public SignIn()
        {
            InitializeComponent();
        }
        //框架加载
        private void Form1_Load(object sender, EventArgs e)
        {
            //读取存储的帐号信息
            FileStream fileStream=null;
            try
            {
                fileStream = new FileStream("./Login.data", FileMode.Open);
            }
            catch {
                fileStream = new FileStream("./Login.data", FileMode.Create);
                fileStream.Close();
                fileStream = new FileStream("./Login.data", FileMode.Open);
            }
            StreamReader streamReader = new StreamReader(fileStream);
            string str = streamReader.ReadToEnd();

            if (str.Length > 5) {
                string[] str1 = str.Split( '\n','\t');
                textBox1.Text = str1[1];
                try
                {
                    textBox2.Text = str1[2];
                }
                catch {}  
            }
            streamReader.Close();
            fileStream.Close();
            //生成随机验证码
            label5.Text = Rand(4);
        }
        //按钮2被按下-清空文本框
        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = null;
            textBox2.Text = null;
            textBox3.Text = null;
        }
        //选择框2被选中-显示密码
        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked)
                textBox2.PasswordChar = '\0';
            else 
                textBox2.PasswordChar = '*';
        }
        //按钮1被选中-登录程序
        private void button1_Click(object sender, EventArgs e)
        {
            //验证验证码是否正确
            if (textBox3.Text.Trim().ToUpper() != label5.Text.Trim().ToUpper())
            {
                MessageBox.Show("验证码不正确!","提示信息");
                label5.Text = Rand(4);
                textBox2.Text = null; textBox3.Text = null;
                return;
            }
            //输入框不能为空
            if (textBox1.Text.Trim() == ""|| textBox2.Text.Trim() == "")
            {
                MessageBox.Show("请补全帐号密码!","提示信息");
                return;
            }
            //进度条的加载
            progressBar1.Visible = true;
            progressBar1.Value = 0;
            System.Threading.Thread.Sleep(1000);//休眠1秒

            //读取登录的帐号信息并分割帐号信息
            FileStream fileStreamR=null;
            //异常控制-如果帐号信息不存在则创建默认帐号信息
            try
            {
                fileStreamR = new FileStream("./Admin.adm", FileMode.Open);
            }
            catch
            {
                fileStreamR = new FileStream("./Admin.adm", FileMode.Create);
                StreamWriter streamWriter = new StreamWriter(fileStreamR);
                streamWriter.Write("170304009-ganyongjie ");
                streamWriter.Close();
                fileStreamR.Close();
                fileStreamR = new FileStream("./Admin.adm", FileMode.Open);
            }
            StreamReader streamReader = new StreamReader(fileStreamR);
            string str = streamReader.ReadToEnd();
            //帐号信息加载完成,进度条更新
            progressBar1.Value = 20;
            
            //依次比较帐号是否相等,如果相等就比较相应密码。帐号-密码的形式不同
            //之间用空格隔开,即密码的前一个字符串必定为对应的帐号。
            string[] str1 = str.Split(' ','-'); 
            //帐号信息分割完成,进度条更新
            progressBar1.Value = 50;
            int temp = (int)fileStreamR.Length;//整个文件的大小
            int step = str1[0].Length + str1[1].Length;//一个帐号信息的大小
            temp = temp / step;//大约多少个帐号
            step = temp / 50+1;//每一个进度条需要加载的帐号数
            temp = 0;//加载的情况
            int i = 0;
            foreach (var v in str1)
            {
                if (i%2==0) {
                    if (textBox1.Text.Trim() == v) {
                        if (textBox2.Text.Trim() == str1[i+1]) {
                            //登录完成,进度条隐藏
                            progressBar1.Value = 100;
                            progressBar1.Visible = false;
                            MessageBox.Show("登录成功!", "登录状态");
                            //根据是否选择记住密码来保存数据
                            FileStream fileStream = new FileStream("./Login.data", FileMode.Create);
                            StreamWriter streamWriter = new StreamWriter(fileStream);
                            if (checkBox1.Checked)
                                streamWriter.Write("begin\n" + textBox1.Text.Trim() + "\t" + textBox2.Text.Trim());
                            else
                                streamWriter.Write("begin\n" + textBox1.Text.Trim());
                            streamWriter.Close();
                            fileStream.Close();

                            break;
                        }
                    }
                }
                str1[i] = "";i++;
                //帐号信息加载完成,进度条更新
                if (i / step > temp)
                {
                    progressBar1.Value += 1;
                    temp = i / step;
                }     
            }
            //登录失败-刷新验证码
            if (i >= str1.Length)
            {
                //登录完成,进度条隐藏
                progressBar1.Value = 100;
                progressBar1.Visible = false;
                MessageBox.Show("登录失败!", "登录状态");
                label5.Text = Rand(4);
                textBox2.Text = null; textBox3.Text = null;
            }
            //关闭文件相关状态
            streamReader.Close();
            fileStreamR.Close();
            GC.Collect();//手动回收垃圾GC
        }
        //验证码刷新
        private void label5_Click(object sender, EventArgs e)
        {
            label5.Text=Rand(4);
        }

        /*自定义函数*/
        private string Rand(int count=4) {//返回生成指定位数的随机字符串的函数
            string code = "";
            var seed = Guid.NewGuid().GetHashCode();//获取全球唯一id(GUID)的哈希值
            Random random = new Random(seed);//随机数对象
            while (code.Length < count)//控制生成的位数
            {
                int c = random.Next(0, 255);
                if (c >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
                {
                    code += (char)c;
                }
            }
            return code;
        }
    }
}

你可能感兴趣的:(源代码)