C#做的登录界面包含验证码

时间:2021-05-26 17:09:32

登录界面如下所示:C#做的登录界面包含验证码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;

namespace 登录
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static string str1 = "";
public static void playMusic()
{
SoundPlayer player = new SoundPlayer();
player.SoundLocation = @"C:\Users\xinqiang\Desktop\1.wav";
player.Play();
}
private void login_Click(object sender, EventArgs e)
{
string strname = txtName.Text.Trim();
string strpwd = txtpwd.Text;
if (rdostudent.Checked || rdoteacher.Checked)
{
if (rdostudent.Checked)
{
if (strname == "student" && strpwd == "student")
{
if (txtvertify.Text.CompareTo(str1) == 0)
{
MessageBox.Show("登陆成功");
playMusic();
}
else
MessageBox.Show("验证码错误");
}
else
{
MessageBox.Show("用户名或者密码错误");
txtName.Clear();
txtpwd.Clear();
txtName.Focus();
}
}
else if (rdoteacher.Checked)
{
if (strname == "teacher" && strpwd == "teacher")
{
if (txtvertify.Text.CompareTo(str1) == 0)
{
MessageBox.Show("登陆成功");
playMusic();
}
else
MessageBox.Show("验证码错误");
}
else
{
MessageBox.Show("用户名或者密码错误");
txtName.Clear();
txtpwd.Clear();
txtvertify.Clear();
txtName.Focus();
}
}
}
else
{
MessageBox.Show("请先选择登录类型");
txtName.Clear();
txtpwd.Clear();
txtvertify.Clear();
txtName.Focus();
}
}

private void reset_Click(object sender, EventArgs e)
{
string strname = txtName.Text.Trim();
string strpwd = txtpwd.Text;
txtName.Clear();
txtpwd.Clear();
txtvertify.Clear();
txtName.Focus();
}
//制作验证码
private void pictureBox1_Click(object sender, EventArgs e)
{
Random r = new Random();
string str = "";
for (int i = 0; i < 5; i++)
{
str += r.Next(0, 10);
}
str1 = string.Copy(str);
//MessageBox.Show(str);
Bitmap bmp = new Bitmap(120, 40);
Graphics g = Graphics.FromImage(bmp);
//随机生成数字
for (int i = 0; i < 5; i++)
{
Point p = new Point(i * 20, 0);
string[] fonts = { "微软雅黑", "宋体", "黑体", "隶书", "华文楷体" };
Color[] cl = { Color.Black, Color.Blue, Color.Red, Color.Green, Color.Yellow };
g.DrawString(str[i].ToString(), new Font(fonts[r.Next(0, 5)], 20, FontStyle.Bold), new SolidBrush(cl[r.Next(0, 5)]), p);
}
//绘制直线识别障碍
for (int i = 0; i < 20; i++)
{
Pen pen = new Pen(Brushes.Black);
Point p1 = new Point(r.Next(0, bmp.Width), r.Next(0, bmp.Height));
Point p2 = new Point(r.Next(0, bmp.Width), r.Next(0, bmp.Height));
g.DrawLine(pen, p1, p2);
}
for (int i = 0; i < 500; i++)
{
bmp.SetPixel(r.Next(0, bmp.Width), r.Next(0, bmp.Height), Color.Black);
}
//将bmp镶嵌到picturbox中去
pictureBox1.Image = bmp;
}

private void Form1_Load(object sender, EventArgs e)
{
label3.Text = DateTime.Now.ToString();
Random r = new Random();
string str = "";
for (int i = 0; i < 5; i++)
{
str += r.Next(0, 10);
}
str1 = string.Copy(str);
//MessageBox.Show(str);
Bitmap bmp = new Bitmap(120, 40);
Graphics g = Graphics.FromImage(bmp);
//随机生成数字
for (int i = 0; i < 5; i++)
{
Point p = new Point(i * 20, 0);
string[] fonts = { "微软雅黑", "宋体", "黑体", "隶书", "华文楷体" };
Color[] cl = { Color.Black, Color.Blue, Color.Red, Color.Green, Color.Yellow };
g.DrawString(str[i].ToString(), new Font(fonts[r.Next(0, 5)], 20, FontStyle.Bold), new SolidBrush(cl[r.Next(0, 5)]), p);
}
//绘制直线识别障碍
for (int i = 0; i < 20; i++)
{
Pen pen = new Pen(Brushes.Black);
Point p1 = new Point(r.Next(0, bmp.Width), r.Next(0, bmp.Height));
Point p2 = new Point(r.Next(0, bmp.Width), r.Next(0, bmp.Height));
g.DrawLine(pen, p1, p2);
}
for (int i = 0; i < 500; i++)
{
bmp.SetPixel(r.Next(0, bmp.Width), r.Next(0, bmp.Height), Color.Black);
}
//将bmp镶嵌到picturbox中去
pictureBox1.Image = bmp;
}

private void timer1_Tick(object sender, EventArgs e)
{
label3.Text = DateTime.Now.ToString();

}
}
}

没有连接数据库