Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 14928|回复: 15

[分享] Excel 画动态时钟,模拟钟表

[复制链接]
发表于 2011-1-30 11:19 | 显示全部楼层 |阅读模式
本帖最后由 nothingwmm 于 2011-1-30 10:29 编辑

模拟时针1.jpg 数字时钟.jpg
动态演示部分见:会走动的钟表
http://www.excelpx.com/forum.php?mod=viewthread&tid=157511&fromuid=420007



  1. Option Explicit
  2. Dim NextTick
  3. Sub StartClock()
  4.     UpdateClock
  5. End Sub
  6. Sub StopClock()
  7. ' 取消ontime事件,停止时钟
  8.     On Error Resume Next
  9.     Application.OnTime NextTick, "UpdateClock", , False
  10. End Sub
  11. Sub cbClockType_Click()
  12. '  显示或者隐藏时钟
  13.     With ThisWorkbook.Sheets("Clock")
  14.         If .DrawingObjects("cbClockType").Value = xlOn Then
  15.             .ChartObjects("ClockChart").Visible = True
  16.         Else
  17.             .ChartObjects("ClockChart").Visible = False
  18.         End If
  19.     End With
  20. End Sub
  21. Sub UpdateClock()
  22. '   更新时钟
  23.     Const PI As Double = 3.14159265358979
  24.     Dim Clock As Chart
  25.     Set Clock = ThisWorkbook.Sheets("Clock").ChartObjects("ClockChart").Chart
  26.    
  27.     If Clock.Parent.Visible Then
  28. '  模拟时钟
  29.         Dim CurrentSeries As Series
  30.         Dim s As Series
  31.         Dim x(1 To 2) As Variant
  32.         Dim v(1 To 2) As Variant
  33.    
  34.    '钟表的画法实际上是3条不同粗细的线条,以中心为原点,以不同长度的3条线段演示时针分针和秒针
  35. '     时针
  36.         Set CurrentSeries = Clock.SeriesCollection("HourHand")
  37.         x(1) = 0
  38.         x(2) = 0.5 * Sin((Hour(Time) + (Minute(Time) / 60)) * (2 * PI / 12))
  39.         v(1) = 0
  40.         v(2) = 0.5 * Cos((Hour(Time) + (Minute(Time) / 60)) * (2 * PI / 12))
  41.         CurrentSeries.XValues = x '设置时针的x变量
  42.         CurrentSeries.Values = v '设置时针的y变量
  43.         
  44. '    分针
  45.         Set CurrentSeries = Clock.SeriesCollection("MinuteHand")
  46.         x(1) = 0
  47.         x(2) = 0.8 * Sin((Minute(Time) + (Second(Time) / 60)) * (2 * PI / 60))
  48.         v(1) = 0
  49.         v(2) = 0.8 * Cos((Minute(Time) + (Second(Time) / 60)) * (2 * PI / 60))
  50.         CurrentSeries.XValues = x
  51.         CurrentSeries.Values = v
  52.    
  53. '    秒针
  54.         Set CurrentSeries = Clock.SeriesCollection("SecondHand")
  55.         x(1) = 0
  56.         x(2) = 0.85 * Sin(Second(Time) * (2 * PI / 60))
  57.         v(1) = 0
  58.         v(2) = 0.85 * Cos(Second(Time) * (2 * PI / 60))
  59.         CurrentSeries.XValues = x
  60.         CurrentSeries.Values = v
  61.     Else
  62. '       数字时针
  63.         ThisWorkbook.Sheets("Clock").Range("DigitalClock").Value = CDbl(Time)
  64.     End If
  65.    
  66. '   每秒中更新
  67.     NextTick = Now + TimeValue("00:00:01")
  68.     Application.OnTime NextTick, "UpdateClock"
  69. End Sub
复制代码

钟表及动画.rar

22.88 KB, 下载次数: 71

售价: 10 个金币  [记录]

评分

参与人数 1 +2 收起 理由
AmoKat + 2 赞一个

查看全部评分

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2011-2-1 08:47 | 显示全部楼层
回复

使用道具 举报

发表于 2011-2-5 12:48 | 显示全部楼层
回复

使用道具 举报

发表于 2011-2-9 20:25 | 显示全部楼层
太牛啦!!!!!!!!!
回复

使用道具 举报

发表于 2012-6-1 09:33 | 显示全部楼层
正想学习一下,楼主威武
回复

使用道具 举报

发表于 2012-9-25 09:57 | 显示全部楼层
楼主太及时啦。女友说如果我做不出来就不要我啦、、、、
回复

使用道具 举报

发表于 2015-4-22 23:16 | 显示全部楼层
认真学习下
回复

使用道具 举报

发表于 2015-4-22 23:16 | 显示全部楼层
再看看能否能用
回复

使用道具 举报

发表于 2016-11-24 10:07 | 显示全部楼层
想要試試看,看是不是可以用
回复

使用道具 举报

发表于 2016-11-24 12:16 | 显示全部楼层
牛逼啊  学习
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-3-29 19:21 , Processed in 1.018879 second(s), 15 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表