VB可使用Timer控件、Line控件和绘图或加载图片等制作指针式时钟。
Timer 控件,通过引发 Timer 事件,Timer 控件可以有规律地隔一段时间执行一次代码。
Line 控件,Line 控件是图形控件,它显示水平线、垂直线或者对角线。
运行时不能使用 Move 方法移动 Line 控件,但是李团可以通过改变 X1、X2、Y1 和 Y2
属性来移动它或者调整它的大小。
Circle 方法,在对象上画圆、椭圆或弧。
以下是通过加载图片的指针式时钟代码:
Option ExplicitPrivate Const PI = 3.1415926
Dim X(1) As Single, Y(1) As Single
Dim OriAngle As Single, DestAngle As Single, r 铅做As Single
Private Sub RotateLine(objL As Line, bsPointX As Single, bsPointY As Single, RotateAngle As Single)
With objL
X(0) = .X1
Y(0) = .Y1
X(1) = .X2
Y(1) = .Y2
End With
Dim i As Integer
For i = 0 To 1
If X(i) - bsPointX <> 0 Then
OriAngle = Atn((Y(i) - bsPointY) / (X(i) - bsPointX))
Else
OriAngle = IIf(Y(i) > bsPointY, PI / 2, 1.5 * PI)
End If
If X(i) - bsPointX < 0 Then
If OriAngle < 0 Then
OriAngle = PI - Abs(OriAngle)
Else
OriAngle = PI + Abs(OriAngle)
End If
End If
DestAngle = OriAngle + RotateAngle
r = Sqr((X(i) - bsPointX) ^ 2 + (Y(i) - bsPointY) ^ 2)
X(i) = bsPointX + r * Cos(DestAngle)
Y(i) = bsPointY + r * Sin(DestAngle)
Next i
With objL
.X1 = X(0)
.Y1 = 槐扰衡Y(0)
.X2 = X(1)
.Y2 = Y(1)
End With
End Sub
Private Sub Form_Load()
Timer1.Interval = 1000
Dim i As Long
For i = 1 To Val(Mid(Time$, 7, 2))
RotateLine Line3, Line3.X1, Line3.Y1, 1 * PI / 30
Next
For i = 1 To Val(Mid(Time$, 4, 2))
RotateLine Line2, Line2.X1, Line2.Y1, 1 * PI / 30
Next
For i = 1 To Val(Mid(Time$, 1, 2)) * 5 '对时针
RotateLine Line1, Line1.X1, Line1.Y1, 1 * PI / 30
Next
For i = 1 To Val(Mid(Time$, 4, 2)) '对时针
RotateLine Line1, Line1.X1, Line1.Y1, 1 * PI / 360
Next
End Sub
Private Sub Timer1_Timer()
RotateLine Line3, Line3.X1, Line3.Y1, 1 * PI / 30
RotateLine Line2, Line2.X1, Line2.Y1, PI / 1800
If Mid(Time$, 7, 2) = "00" Or Mid(Time$, 7, 2) = "30" Then
RotateLine Line1, Line1.X1, Line1.Y1, 1 * PI / 720
End If
Me.Caption = Time$
End Sub
以前写过一个,如果不想看下面的代码就直接跳到最后吧。猜颤'添加计时器tmr,图片容器pic,然后添加如下穗培败代码:中毁Public originX, originY As DoublePublic i As DoublePublic s, m, h As IntegerConst stepN As Double = 6 * 3.141592627 / 180 '这是后面要用到的步长Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As LongConst HWND_TOPMOST = -1Const SWP_SHOWWINDOW = &H40'窗体载入时就画好时钟界面,包括时针、分针、秒针Private Sub Form_Load()Dim angleN As DoubleDim counts As Integer'originX,originY为时钟中心originX = pic.Width / 2originY = pic.Height / 2'画两个圆表示时钟的外边缘pic.Circle (originX, originY), 1400pic.Circle (originX, originY), 1500'画时钟中心pic.Circle (originX, originY), 40pic.Circle (originX, originY), 70pic.Circle (originX, originY), 100For angleN = 0 To 3.134952627 * 2 Step stepNIf counts Mod 5 = 0 Thenpic.Line (originX + 1200 * Cos(angleN), originY + 1200 * Sin(angleN))- _ (originX + 1400 * Cos(angleN), originY + 1400 * Sin(angleN)), vbRedElsepic.Line (originX + 1300 * Cos(angleN), originY + 1300 * Sin(angleN))- _ (originX + 1400 * Cos(angleN), originY + 1400 * Sin(angleN)), vbBlueEnd Ifcounts = counts + 1NextangleN = ((Hour(Time) Mod 12) * 5 + Int(Minute(Time) / 12) - 15) * stepN '借用一下上面的变量angleNpic.Line (originX, originY)- _ (originX + 800 * Cos(angleN), originY + 800 * Sin(angleN)), vbMagenta '画时针angleN = (Minute(Time) - 15) * stepN '同样继续借用一下上面的变量angleNpic.Line (originX, originY)- _ (originX + 1000 * Cos(angleN), originY + 1000 * Sin(angleN)), vbCyan '画分针Dim retValue As Long'设置为顶层窗口retValue = SetWindowPos(Me.hwnd, HWND_TOPMOST, 250, 250, 310, 270, SWP_SHOWWINDOW)End SubPrivate Sub tmr_Timer()Dim currentS, currentM, currentH As DoubleDim compareH, compareM As IntegerDim status As Integer '后面将会发现status是一个重要的变量,它保证了时针每次只重画一次Dim ThreeLineToOne As IntegercurrentS = Second(Time)currentM = Minute(Time)currentH = Hour(Time)If currentS s Then'////////////////////////////////'// 首先进位按照时、分、秒画 //'////////////////////////////////'清除上一刻度i = (Second(Time) - 15) * stepNpic.Line (originX, originY)- _ (originX + 1200 * Cos(i - stepN), originY + 1200 * Sin(i - stepN)), vbButtonFace '重画上一秒针'分针过了12分钟,时针进位i = ((Hour(Time) Mod 12) * 5 + Int(Minute(Time) / 12) - 15) * stepNIf Minute(Time) Mod 12 = 0 And Second(Time) = 0 Thenpic.Line (originX, originY)- _ (originX + 800 * Cos(i - stepN), originY + 800 * Sin(i - stepN)), vbButtonFace '先重画pic.Line (originX, originY)- _ (originX + 800 * Cos(i), originY + 800 * Sin(i)), vbMagenta '画新时针End If'秒针为0时,分针进位i = (Minute(Time) - 15) * stepNIf Second(Time) = 0 Thenpic.Line (originX, originY)- _ (originX + 1000 * Cos(i - stepN), originY + 1000 * Sin(i - stepN)), vbButtonFace '先重画pic.Line (originX, originY)- _ (originX + 1000 * Cos(i), originY + 1000 * Sin(i)), vbCyan '画新分针status = 1 'status置1作为后面时针是否重画的一个条件End If'画新秒i = (Second(Time) - 15) * stepNpic.Line (originX, originY)- _ (originX + 1200 * Cos(i), originY + 1200 * Sin(i)), vbYellow '画新秒针'////////////////////////////////////'// 接下来是处理覆盖时重画的问题 //'//////////////////////////////////// '分针盖住时针则重画一下时针(除了在中午三线合一的时候)ThreeLineToOne = (currentS = 0 And currentM = 1 And currentH Mod 12 = 0) '返回0则3线未合1,否则三线合一i = ((Hour(Time) Mod 12) * 5 + Int(Minute(Time) / 12) - 15) * stepNcompareH = (Hour(Time) Mod 12) * 5 + Int(Minute(Time) / 12) '设置compareH为时针刻度比较的量If Minute(Time) = compareH + 1 And status And Not ThreeLineToOne Thenpic.Line (originX, originY)- _ (originX + 800 * Cos(i), originY + 800 * Sin(i)), vbMagentastatus = 0End If'秒针盖住时针则再重画一下时针i = ((Hour(Time) Mod 12) * 5 + Int(Minute(Time) / 12) - 15) * stepNcompareH = (Hour(Time) Mod 12) * 5 + Int(Minute(Time) / 12) '设置compareH为时针刻度比较的量If currentS = compareH + 1 Thenpic.Line (originX, originY)- _ (originX + 800 * Cos(i), originY + 800 * Sin(i)), vbMagentaEnd If'秒针盖住分针则再重画一下分针i = (Minute(Time) - 15) * stepNcompareM = currentM '设置compareM为分针刻度比较的量If currentS = compareM + 1 Thenpic.Line (originX, originY)- _ (originX + 1000 * Cos(i), originY + 1000 * Sin(i)), vbCyanEnd If '////////////////////////'// 最后重画时钟中心 //'////////////////////////pic.Circle (originX, originY), 40pic.Circle (originX, originY), 70pic.Circle (originX, originY), 100s = currentS 's作为比较的变量,每次都随当前时间增加,但需放在最后End IfEnd Sub'图片演示:''源码下载:'http://www.ruiyuan-power.com/sfw/Clock.rar
1、添加一个“label控件”命名为label
2、添加一个“timer控件”命名为timer1
3、设置“timer1”的“Interval属性”为1000
使用到的代码:
Dim Hour As Integer '小时Dim Min As Integer '分钟
Dim Sec As Integer '秒
Private Sub Form_Load()
Hour = 0
Min = 0
Sec = 0
Label1.Caption = "00 : 00 : 00"
End Sub
Private Sub Timer1_Timer()
Dim strHour As String
Dim strMin As String
Dim strSec As String
Sec = Sec + 1
If Sec >= 60 Then
Sec = 0
Min = Min + 1
If Min >= 60 Then
友袭 Min = 0
绝告乎 Hour = Hour + 1
If Hour >= 24 Then
Hour = 0
End If
End If
End If
If Hour < 10 Then
strHour = "0" & Hour
Else
strHour = Hour
End If
If Min < 10 Then
strMin = "0" & Min
Else
strMin = Min
End If
If Sec < 10 Then
strSec = "0" & Sec
Else
strSec = Sec
End If
Label1.Caption = strHour & " : " & strMin &并悉 " : " & strSec
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)