|
本帖最后由 nothingwmm 于 2011-1-30 10:29 编辑
动态演示部分见:会走动的钟表
http://www.excelpx.com/forum.php?mod=viewthread&tid=157511&fromuid=420007
- Option Explicit
- Dim NextTick
- Sub StartClock()
- UpdateClock
- End Sub
- Sub StopClock()
- ' 取消ontime事件,停止时钟
- On Error Resume Next
- Application.OnTime NextTick, "UpdateClock", , False
- End Sub
- Sub cbClockType_Click()
- ' 显示或者隐藏时钟
- With ThisWorkbook.Sheets("Clock")
- If .DrawingObjects("cbClockType").Value = xlOn Then
- .ChartObjects("ClockChart").Visible = True
- Else
- .ChartObjects("ClockChart").Visible = False
- End If
- End With
- End Sub
- Sub UpdateClock()
- ' 更新时钟
- Const PI As Double = 3.14159265358979
- Dim Clock As Chart
- Set Clock = ThisWorkbook.Sheets("Clock").ChartObjects("ClockChart").Chart
-
- If Clock.Parent.Visible Then
- ' 模拟时钟
- Dim CurrentSeries As Series
- Dim s As Series
- Dim x(1 To 2) As Variant
- Dim v(1 To 2) As Variant
-
- '钟表的画法实际上是3条不同粗细的线条,以中心为原点,以不同长度的3条线段演示时针分针和秒针
- ' 时针
- Set CurrentSeries = Clock.SeriesCollection("HourHand")
- x(1) = 0
- x(2) = 0.5 * Sin((Hour(Time) + (Minute(Time) / 60)) * (2 * PI / 12))
- v(1) = 0
- v(2) = 0.5 * Cos((Hour(Time) + (Minute(Time) / 60)) * (2 * PI / 12))
- CurrentSeries.XValues = x '设置时针的x变量
- CurrentSeries.Values = v '设置时针的y变量
-
- ' 分针
- Set CurrentSeries = Clock.SeriesCollection("MinuteHand")
- x(1) = 0
- x(2) = 0.8 * Sin((Minute(Time) + (Second(Time) / 60)) * (2 * PI / 60))
- v(1) = 0
- v(2) = 0.8 * Cos((Minute(Time) + (Second(Time) / 60)) * (2 * PI / 60))
- CurrentSeries.XValues = x
- CurrentSeries.Values = v
-
- ' 秒针
- Set CurrentSeries = Clock.SeriesCollection("SecondHand")
- x(1) = 0
- x(2) = 0.85 * Sin(Second(Time) * (2 * PI / 60))
- v(1) = 0
- v(2) = 0.85 * Cos(Second(Time) * (2 * PI / 60))
- CurrentSeries.XValues = x
- CurrentSeries.Values = v
- Else
- ' 数字时针
- ThisWorkbook.Sheets("Clock").Range("DigitalClock").Value = CDbl(Time)
- End If
-
- ' 每秒中更新
- NextTick = Now + TimeValue("00:00:01")
- Application.OnTime NextTick, "UpdateClock"
- End Sub
复制代码
|
评分
-
查看全部评分
|