登陆小界面设计

4 篇文章 0 订阅
  • 用户登陆界面
  • 密码隐藏
  • 动态验证码
  • 保护设置

源代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private int erro_times = 0;
        private void btn_log_in_Click(object sender, EventArgs e)
        {           
            if (erro_times > 5)
            {
                //Application.Exit();  // exit
                System.Environment.Exit(System.Environment.ExitCode);  // Force to exit
                MessageBox.Show("输入次数过多!");
            }
            if (tbox_check.Text == _code)
            {
                MessageBox.Show("验证码正确!!!" + "密码验证中...");
            }
            else
            {
                MessageBox.Show("验证码错误!!!");
                erro_times += 1;
                CreateCodeImage();
            }


        }

        private void btn_log_off_Click(object sender, EventArgs e)
        {
            MessageBox.Show("注销当前用户!");
        }

        private void Form1_Load(object sender, EventArgs e)  //窗体的Load事件,加载时执行
        {
            CreateCodeImage();           
            
            //if (MessageBox.Show("是否登陆!", "", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            //{
            //}
        }

        private void btn_change_Click(object sender, EventArgs e)
        {
            CreateCodeImage();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            // 创建对话框属性
            DialogResult dr = MessageBox.Show("是否退出!", "提示!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (dr == DialogResult.Yes)
            {
                e.Cancel = false; // 点击“是”按键则关闭
            }
            else
            {
                e.Cancel = true; // 点击“否”按键则不执行操作
            }
        }

        //Form2 frm2 = new Form2(); // 创建Form2窗体的对象
        //frm2.MdiParent = this; // 设置MdiParent属性,将当前窗体作为父窗体
        //frm2.Show(); // 使用Show方法打开窗体

        //Form3 frm3 = new Form3(); // 创建Form3窗体的对象
        //frm3.MdiParent = this; // 设置MdiParent属性,将当前窗体作为父窗体
        //frm3.Show(); // 使用Show方法打开窗体

        private string _code = "";

        public string Code
        {
            get
            {
                return this._code;
            }
        }

        public void CreateCodeImage()
        {
            string[] str = new string[4];
            //string serverCode = "";
            // Random number generator 
            Random random = new Random();
            _code = "";
            for (int i = 0; i < 4; i++)
            {
                str[i] = random.Next(10).ToString().Substring(0, 1);
                _code += str[i];
            }
            CreateCodeImage(str);
        }

        public void CreateCodeImage(string[] checkCode)
        {
            if (checkCode == null || checkCode.Length <= 0)
                return;

            //  Initializes a new instance of the Bitmap class with the specified size.
            //System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 32.5)),this.CodePictureBox.Height);
            //System.Drawing.Bitmap image = new System.Drawing.Bitmap(this.CodePictureBox.Width, this.CodePictureBox.Height);
            Bitmap image = new Bitmap(this.CodePictureBox.Width, this.CodePictureBox.Height);

            //System.Drawing.Graphics g = Graphics.FromImage(image);
            Graphics g = Graphics.FromImage(image);  // Creates a new Graphics from the specified Image.
            try
            {
                Random random = new Random();
                g.Clear(Color.White); // Clears the entire drawing surface and fills it with the specified background color.

                //  Draw background noise lines for the picture
                for (int i = 0; i < 20; i++)
                {
                    int x1 = random.Next(image.Width);
                    int x2 = random.Next(image.Width);
                    int y1 = random.Next(image.Height);
                    int y2 = random.Next(image.Height);
                    g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
                }
                // Custom Colors
                Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
                // Custom Font
                string[] f = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };

                for (int k = 0; k <= checkCode.Length - 1; k++)
                {
                    int cindex = random.Next(8);
                    int findex = random.Next(5);
                    // Font drawFont = new Font(f[findex], 16, (System.Drawing.FontStyle.Bold));
                    Font drawFont = new Font(f[findex], 16, (FontStyle.Bold));     //  Bold text.
                    SolidBrush drawBrush = new SolidBrush(c[cindex]); // fill graphics

                    float x = 3.0F;
                    float y = 0.0F;
                    float width = 20.0F;
                    float height = 25.0F;
                    int sjx = random.Next(10);
                    int sjy = random.Next(image.Height - (int)height);
                    RectangleF drawRect = new RectangleF(x + sjx + (k * 14), y + sjy, width, height);

                    // specifies formatting attributes
                    StringFormat drawFormat = new StringFormat();
                    drawFormat.Alignment = StringAlignment.Center;

                    g.DrawString(checkCode[k], drawFont, drawBrush, drawRect, drawFormat);
                }
                // Draw a picture of the foreground noise point
                for (int i = 0; i < 100; i++)
                {
                    int x = random.Next(image.Width);
                    int y = random.Next(image.Height);
                    image.SetPixel(x, y, Color.FromArgb(random.Next()));
                    // image.SetPixel(x, y, Color.FromArgb(128));
                }
                // Draw the border line of the picture
                g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
                MemoryStream ms = new MemoryStream();
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                this.CodePictureBox.Image = Image.FromStream(ms);
            }
            finally
            {
                g.Dispose();
                image.Dispose();
            }
        }

        private bool windowCreate = true;

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.Visible == true)
            {
                this.Hide();
                this.ShowInTaskbar = false;
            }
            else
            {
                this.Visible = true;
                this.ShowInTaskbar = true;
                this.WindowState = FormWindowState.Normal;
                //this.Show();
                this.BringToFront();
                windowCreate = true;
            }
        }

        private void 最小化ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (windowCreate)
            {
                base.Visible = false;
                windowCreate = false;
            }
            this.Hide();
            base.OnActivated(e); 
        }

      

        
    }
}

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值