任务描述
要求设计一个Winform窗体应用程序,进入程序后显示第一个窗体及相应的验证码,验证码可以刷新。然后允许用户输入进行验证。如果验证成功则跳到窗体2,否则报错。
窗体设计
主要是对Form1进行设计,Form2只是检验Form1的功能。
Form1包括:两个Label控件,一个TextBox控件(用于用户输入验证码),一个PictureBox控件(用于程序显示验证码),两个Button控件(Button1表示确认,Button2表示刷新)。
Form1设计界面:
Form2设计界面:
用到的主要类及方法
自己写两个方法:
- CheckBox():用于生成四位的验证码。
- CodeImage():用于在PictureBox控件上绘制验证码。
用到的类与方法:
- Random类,random.Next().
- (char)强制类型转换。
- Bitmap类及其创造函数。bitmap.SetPixel():画点
- Graphics类,创建:Graphics.FromImage()。grahics.DrawLine():画线。
- grahics.DrawString():画字符串。
- grahics.DrawRectangle(): 画边框线矩形。
- 设置PictureBox.BackgroundImage= imit.
- showdialog, this.dispose(): 窗体切换的方法。
主程序代码
Form1的代码:
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;
namespace WindowsFormsApp5
{
public partial class Form1 : Form
{
private string checkcode;
public Form1()
{
InitializeComponent();
}
private string CheckBox() //生成所需的验证码
{
int number;
char code;
Random random = new Random();
this.checkcode = String.Empty;
string showcheckcode = String.Empty;
for(int i=0;i<4;i++)
{
number = random.Next(36);
if(number>10)
{
code = (char)('A' + (char)((number-9) % 26));
}
else
{
code = (char)('0' + (char)(number));
}
showcheckcode += " " + code.ToString();
this.checkcode += code.ToString();
}
return showcheckcode;
}
private void CodeImage(string showcheckcode) //显示验证码图片
{
/*所需要的操作包括:
* 1.画背景噪音线
* 2.画验证码字符串
* 3.画前景噪音点
* 4.画边框线
*/
if (showcheckcode == null || showcheckcode.Trim() == String.Empty) return;
Bitmap imit = new Bitmap((int)Math.Ceiling((showcheckcode.Length*10.5)), 20);
Graphics g = Graphics.FromImage(imit);
Random random = new Random(); //生成随机生成器对象
g.Clear(Color.White); //清空图片背景色
//画背景噪音线
for(int i=0;i<3; i++)
{
int x1 = random.Next(imit.Width);
int x2 = random.Next(imit.Width);
int x3 = random.Next(imit.Width);
int y1 = random.Next(imit.Height);
int y2 = random.Next(imit.Height);
int y3 = random.Next(imit.Height);
g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
}
//画验证码字符串
Font font = new Font("Arial", 12, (FontStyle.Bold));
g.DrawString(showcheckcode, font, new SolidBrush(Color.Red), 2, 2);
//画前景噪声点
for(int i=0;i<100;i++)
{
int x3 = random.Next(imit.Width);
int y1 = random.Next(imit.Height);
imit.SetPixel(x3, y1, Color.FromArgb(random.Next()));
}
//画边框线
g.DrawRectangle(new Pen(Color.Blue), 0,0, imit.Width - 1, imit.Height - 1);
this.pictureBox1.Width = imit.Width;
this.pictureBox1.Height = imit.Height;
this.pictureBox1.BackgroundImage = imit;
}
private void Form1_Load(object sender, EventArgs e)
{
CodeImage(CheckBox());
}
private void button2_Click(object sender, EventArgs e)
{
CodeImage(CheckBox());
}
private void button1_Click(object sender, EventArgs e)
{
if(this.checkcode==textBox1.Text)
{
Form2 frm2 = new Form2();
frm2.ShowDialog();
this.Hide();
this.Dispose();
}
else
{
MessageBox.Show("验证码错误,请重新输入!");
CodeImage(CheckBox());
}
}
}
}
Form2的代码:
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;
namespace WindowsFormsApp5
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Form1 frm1 = new Form1();
frm1.ShowDialog();
this.Dispose();
}
}
}