下面是代码过程
private void Form2_Load(object sender, EventArgs e)
{
this.CenterToScreen();
}
private void douyidou_Click(object sender, EventArgs e)
{
for (int i = 0; i < 20; i++)
{
this.Location = new Point(this.Left + 10,this.Top );
Thread.Sleep(10);
this.Location = new Point(this.Left, this.Top + 10);
Thread.Sleep(10);
this.Location = new Point(this.Left - 10, this.Top);
Thread.Sleep(10);
this.Location = new Point(this.Left - 10, this.Top);
Thread.Sleep(10);
this.Location = new Point(this.Left, this.Top - 10);
Thread.Sleep(10);
this.Location = new Point(this.Left, this.Top - 10);
Thread.Sleep(10);
this.Location = new Point(this.Left + 10, this.Top);
Thread.Sleep(10);
this.Location = new Point(this.Left + 10, this.Top);
Thread.Sleep(10);
this.Location = new Point(this.Left, this.Top + 10);
Thread.Sleep(10);
this.Location = new Point(this.Left - 10, this.Top);
Thread.Sleep(10);
}
}
}
}
1)第一种方法:
private void yanzhengma_Click(object sender, EventArgs e)
{
string ai = "";
Random p = new Random();
for (int i = 0; i < 4; i++)
{
int type = p.Next(0, 3);
if (type ==0)
{
ai += ((char)p.Next(97, 123)).ToString();
// this.BackColor = Color.Red;
}
if (type ==1)
{
ai += ((char)p.Next(65, 91)).ToString();
}
if (type ==2)
{
ai += p.Next(0, 10).ToString();
}
}
label1.Text = ai;
string str1 = ai.Substring(0, 1);
string str2 = ai.Substring(1, 1);
string str3 = ai.Substring(2, 1);
string str4 = ai.Substring(3, 1);
label1.Text = str1;
label2.Text = str2;
label3.Text = str3;
label4.Text = str4;
label1.ForeColor = Color.Red ;
label1.Font = new Font ("楷体",20f );
label2.ForeColor = Color.Blue;
label2.Font = new Font("楷体", 20f);
label3.ForeColor = Color.Yellow;
label3.Font = new Font("楷体", 20f);
label4.ForeColor = Color.Orange;
label4.Font = new Font("楷体", 20f);
}
}
}
2)第二种方法:
private void yanzhengma_Click(object sender, EventArgs e)
{
string g = "";
Random p = new Random();
for (int i = 0; i < 4; i++)
{
int type = p.Next(0, 3);
if (type == 0)
{
g += ((char)p.Next(97, 123)).ToString() + "-";
}
if (type == 1)
{
g += ((char)p.Next(65, 91)).ToString() + "-";
}
if (type == 2)
{
g += p.Next(0, 10).ToString() + "-";
}
}
Console.WriteLine(g);
string[] str = g.Split('-');
label1.Text = str[0];
label2.Text = str[1];
label3.Text = str[2];
label4.Text = str[3];
label1.ForeColor = Color.Pink;
label1.Font = new Font("宋体", 30f, FontStyle.Italic);
label2.ForeColor = Color.Red;
label2.Font = new Font("宋体", 30f, FontStyle.Bold);
label3.ForeColor = Color.Blue;
label3.Font = new Font("宋体", 30f, FontStyle.Underline);
label4.ForeColor = Color.Black;
label4.Font = new Font("宋体", 30f);
}
}
}
第三种方法
namespace 随机验证码
{
public partial class Form7 : Form
{
public Form7()
{
InitializeComponent();
}
private void Form7_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string g = "";
Random p = new Random();
for (int i = 0; i < 4; i++)
{
int type = p.Next(0, 3);
if (type == 0)
{
g += ((char)p.Next(97, 123)).ToString() ;
}
if (type == 1)
{
g += ((char)p.Next(65, 91)).ToString() ;
}
if (type == 2)
{
g += p.Next(0, 10).ToString() ;
}
}
label1.Text = g[0].ToString();
label2.Text = g[1].ToString();
label3.Text = g[2].ToString();
label4.Text = g[3].ToString();
label1.ForeColor = Color.FromArgb(p.Next(0, 255), p.Next(0, 255), p.Next(0, 255));
label2.ForeColor = Color.FromArgb(p.Next(0, 255), p.Next(0, 255), p.Next(0, 255));
label3.ForeColor = Color.FromArgb(p.Next(0, 255), p.Next(0, 255), p.Next(0, 255));
label4.ForeColor = Color.FromArgb(p.Next(0, 255), p.Next(0, 255), p.Next(0, 255));
label1.Font = new Font("楷体",p.Next(14, 30));
label2.Font = new Font("楷体", p.Next(18, 30));
label3.Font = new Font("楷体", p.Next(16, 30));
label4.Font = new Font("楷体", p.Next(14, 30));
}
}
}
4)颜色,字体大小随机
label1.ForeColor = Color.FromArgb(p.Next(0, 255), p.Next(0, 255), p.Next(0, 255));
label2.ForeColor = Color.FromArgb(p.Next(0, 255), p.Next(0, 255), p.Next(0, 255));
label3.ForeColor = Color.FromArgb(p.Next(0, 255), p.Next(0, 255), p.Next(0, 255));
label4.ForeColor = Color.FromArgb(p.Next(0, 255), p.Next(0, 255), p.Next(0, 255));
label1.Font = new Font("楷体",p.Next(14, 30));
label2.Font = new Font("楷体", p.Next(18, 30));
label3.Font = new Font("楷体", p.Next(16, 30));
label4.Font = new Font("楷体", p.Next(14, 30));
namespace 随机验证码
{
public partial class Form8 : Form
{
public Form8()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
int x = this.Left;
int y = this.Top;
for (int i = 0; i < 10; i++)
{
this.Location = new Point(x - 10, y);
this.Location = new Point(x - 10, y - 10);
this.Location = new Point(x, y - 10);
this.Location = new Point(x + 10, y - 10);
this.Location = new Point(x + 10, y);
this.Location = new Point(x + 10, y + 10);
this.Location = new Point(x, y + 10);
this.Location = new Point(x - 10, y + 10);
this.Location = new Point(x - 10, y);
this.Location = new Point(x, y);
}
timer1.Stop();
}
private void Form8_Load(object sender, EventArgs e)
{
}
}
}
以上就是今天的总结,还有不足之处,望各位朋友加以指正。