今天用C#做了一个漂亮的数字时钟。界面如下。

实现技术:主要是通过Graphics类的DrawImage方法来绘制数字时钟中所有的数字,这些数字是从网上找的一些图片文件。时钟使用DateTime中Now属性来获得不同的,时,分,秒,最后通过定时器来实现时钟的运行状态。

Main Code:

  1. //将0~9数字图片保存在Image数组中
  2. private Image[] image = new Bitmap[10];
  3. public Form1()
  4. {
  5. InitializeComponent();
  6. for (int i = 0; i < 10;i++ )
  7. {
  8. image[i] = new Bitmap(@"D:/编程/C#/数字时钟/数字时钟/Resources/"+i.ToString()+".jpg");
  9. }
  10. }
  1. private void Form1_Paint(object sender, PaintEventArgs e)
  2. {
  3. Graphics g = e.Graphics;
  4. int hh = DateTime.Now.Hour;                       //取得小时数字
  5. int hh1 = hh / 10;
  6. int hh2 = hh % 10;
  7. g.DrawImage(image[hh1], 20, 20, 80, 180);
  8. g.DrawImage(image[hh2], 100, 20, 80, 180);
  9. int mm = DateTime.Now.Minute;                      //取得分钟数字
  10. int mm1 = mm / 10;
  11. int mm2 = mm % 10;
  12. g.DrawImage(image[mm1], 260, 20, 80, 180);
  13. g.DrawImage(image[mm2], 340, 20, 80, 180);
  14. int ss = DateTime.Now.Second;                       //取得秒数字
  15. int ss1 = ss / 10;
  16. int ss2 = ss % 10;
  17. g.DrawImage(image[ss1], 500, 20, 80, 180);
  18. g.DrawImage(image[ss2], 580, 20, 80, 180);
  19. }
  20. private void timer1_Tick(object sender, EventArgs e)  //对窗体进行重绘
  21. {
  22. this.Invalidate();
  23. }

另外,需要将Timer的Interval属性设为1000mm,Enable设置为True!

最新文章

  1. JDBC连接数据库
  2. 纯CSS3制作超级漂亮又实用的加载小图标
  3. 复制SharePoint列表项(SPListItem)到另一个列表
  4. ligerui_ligerTree_005_动态增加“树”节点
  5. linux中ll和du的区别
  6. Machine Learning 学习笔记 (1) —— 线性回归与逻辑回归
  7. BFM1
  8. QTP关于AOM的Javascript启动方式
  9. R实战读书笔记四
  10. Inspinia_admin-V2.3原版(英文)
  11. alpha冲刺第五天
  12. nyoj 黑色帽子
  13. redis 系列6 数据结构之字典(下)
  14. 在Linux下使用gcc编译mesa文件报undefined reference to symbol &#39;sin@@GLIBC_2.2.5和DSO missing from command line两个错误的解决方案
  15. 软件包管理之rpm与yum
  16. C#基础巩固(3)-Linq To XML 读取XML
  17. pymysql 模块 使用目录
  18. node微信公众号开发---自动回复
  19. Linux添加防火墙、iptables的安装和配置(亲测)
  20. 解决python2安装MySQL-python模块报错

热门文章

  1. Eclipse恢复初始界面&amp;打开视图
  2. ESP8266开发课堂之 - 建立一个新项目
  3. WTL 中CComboBoxEx显示不了的问题
  4. Linux用户级线程和内核级线程区别
  5. mysql 乱码问题处理
  6. opengl打开本地bmp图片绘制
  7. IOS中如何判断APP是否安装后首次运行或升级后首次运行
  8. C#操作mongodb数据库
  9. Tesseract 3.02中文字库训练
  10. CSS 元素透明