浏览
| 提交新的代码片段
| 创建代码包
Display a clock
|
类型:
Sample Code (HOWTO) |
类别:
Games
|
许可证:
GNU General Public License |
语言:
Visual Basic
|
描述:
Copy the declarations and code below and paste directly into your VB project.
|
该代码片段的版本系列:
片段ID |
下载版本 |
提交时间 |
提交人 |
删除 |
4831 | 1.0 | 2003-11-24 23:15 | cbd001 | |
点击"下载版本"来下载该代码片段.
最新版本的代码片段: 1.0
You will need a label (name = label1) somewhere near the bottom and a timer control (name = timer1, interval = 100, enabled = true).
This uses trigonometry, to calculate where the hands should be.
In Declarations:
Option Explicit
Const pi = 3.141592654
Dim MyTime As String
Dim sec As Integer, hr As Integer, Min As Integer
Dim X As Integer, Y As Integer
Dim x1 As Integer, y1 As Integer
Dim xsold As Integer, ysold As Integer
Dim xmold As Integer, ymold As Integer
Dim xhold As Integer, yhold As Integer
Dim Rad
Dim a As Integer
In Form_Load:
xsold = x1 ' This is the old x for the seconds
ysold = y1 ' likewise this is for the mins
xmold = x1
ymold = y1
xhold = x1
yhold = y1
In timer1_Timer()
Dim c1, c2, c3 ' Variant since no As
c1 = RGB(255, 255, 0)
c2 = RGB(0, 0, 255)
c3 = RGB(255, 0, 0)
label1.Caption = Time$
MyTime = Time$
sec = Val(Right(MyTime, 2))
Min = Val(Mid(MyTime, 4, 2))
hr = Val(Left(MyTime, 2))
If hr > 12 Then hr = hr - 12
Call runtime(sec, c1, 5)
If Int(a / 5) = (a / 5) Then Call runtime(Min, c2, 4)
If a = 0 Then Call runtime(hr, c3, 2)
a = a + 1
If a = 29 Then a = 0
Now make this procedure:
Public Sub runtime(value As Variant, colour As Variant, i As Integer)
Circle (x1, y1), 550, RGB(255, 255, 255)
If i = 2 Then
Rad = (30 * value) * (pi / 180)
Else
Rad = (6 * value) * (pi / 180)
End If
X = x1 + (i * 100 * (Sin(Rad)))
Y = y1 - (i * 100 * (Cos(Rad)))
Select Case i
Case 5
frmClock.Line (x1, y1)-(xsold, ysold), RGB(0, 0, 0)
frmClock.Line (x1, y1)-(X, Y), colour
xsold = X
ysold = Y
Case 4
frmClock.Line (x1, y1)-(xmold, ymold), RGB(0, 0, 0)
frmClock.Line (x1, y1)-(X, Y), colour
xmold = X
ymold = Y
Case 2
frmClock.Line (x1, y1)-(xhold, yhold), RGB(0, 0, 0)
frmClock.Line (x1, y1)-(X, Y), colour
xhold = X
yhold = Y
End Select
End Sub
如果您修改了一个代码片段并且觉得很应该让别人共享,您可以把这作为这个代码片段的最新版本提交上来.