【Windows】窗体基本控件的使用
效果图
Form1_登录窗口
Form2_编辑器
Form2_编辑器效果
Form3_自测题
Form4_窗口
Form4_光
Form4_海洋
Form4_火
Form4_雷霆
Form4_水晶
Form4_影
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 Form4 form4 = new Form4(); 118 form4.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 }
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 Form2 : Form 14 { 15 public Form2() 16 { 17 InitializeComponent(); 18 Form_Load(); 19 Init_FontFa = textBox1.Font.FontFamily; 20 Init_FontEsize = textBox1.Font.Size; 21 } 22 23 FontFamily Init_FontFa; 24 float Init_FontEsize; 25 #region 自定义函数 26 void Btn1_Click() 27 { 28 textBox1.Font = new Font(Init_FontFa, Init_FontEsize); 29 textBox1.ForeColor = Color.Red; 30 } 31 void Btn2_Click() 32 { 33 textBox1.Font = new Font("宋体", 16); 34 textBox1.ForeColor = Color.Black; 35 } 36 void Form_Load() 37 { 38 toolTip1.SetToolTip(textBox1, "输入文本进去"); 39 toolTip1.SetToolTip(textBox2, "文本只读,并显示上面文本形式"); 40 toolTip1.SetToolTip(button1, "红色"); 41 toolTip1.SetToolTip(button2, "黑色,并同时改字号为16"); 42 } 43 #endregion 44 45 46 #region 点击事件 47 48 private void button1_Click(object sender, EventArgs e) 49 { 50 Btn1_Click(); 51 } 52 53 private void button2_Click(object sender, EventArgs e) 54 { 55 Btn2_Click(); 56 } 57 private void btn3_click(object sender, EventArgs e) 58 { 59 fontDialog1.ShowColor = true; 60 DialogResult dr = fontDialog1.ShowDialog(); 61 62 if (dr == DialogResult.OK) 63 { 64 textBox1.Font = fontDialog1.Font; 65 textBox1.ForeColor = fontDialog1.Color; 66 } 67 } 68 69 #endregion 70 71 #region 同时显示 TextChanged 72 private void textBox1_TextChanged(object sender, EventArgs e) 73 { 74 textBox2.Text = textBox1.Text; 75 } 76 #endregion 77 78 79 } 80 }
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 Form3 : Form 14 { 15 DateTime Start_time,End_time; 16 public Form3() 17 { 18 InitializeComponent(); 19 Start_time = System.DateTime.Now; 20 End_time = DateTime.Parse(Start_time.ToString("yyyy-MM-dd HH:mm:ss")).AddMinutes(1); 21 Start_Time_Tb.Text = Start_time.ToString("yyyy-MM-dd HH:mm:ss"); 22 End_Time_Tb.Text = End_time.ToString("yyyy-MM-dd HH:mm:ss"); 23 } 24 public void btn_close() 25 { 26 this.Close(); 27 } 28 public void btn_submit() 29 { 30 int total_score = 0; 31 if (radioButton1_3.Checked) total_score += 20; 32 if (radioButton2_4.Checked) total_score += 20; 33 try{ 34 if ((string)(comboBox1.Items[comboBox1.SelectedIndex]) == "法师") 35 total_score += 20; 36 } 37 catch (Exception ex) 38 { 39 total_score += 0; 40 } 41 42 if (checkBox4_1.Checked) total_score += 10; 43 if (checkBox4_3.Checked) total_score += 10; 44 45 foreach (int i in listBox1.SelectedIndices) 46 { 47 if (i == 0 || i == 3) total_score += 10; 48 } 49 Score_txt.Text = total_score.ToString(); 50 } 51 52 private void Time_Show(object sender, EventArgs e) 53 { 54 TimeSpan t = End_time.Subtract(DateTime.Now); 55 Diff_Time_Tb.Text = t.ToString(@"hh\:mm\:ss"); 56 if( t.ToString(@"hh\:mm\:ss") == "00:00:00") 57 { 58 btn_submit(); 59 timer1.Enabled = false; 60 } 61 } 62 63 private void btn_submit(object sender, EventArgs e) 64 { 65 btn_submit(); 66 } 67 68 public void btn_close(object sender, EventArgs e) 69 { 70 btn_close(); 71 } 72 73 } 74 }
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 Form4 : Form 14 { 15 public Form4() 16 { 17 InitializeComponent(); 18 } 19 20 public void Form_load(object sender, EventArgs e) 21 { 22 toolStripStatusLabel1.Text = "光"; 23 pictureBox1.Image = Properties.Resources.光_维恩; 24 Light.Checked = false; 25 } 26 27 28 private void 自测题ToolStripMenuItem_Click(object sender, EventArgs e) 29 { 30 Form3 form3 = new Form3(); 31 form3.ShowDialog(); 32 } 33 34 private void 文本编辑ToolStripMenuItem_Click(object sender, EventArgs e) 35 { 36 Form2 form2 = new Form2(); 37 form2.ShowDialog(); 38 } 39 40 private void Fire_Click(object sender, EventArgs e) 41 { 42 Fire.Checked = true; 43 Light.Checked = false; 44 Shadow.Checked = false; 45 Ocean.Checked = false; 46 Polar.Checked = false; 47 Crystal.Checked = false; 48 Electric.Checked = false; 49 pictureBox1.Image = Properties.Resources.火_阿木木; 50 toolStripStatusLabel1.Text = "Fire"; 51 } 52 53 private void Light_Click(object sender, EventArgs e) 54 { 55 Fire.Checked = false; 56 Light.Checked = true; 57 Shadow.Checked = false; 58 Ocean.Checked = false; 59 Polar.Checked = false; 60 Crystal.Checked = false; 61 Electric.Checked = false; 62 Random r = new Random(); 63 int k = r.Next(2); 64 if( k == 0 ) 65 pictureBox1.Image = Properties.Resources.光_掘墓; 66 else 67 pictureBox1.Image = Properties.Resources.光_维恩; 68 toolStripStatusLabel1.Text = "Light"; 69 } 70 71 private void Shadow_Click(object sender, EventArgs e) 72 { 73 Fire.Checked = false; 74 Light.Checked = false; 75 Shadow.Checked = true; 76 Ocean.Checked = false; 77 Polar.Checked = false; 78 Crystal.Checked = false; 79 Electric.Checked = false; 80 81 pictureBox1.Image = Properties.Resources.影_剑圣; 82 83 toolStripStatusLabel1.Text = "Shadow"; 84 } 85 86 private void Ocean_Click(object sender, EventArgs e) 87 { 88 Fire.Checked = false; 89 Light.Checked = false; 90 Shadow.Checked = false; 91 Ocean.Checked = true; 92 Polar.Checked = false; 93 Crystal.Checked = false; 94 Electric.Checked = false; 95 Random r = new Random(); 96 int k = r.Next(2); 97 if (k == 0) 98 pictureBox1.Image = Properties.Resources.海洋_吸血鬼; 99 else 100 pictureBox1.Image = Properties.Resources.海洋_辛德拉; 101 toolStripStatusLabel1.Text = "Ocean"; 102 } 103 104 private void Polar_Click(object sender, EventArgs e) 105 { 106 Fire.Checked = false; 107 Light.Checked = false; 108 Shadow.Checked = false; 109 Ocean.Checked = false; 110 Polar.Checked = true; 111 Crystal.Checked = false; 112 Electric.Checked = false; 113 pictureBox1.Image = Properties.Resources.极地_EZ; 114 toolStripStatusLabel1.Text = "Polar"; 115 } 116 117 private void Crystal_Click(object sender, EventArgs e) 118 { 119 Fire.Checked = false; 120 Light.Checked = false; 121 Shadow.Checked = false; 122 Ocean.Checked = false; 123 Polar.Checked = false; 124 Crystal.Checked = true; 125 Electric.Checked = false; 126 pictureBox1.Image = Properties.Resources.水晶_寒冰; 127 toolStripStatusLabel1.Text = "Crystal"; 128 } 129 130 private void Electric_Click(object sender, EventArgs e) 131 { 132 Fire.Checked = false; 133 Light.Checked = false; 134 Shadow.Checked = false; 135 Ocean.Checked = false; 136 Polar.Checked = false; 137 Crystal.Checked = false; 138 Electric.Checked = true; 139 Random r = new Random(); 140 int k = r.Next(2); 141 if (k == 0) 142 pictureBox1.Image = Properties.Resources.拉克丝; 143 else 144 pictureBox1.Image = Properties.Resources.雷霆; 145 toolStripStatusLabel1.Text = "Electric"; 146 } 147 148 private void BackGrand_Customize_Click(object sender, EventArgs e) 149 { 150 //1、创建打开文件对话框:设计视图中通过从工具箱中拖动控件已完成 151 //OpenFileDialog d = new OpenFileDialog(); 152 //2、对话框的初始化设置:可选项 153 openFileDialog1.Title = "对话框的使用";//标题 154 openFileDialog1.InitialDirectory = "d:\\";//设置初始查找目录 155 openFileDialog1.Filter = "图片(*.jpg)|*.jpg|图片(*.jepg)|*.jepg|所有文件(*.*)|*.*";//文件类型筛选器 156 //3、显示对话框 157 DialogResult dr = openFileDialog1.ShowDialog(); 158 //4、根据用户的选择执行后续的操作 159 if (dr == DialogResult.OK) 160 {//用户的设置,选择的文件 161 //不同的对话框,用户的不同设置,通过对话框的相关属性获取 162 //打开文件对话框:FileName 163 pictureBox1.Load(openFileDialog1.FileName); 164 toolStripStatusLabel1.Text = openFileDialog1.FileName; //打开文件对话框中选择的文件名 165 } 166 else 167 { 168 MessageBox.Show("您没有选择任何文件!"); 169 } 170 } 171 172 //提供复用性强的函数代替以上的函数 173 private void Selection(object sender, EventArgs e) 174 { 175 ToolStripMenuItem menu = sender as ToolStripMenuItem; 176 if (menu != null) 177 { 178 Fire.Checked = false; 179 Light.Checked = false; 180 Shadow.Checked = false; 181 Ocean.Checked = false; 182 Polar.Checked = false; 183 Crystal.Checked = false; 184 Electric.Checked = false; 185 BackGrand_Customize.Checked = false; 186 menu.Checked = true;//在单击的菜单项前显示对勾 187 switch (menu.Name) 188 {//2、文本框的前景色 189 case "Fire": 190 pictureBox1.Image = Properties.Resources.火_阿木木; break; 191 case "Light": 192 pictureBox1.Image = Properties.Resources.光_掘墓; break; 193 case "Shadow": 194 pictureBox1.Image = Properties.Resources.影_剑圣; break; 195 case "Ocean": 196 pictureBox1.Image = Properties.Resources.海洋_吸血鬼; break; 197 case "Polar": 198 pictureBox1.Image = Properties.Resources.极地_EZ; break; 199 case "Crystal": 200 pictureBox1.Image = Properties.Resources.水晶_寒冰; break; 201 case "Electric": 202 pictureBox1.Image = Properties.Resources.雷霆; break; 203 default: 204 openFileDialog1.Title = "对话框的使用";//标题 205 openFileDialog1.InitialDirectory = "d:\\";//设置初始查找目录 206 openFileDialog1.Filter = "图片(*.jpg)|*.jpg|图片(*.jepg)|*.jepg|所有文件(*.*)|*.*";//文件类型筛选器 207 //3、显示对话框 208 DialogResult dr = openFileDialog1.ShowDialog(); 209 //4、根据用户的选择执行后续的操作 210 if (dr == DialogResult.OK) 211 { 212 pictureBox1.Load(openFileDialog1.FileName); 213 toolStripStatusLabel1.Text = openFileDialog1.FileName; //打开文件对话框中选择的文件名 214 } 215 else 216 { 217 MessageBox.Show("您没有选择任何文件!"); 218 } 219 break; 220 } 221 toolStripStatusLabel1.Text = menu.Text; 222 } 223 } 224 private void 退出AltshiftEToolStripMenuItem_Click(object sender, EventArgs e) 225 { 226 this.Close(); 227 } 228 229 private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e) 230 { 231 if( toolStripComboBox1.SelectedIndex == 0) 232 { 233 pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; 234 } 235 else if(toolStripComboBox1.SelectedIndex == 1) 236 { 237 pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage; 238 } 239 else if(toolStripComboBox1.SelectedIndex == 2) 240 { 241 pictureBox1.SizeMode = PictureBoxSizeMode.Normal; 242 } 243 } 244 245 } 246 }
链接: https://pan.baidu.com/s/1Uigzg2hrAOjJL_ScycwBkA 提取码: uxbw