1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Threading.Tasks;
9 using System.Windows.Forms;
10
11 namespace Test_1
12 {
13 public partial class Form1 : Form
14 {
15 //验证码
16 string Code = "";
17 public Form1()
18 {
19 InitializeComponent();
20 Verification_Code();
21 Form_Load();
22 }
23
24 #region 自定义函数
25 private void Form_Load()
26 {
27 toolTip1.SetToolTip(User_ID, "请输入账号(默认账号为20172202899)");
28 toolTip1.SetToolTip(Password, "请输入密码(默认账号为123456)");
29 toolTip1.SetToolTip(Captcha, "请输入验证码");
30 toolTip1.SetToolTip(button1, "登录并显示文本编辑器");
31 toolTip1.SetToolTip(button2, "退出");
32 toolTip1.SetToolTip(pictureBox1, "您好,我叫龙猫");
33 toolTip1.SetToolTip(pictureBox2, "验证码为" + Code);
34
35 }
36 private void Verification_Code(){
37 Bitmap map = new Bitmap(pictureBox2.Width, pictureBox2.Height);
38 Graphics G = Graphics.FromImage(map);
39
40 Random rand = new Random();
41 Code = "";
42 for (int i = 0; i < 4; i++)
43 {
44 int x = rand.Next(62);
45 if( x < 10)
46 {
47 Code += x;
48 }else if( x <= 35)
49 {
50 char tmp = (char)(65 + x - 10);
51 Code += tmp;
52 }
53 else
54 {
55 char tmp = (char)(97 + x - 36);
56 Code += tmp;
57 }
58
59 }
60 string[] MyFont = { "宋体", "隶书", "楷书", "微软雅黑" };
61
62 Color[] MyColor = { Color.Red, Color.Green, Color.Blue, Color.Brown };
63
64 for (int i = 0; i < 4; i++)
65 {
66 //设置位置
67 Point p = new Point(i*30, 10);
68 //设置字体(字型,字号,粗细)
69 Font myFont = new Font(MyFont[rand.Next(4)], 20, FontStyle.Bold);
70 //填充颜色
71 SolidBrush Sol_b = new SolidBrush(MyColor[rand.Next(4)]);
72 //画
73 G.DrawString(Code[i].ToString(), myFont, Sol_b, p);
74 }
75 //设置背景颜色
76 //pictureBox2.BackColor = Color.Black;
77
78 //线
79 for( int i = 0; i < 5; i++)
80 {
81 Point P1 = new Point(rand.Next(map.Width), rand.Next(map.Height));
82 Point P2 = new Point(rand.Next(map.Width), rand.Next(map.Height));
83 G.DrawLine(new Pen(Color.Gray),P1, P2);
84 }
85 //点
86 for( int i = 0; i < 50; i++)
87 {
88 Point P = new Point(rand.Next(map.Width), rand.Next(map.Height));
89 map.SetPixel(P.X, P.Y, Color.Black);
90 }
91 pictureBox2.Image = map;
92 }
93
94 void Clear_Account()
95 {
96 User_ID.Clear();
97 Password.Clear();
98 }
99 void Clear_Password()
100 {
101 Password.Clear();
102 }
103 void Clear_VC()
104 {
105 Captcha.Clear();
106 }
107
108 private void btn_Click()
109 {
110 if (Captcha.Text.ToLower() != Code.ToLower())
111 {
112 MessageBox.Show("验证码输入有误", "验证码有误", MessageBoxButtons.OK);
113 Clear_VC();
114 }
115 else if (User_ID.Text == "20172202899" && Password.Text == "123456")
116 {
117 Form2 form2 = new Form2();
118 form2.ShowDialog();
119 }
120 else
121 {
122 if (User_ID.Text != "20172202899")
123 {
124 MessageBox.Show("不存在该用户", "用户名有误", MessageBoxButtons.OK);
125 Clear_Account();
126 }
127 else
128 {
129 MessageBox.Show("输入的密码错误", "密码错误", MessageBoxButtons.OK);
130 Clear_Password();
131 }
132 }
133 }
134 #endregion
135
136 #region 点击事件
137
138 private void pictureBox2_Click(object sender, EventArgs e)
139 {
140 Verification_Code();
141 }
142
143 private void button1_Click(object sender, EventArgs e)
144 {
145 btn_Click();
146 }
147
148 private void button2_Click(object sender, EventArgs e)
149 {
150 MessageBox.Show("感谢使用","Warming", MessageBoxButtons.OK);
151 this.Close();
152 }
153 #endregion
154
155
156 #region 按键设置
157
158 private void Captcha_KeyPress(object sender, KeyPressEventArgs e)
159 {
160 if (e.KeyChar == 13)
161 {
162 btn_Click();
163 }
164 }
165 #endregion
166
167 #region 时钟设计
168 private void timer1_Tick(object sender, EventArgs e)
169 {
170 Title.Left-=10;
171 if( Title.Left <= -Title.Width)
172 {
173 Title.Left = this.Width + Title.Width;
174 }
175 }
176
177
178 #endregion
179
180 }
181
182 }