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.Windows.Forms; 9 10 //添加以下命名空间 11 using System.IO; 12 using System.Reflection; 13 using System.Resources; 14 namespace 帅的计时器哈哈 15 { 16 public partial class Form1 : Form 17 { 18 private int _nowsecond = 0; 19 //类字段:冒号控制标志 20 private Boolean _colonFlag = true; 21 22 //数码管位图数组 23 private Bitmap[] _bmpShow = new Bitmap[13]; 24 25 //自定义方法:由显示字符获取对应的数码管位图 26 private Image GetResourceImage(string displayStr) 27 { 28 if (displayStr == "0") 29 return _bmpShow[0]; 30 else if (displayStr == "1") 31 return _bmpShow[1]; 32 else if (displayStr == "2") 33 return _bmpShow[2]; 34 else if (displayStr == "3") 35 return _bmpShow[3]; 36 else if (displayStr == "4") 37 return _bmpShow[4]; 38 else if (displayStr == "5") 39 return _bmpShow[5]; 40 else if (displayStr == "6") 41 return _bmpShow[6]; 42 else if (displayStr == "7") 43 return _bmpShow[7]; 44 else if (displayStr == "8") 45 return _bmpShow[8]; 46 else if (displayStr == "9") 47 return _bmpShow[9]; 48 else if (displayStr == ".") 49 return _bmpShow[10]; 50 else if (displayStr == ":") 51 return _bmpShow[11]; 52 else 53 return _bmpShow[12]; 54 } 55 56 public Form1() 57 { 58 InitializeComponent(); 59 //初始化时间计数值(秒) 60 _nowsecond = 0; 61 //装载数码管位图文件(注意:要保证Debug目录下有images子目录及bmp文件,否则会报错) 62 _bmpShow[0] = new Bitmap("images/0.bmp"); 63 _bmpShow[1] = new Bitmap("images/1.bmp"); 64 _bmpShow[2] = new Bitmap("images/2.bmp"); 65 _bmpShow[3] = new Bitmap("images/3.bmp"); 66 _bmpShow[4] = new Bitmap("images/4.bmp"); 67 _bmpShow[5] = new Bitmap("images/5.bmp"); 68 _bmpShow[6] = new Bitmap("images/6.bmp"); 69 _bmpShow[7] = new Bitmap("images/7.bmp"); 70 _bmpShow[8] = new Bitmap("images/8.bmp"); 71 _bmpShow[9] = new Bitmap("images/9.bmp"); 72 _bmpShow[10] = new Bitmap("images/dot1.bmp"); 73 _bmpShow[11] = new Bitmap("images/dot2.bmp"); 74 _bmpShow[12] = new Bitmap("images/blank.bmp"); 75 } 76 77 78 private void label3_Click(object sender, EventArgs e) 79 { 80 81 } 82 83 84 //按钮"开始"的Click事件响应方法 85 private void buttonStart_Click(object sender, EventArgs e) 86 { 87 label2.Visible = false; 88 _nowsecond = 0; 89 //启动定时器 90 timer1.Enabled = true; timer2.Enabled = true; 91 92 //初始化时间计数值 93 labelHour.Text = "00"; 94 labelMinute.Text = "00"; 95 labelSecond.Text = "00"; 96 labelColon1.Visible = true; 97 labelColon2.Visible = true; 98 //启用或禁用有关按钮 99 buttonStart.Enabled = false; buttonPauseContinue.Enabled = true; buttonStop.Enabled = true; 100 //改变按钮文字 101 buttonPauseContinue.Text = "暂停"; 102 buttonStart.BackColor=buttonStart.BackColor == Color.Blue ? Color.Yellow : Color.Blue; 103 //冒号控制标志设置为true 104 _colonFlag = true; 105 //隐藏冒号 106 pictureBoxColon1.Image = GetResourceImage(" "); 107 pictureBoxColon2.Image = GetResourceImage(" "); 108 109 } 110 111 private void timer1_Tick(object sender, EventArgs e) 112 { 113 _nowsecond++; 114 int hour = _nowsecond / 3600; 115 int minute = (_nowsecond % 3600) / 60; 116 int second = (_nowsecond % 3600) % 60; 117 118 if(hour<=9) 119 labelHour.Text="0"+hour.ToString(); 120 else labelHour.Text = hour.ToString(); 121 if(minute<=9) 122 labelMinute.Text = "0" + minute.ToString(); 123 else labelMinute.Text = minute.ToString(); 124 if(second<=9) 125 labelSecond.Text = "0" + second.ToString(); 126 else labelSecond.Text = second.ToString(); 127 //显示当前的秒 128 this.pictureBoxSecond1.Image = GetResourceImage((second / 10).ToString()); 129 this.pictureBoxSecond2.Image = GetResourceImage((second % 10).ToString()); 130 //显示当前的分 131 this.pictureBoxMinute1.Image = GetResourceImage((minute / 10).ToString()); 132 this.pictureBoxMinute2.Image = GetResourceImage((minute % 10).ToString()); 133 //显示当前的小时 134 this.pictureBoxHour1.Image = GetResourceImage((hour / 10).ToString()); 135 this.pictureBoxHour2.Image = GetResourceImage((hour % 10).ToString()); 136 137 } 138 139 private void labelHour_Click(object sender, EventArgs e) 140 { 141 142 } 143 144 private void labelMinute_Click(object sender, EventArgs e) 145 { 146 147 } 148 149 private void labelSecond_Click(object sender, EventArgs e) 150 { 151 152 } 153 154 //Timer2定时器的Tick事件响应方法 155 private void timer2_Tick(object sender, EventArgs e) 156 { 157 //让中间的冒号闪烁起来 158 if (labelColon1.Visible == true) 159 { 160 labelColon1.Visible = false; 161 labelColon2.Visible = false; 162 } 163 else 164 { 165 labelColon1.Visible = true; 166 labelColon2.Visible = true; 167 } 168 //切换冒号显示控制标志 169 _colonFlag = !_colonFlag; 170 171 //判断冒号控制标志是否为true 172 if (_colonFlag == true) 173 { 174 pictureBoxColon1.Image = GetResourceImage(" "); 175 pictureBoxColon2.Image = GetResourceImage(" "); 176 } 177 else 178 { 179 pictureBoxColon1.Image = GetResourceImage(":"); 180 pictureBoxColon2.Image = GetResourceImage(":"); 181 } 182 } 183 184 private void buttonStart_MouseDown(object sender, MouseEventArgs e) 185 { 186 } 187 188 private void buttonStart_KeyPress(object sender, KeyPressEventArgs e) 189 { 190 191 } 192 193 private void Form1_Load(object sender, EventArgs e) 194 { 195 buttonStart.Enabled = true; 196 buttonPauseContinue.Enabled = false; 197 buttonStop.Enabled = false; 198 buttonPauseContinue.Text = "暂停"; 199 labelColon1.Visible = true; 200 labelColon2.Visible = true; 201 label2.Visible = false; 202 //初始化所有ImageBox控件的初始位图 203 pictureBoxHour1.Image = GetResourceImage("0"); 204 pictureBoxHour2.Image = GetResourceImage("0"); 205 pictureBoxMinute1.Image = GetResourceImage("0"); 206 pictureBoxMinute2.Image = GetResourceImage("0"); 207 pictureBoxSecond1.Image = GetResourceImage("0"); 208 pictureBoxSecond2.Image = GetResourceImage("0"); 209 pictureBoxColon1.Image = GetResourceImage(":"); 210 pictureBoxColon2.Image = GetResourceImage(":"); 211 212 } 213 214 private void buttonStop_Click(object sender, EventArgs e) 215 { 216 _nowsecond = 0; 217 timer1.Enabled = false; 218 timer2.Enabled = false; 219 buttonStart.Enabled = true; buttonPauseContinue.Enabled = false; buttonStop.Enabled = false; 220 buttonPauseContinue.Text = "暂停"; 221 labelColon2.Visible = true; 222 labelColon1.Visible = true; 223 buttonStart.BackColor = Color.Yellow; 224 labelHour.Text = "00"; 225 labelMinute.Text = "00"; 226 labelSecond.Text = "00"; 227 label2.Visible = true; 228 //冒号控制标志设置为true 229 _colonFlag = true; 230 //显示冒号 231 pictureBoxColon1.Image = GetResourceImage(":"); 232 pictureBoxColon2.Image = GetResourceImage(":"); 233 234 //清空时间计数值(秒) 235 _nowsecond = 0; 236 pictureBoxHour1.Image = GetResourceImage("0"); 237 pictureBoxHour2.Image = GetResourceImage("0"); 238 pictureBoxMinute1.Image = GetResourceImage("0"); 239 pictureBoxMinute2.Image = GetResourceImage("0"); 240 pictureBoxSecond1.Image = GetResourceImage("0"); 241 pictureBoxSecond2.Image = GetResourceImage("0"); 242 pictureBoxColon1.Image = GetResourceImage(":"); 243 pictureBoxColon2.Image = GetResourceImage(":"); 244 } 245 246 private void buttonPauseContinue_Click(object sender, EventArgs e) 247 { 248 if (buttonPauseContinue.Text== "暂停") 249 { 250 labelColon2.Visible = true; 251 labelColon1.Visible = true; 252 timer1.Enabled = false; 253 timer2.Enabled = false; 254 buttonPauseContinue.Text = "继续"; 255 } 256 else 257 { 258 labelColon2.Visible = true; 259 labelColon1.Visible = true; 260 timer1.Enabled = true; 261 timer2.Enabled = true; 262 buttonPauseContinue.Text = "暂停"; 263 } 264 pictureBoxColon1.Image = GetResourceImage(":"); 265 pictureBoxColon2.Image = GetResourceImage(":"); 266 } 267 268 private void Form1_Move(object sender, EventArgs e) 269 { 270 //停靠屏幕左右边框 271 int screenRight = Screen.PrimaryScreen.Bounds.Right; 272 int formRight = this.Left + this.Size.Width; 273 if (Math.Abs(screenRight - formRight) <= 100) 274 this.Left = screenRight - this.Size.Width; 275 if (Math.Abs(this.Left) <= 100) 276 this.Left = 0; 277 278 //停靠屏幕上下边框 279 int screenBottom = Screen.PrimaryScreen.Bounds.Bottom; 280 int formBottom = this.Top + this.Size.Height; 281 if (Math.Abs(screenBottom - formBottom) <= 60) 282 this.Top = screenBottom - this.Size.Height; 283 if (Math.Abs(this.Top) <= 100) 284 this.Top = 0; 285 } 286 287 private void pictureBox2_Click(object sender, EventArgs e) 288 { 289 290 } 291 292 private void pictureBox5_Click(object sender, EventArgs e) 293 { 294 295 } 296 297 private void pictureBoxHour1_Click(object sender, EventArgs e) 298 { 299 300 } 301 } 302 }