⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fmlogin.cs

📁 一个通讯录源码,用C#写的.挺不错的,大家可以参考看一下.
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using Microsoft.Win32;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Drawing.Drawing2D;
using System.Globalization;

namespace AddressList.Forms
{
    public partial class fmLogin : fmBaseForm
    {
        /// <summary>
        /// 是否重新登录 注销
        /// </summary>
        private bool isReLogin = false;

        public fmLogin()
        {
            InitializeComponent();
        }

        public bool IsReLogin
        {
            get
            {
                return isReLogin;
            }
            set
            { 
                isReLogin = value;
            }
        }

        private const string sError = "错误";
        private const string sWarring = "警告";
        private const string sInfo = "提示";

        private void btnCancle_Click(object sender, EventArgs e)
        {
            if (!isReLogin)
            {
                Application.Exit();
            }
            else
            {
                this.DialogResult = DialogResult.Cancel;
                Close();
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            DoAccept();  //确定
        }

        //<summauly>
        //验证密码
        //</summauly>
        private bool CheckPassWord()
        {
            const string SReadUser = "SELECT FUserID,FSCPwd,FDescription FROM t_User WHERE FName = {0}";
            bool CheckedPwd = false;
            bool hasRows = false;
       
            object[] userData = DBConn.DBSelectValue(string.Format(SReadUser, QuoteStr(txtUser.Text)));
            hasRows = (null != userData);
            if (hasRows)
            {
                if (txtPassWord.Text == userData[1].ToString())
                {
                    CurrentLoginInfo.LoginID = userData[0].ToString();
                    CurrentLoginInfo.LoginTime = DateTime.Now;
                    CurrentLoginInfo.Password = txtPassWord.Text;
                    CurrentLoginInfo.UserID = (int)userData[0];

                    CurrentUserInfo.LoginID = CurrentLoginInfo.LoginID;
                    CurrentUserInfo.Password = CurrentLoginInfo.Password;
                    CurrentUserInfo.UserID = CurrentLoginInfo.UserID;
                    CurrentUserInfo.UserName = txtUser.Text;
                    CurrentUserInfo.Description = userData[2].ToString();
                    CheckedPwd = true;
                }
            }                  
               
            if (!CheckedPwd)
            {
                const string sUserNameError = "录入的用户名不存在,请确认。";
                const string sPassWordError = "录入的密码错误,请确认。";
                string sError;
                Control editControl;
                if (hasRows)               
                {
                    sError = sPassWordError; 
                    editControl = txtPassWord;
                }
                else
                {
                    sError = sUserNameError;
                    editControl = txtUser;
                }
                errProvider.SetError(editControl, sError);
                ShowErrorMsg(sError);
            }
            return CheckedPwd;
        }

        //确定
        private void DoAccept()
        {
            errProvider.Clear();
            if (!CheckBlank())
                return;
            if (!CheckPassWord())
                return;

            if (savePassWord.Checked)
                SaveInfoToRegistry();
            this.DialogResult = DialogResult.Yes;
            Close();
        }

        private bool CheckBlank()
        {
            bool result = !EditIsBlank(txtUser, loginUserName, true);
            if (!result) errProvider.SetError(txtUser, eInputUserName);
            return result;
        }

        private const string loginUserName = "用户名";
        private const string eInputUserName = "请录入用户名";
        private const string userRoot  = "HKEY_CURRENT_USER";
        private const string subKey = "AddressList";
        private const string keyName = userRoot + "\\SoftWare\\" + subKey;
        private const string loginInfo = "LoginInfo";

        [SerializableAttribute]  //序列化 标识
        private class LoginUserInfo
        {
            public string UserName;
            public string PassWord;
            public bool SavePassWord;
        };                     

        //保存信息到注册表
        private void SaveInfoToRegistry()
        {
            LoginUserInfo userInfo = new LoginUserInfo();
            userInfo.UserName = txtUser.Text;
            userInfo.PassWord = txtPassWord.Text;
            userInfo.SavePassWord = savePassWord.Checked;
            
            MemoryStream stream = new MemoryStream();
            try
            {
                BinaryFormatter binFmt = new BinaryFormatter();     //创建二进制序列化类
                binFmt.Serialize(stream, userInfo);                 //序列化类

                stream.Position = 0;
                Registry.SetValue(keyName, loginInfo, stream.GetBuffer(), RegistryValueKind.Binary);  //将流的二进制信息写到注册表
            }
            catch 
            {
                throw;
            }
            finally
            {
                stream.Close();
                stream.Dispose();
            }
        }

        //读取保存在注册表的信息
        private void ReadUserInfo()
        {
            //读取注册表信息
            byte[] bytes = (byte[])Registry.GetValue(keyName, loginInfo, null);
            if ((bytes != null) && (bytes.Length > 0))
            {
                MemoryStream stream = new MemoryStream();
                try
                {
                    //将信息写入流中
                    stream.Write(bytes, 0, bytes.Length);
                    stream.Position = 0;
                    BinaryFormatter binFmt = new BinaryFormatter();
                    LoginUserInfo userInfo = new LoginUserInfo();
                    //反序列
                    userInfo = (LoginUserInfo)binFmt.Deserialize(stream);

                    savePassWord.Checked = userInfo.SavePassWord;
                    if (userInfo.SavePassWord)
                    {
                        txtUser.Text = userInfo.UserName;
                        txtPassWord.Text = userInfo.PassWord;
                    }
                }
                catch
                {
                    //throw;
                }
                finally
                {
                    stream.Close();
                    stream.Dispose();
                }
            }            
        }

        private void PaintImage()
        {
            const string sExeCapion = "通讯录";
            const string sVersion   = "版本 ";
            const string sFontName  = "华文行楷";
            const string sVerFontName = "Arial";

            const int DecWidth = 2;

            RectangleF rect = picBoxLogin.ClientRectangle;

            Image image = new Bitmap(pictureBox1.Image, (int)rect.Width, (int)rect.Height);

            Graphics g = Graphics.FromImage(image);

            //清除锯齿效果
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;           

            //画黑色
            //Pen pen = new Pen(Brushes.Black);
            //g.DrawArc(pen, rect, 2, 2);
            //g.FillRectangle(Brushes.Black, rect);
            Brush brush = new SolidBrush(Color.FromArgb(47, 67, 115));
            g.FillRectangle(brush, rect);
            brush.Dispose();

            //画红色
            rect.Offset(DecWidth, DecWidth);
            rect.Height = rect.Height - DecWidth * 2;
            rect.Width = rect.Width - DecWidth * 2;
            Color beginColor = Color.FromArgb(255, 255, 255); //Color.FromArgb(107, 116, 203);
            Color endColor = Color.FromArgb(163, 184, 226); //Color.FromArgb(103, 114, 196); //Color.FromArgb(215, 212, 204);  
            //brush = new LinearGradientBrush(new PointF(rect.X, rect.Y), new PointF(rect.Width, rect.Height), 
            //                                      beginColor, endColor);            
            try
            {
                brush = new LinearGradientBrush(rect, beginColor, endColor, LinearGradientMode.Vertical);
                g.FillRectangle(brush, rect);                
            }
            finally
            {
                brush.Dispose();
            }
            //画白色字体 
            Font font = new Font(sFontName, 26, FontStyle.Bold);
            SizeF sSize = g.MeasureString(sExeCapion, font);
            PointF point = new PointF(10, (rect.Height - sSize.Height) / 2 + DecWidth);


            g.DrawString(sExeCapion, font, Brushes.White, point);

            //画绿色字体
            point.X = point.X - 1;
            point.Y = point.Y - 1;
            g.DrawString(sExeCapion, font, Brushes.Green, point);

            //画版本号
            string version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
 
            //画白色字体 
            font.Dispose();
            font = new Font(sVerFontName, 9, FontStyle.Bold);
            sSize = g.MeasureString(sExeCapion, font);
            point.X = rect.Width - sSize.Width * 2;
            point.Y = rect.Height - (float)(sSize.Height * 1.5);
            g.DrawString(sVersion + version, font, Brushes.Black, point);

            picBoxLogin.Image = image;

            g.Dispose();
            font.Dispose();
        }

        private void SetToolTips()
        {
            const string sInputUserName = "录入用户名称";
            const string sInputPassWord = "录入密码";
            ttTip.SetToolTip(txtUser, sInputUserName);
            ttTip.SetToolTip(txtPassWord, sInputPassWord);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            ReadUserInfo();
            PaintImage();
            SetToolTips();
        }

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -