标题:用VB编写简单的时钟程序(有时、分、秒针转动)
只看楼主
griefforyou
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:3336
专家分:0
注 册:2004-4-15
 问题点数:0 回复次数:126 
用VB编写简单的时钟程序(有时、分、秒针转动)
'/////////////////////////////////
'小闹钟示例
'Written By griefforyou
'在窗体中添加一个Timer控件,将Interval设为1000以下。
'////////////////////////////////

Option Explicit


Const PI = 3.1415926
Dim BaseX As Integer, BaseY As Integer, R As Integer
Dim r1 As Integer, r2 As Integer, r3 As Integer

Private Sub Form_Load()
    Me.ScaleMode = 3
    Me.AutoRedraw = True
   
    If Me.Width < 3000 Then Me.Width = 3000
    If Me.Height < 3000 Then Me.Height = 3000
   
End Sub

Private Sub Init()
Dim i As Integer

    BaseX = Me.ScaleWidth / 2
    BaseY = Me.ScaleHeight / 2
   
    R = IIf(BaseX > BaseY, BaseY * 0.8, BaseY * 0.8)
    r1 = R * 0.2
    r2 = R * 0.1
    r3 = R * 0.05

    For i = 0 To 360 Step 6
        
        If i Mod 30 = 0 Then    '时
            Me.DrawWidth = 2
            DrawLine BaseX + (R - 3) * Sin(i * PI / 180), BaseY - (R - 3) * Cos(i * PI / 180), BaseX + (R - 8) * Sin(i * PI / 180), BaseY - (R - 8) * Cos(i * PI / 180), 3
        Else                    '分
            Me.DrawWidth = 2
            Me.PSet (BaseX + (R - 3) * Sin(i * PI / 180), BaseY - (R - 3) * Cos(i * PI / 180))
        End If
    Next
   
    Me.DrawWidth = 1
    Me.Circle (BaseX, BaseY), R
End Sub

'绘制指针
Private Sub DrawClock()
Dim Second As Integer
Dim Minute As Integer
Dim Hours As Integer

    Second = DatePart("s", Time)
    Minute = DatePart("n", Time)
    Hours = DatePart("h", Time)
    If Hours > 12 Then
        Hours = Hours - 12
    End If

    Me.DrawWidth = 1
    Me.Circle (BaseX, BaseY), 4

    DrawLine BaseX - r1 * Sin(Second * PI / 30), BaseY + r1 * Cos(Second * PI / 30), BaseX + (R - 10) * Sin(Second * PI / 30), BaseY - (R - 10) * Cos(Second * PI / 30), 0
    DrawLine BaseX - r2 * Sin(Minute * PI / 30), BaseY + r2 * Cos(Minute * PI / 30), BaseX + R * 0.8 * Sin(Minute * PI / 30), BaseY - R * 0.8 * Cos(Minute * PI / 30), 1
    DrawLine BaseX - r3 * Sin((Hours + Minute / 60) * PI / 6), BaseY + r3 * Cos((Hours + Minute / 60) * PI / 6), BaseX + R * 0.6 * Sin((Hours + Minute / 60) * PI / 6), BaseY - R * 0.6 * Cos((Hours + Minute / 60) * PI / 6), 2
End Sub

'画线函数
Private Sub DrawLine(x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer, Flag As Integer)
Static OldSX1 As Integer, OldSX2 As Integer, OldSY1 As Integer, OldSY2 As Integer
Static OldMX1 As Integer, OldMX2 As Integer, OldMY1 As Integer, OldMY2 As Integer
Static OldHX1 As Integer, OldHX2 As Integer, OldHY1 As Integer, OldHY2 As Integer
    Select Case Flag
        Case 0
            Me.DrawWidth = 1
            Me.Line (OldSX1, OldSY1)-(OldSX2, OldSY2), Me.BackColor
            Me.Line (x1, y1)-(x2, y2)
            OldSX1 = x1
            OldSX2 = x2
            OldSY1 = y1
            OldSY2 = y2
        Case 1
            Me.DrawWidth = 2
            Me.Line (OldMX1, OldMY1)-(OldMX2, OldMY2), Me.BackColor
            Me.Line (x1, y1)-(x2, y2)
            OldMX1 = x1
            OldMX2 = x2
            OldMY1 = y1
            OldMY2 = y2
        Case 2
            Me.DrawWidth = 3
            Me.Line (OldHX1, OldHY1)-(OldHX2, OldHY2), Me.BackColor
            Me.Line (x1, y1)-(x2, y2)
            OldHX1 = x1
            OldHX2 = x2
            OldHY1 = y1
            OldHY2 = y2
        Case Else
            Me.Line (x1, y1)-(x2, y2)
    End Select
End Sub

Private Sub Form_Resize()
    Me.Cls
    Call Init
End Sub

Private Sub Timer1_Timer()
    Call DrawClock
End Sub
搜索更多相关主题的帖子: 时钟 秒针 Integer 
2005-05-13 17:11
ruijian1227
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2005-4-22
得分:0 
斑竹强
2005-05-13 23:40
天际流星
Rank: 1
等 级:新手上路
帖 子:121
专家分:0
注 册:2005-3-18
得分:0 
佩服!!

2005-05-14 12:32
萧凡
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2005-5-11
得分:0 
复制下来了

刀剑无悔生平事 心脾皆无何来愁 相识俊凡平生快 木屋竹萧行九州
2005-05-15 13:43
lingling00
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2005-5-8
得分:0 

我也做了一个时钟,界面是用shape画了一个圆,再画上时针、分针和秒针线。可是出了一个问题,显示时间和系统时间不一致,不知道怎么改?请griefforyou帮我看看。 程序如下: Option Explicit Const PI = 3.14159 Private len_S As Single, len_M As Single, len_H As Single

Private Sub Form_Load() len_S = Sqr((LineS.Y2 - LineS.Y1) ^ 2 + (LineS.X2 - LineS.X1) ^ 2) len_M = Sqr((LineM.Y2 - LineM.Y1) ^ 2 + (LineM.X2 - LineM.X1) ^ 2) len_H = Sqr((LineH.Y2 - LineH.Y1) ^ 2 + (LineH.X2 - LineH.X1) ^ 2) Call Timer1_Timer End Sub

Private Sub Timer1_Timer() Dim s As Single, m As Single, h As Single Form1.Caption = Time s = Second(Time) m = Minute(Time) h = Hour(Time) + m / 60 LineS.X2 = LineS.X1 + len_S * Cos(PI * s / 30) LineS.Y2 = LineS.Y1 + len_S * Sin(PI * s / 30) LineM.X2 = LineM.X1 + len_M * Cos(PI * m / 30) LineM.Y2 = LineM.Y1 + len_M * Sin(PI * m / 30) If h >= 12 Then h = h - 12 LineH.X2 = LineH.X1 + len_H * Cos(PI * h / 6) LineH.Y2 = LineH.Y1 + len_H * Sin(PI * h / 6) End Sub

2005-05-16 18:25
lingling00
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2005-5-8
得分:0 
griefforyou,你是天津红桥区的吗?我是天津河东区的。
2005-05-16 19:40
griefforyou
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:3336
专家分:0
注 册:2004-4-15
得分:0 

把加减号弄反了,Sin Cos也弄反了。。。 我也是河东。 Option Explicit Const PI = 3.14159 Private len_S As Single, len_M As Single, len_H As Single

Private Sub Form_Load() len_S = Sqr((LineS.y2 - LineS.y1) ^ 2 + (LineS.x2 - LineS.x1) ^ 2) len_M = Sqr((LineM.y2 - LineM.y1) ^ 2 + (LineM.x2 - LineM.x1) ^ 2) len_H = Sqr((LineH.y2 - LineH.y1) ^ 2 + (LineH.x2 - LineH.x1) ^ 2) Call Timer1_Timer End Sub

Private Sub Timer1_Timer() Dim s As Single, m As Single, h As Single Form1.Caption = Time s = Second(Time) m = Minute(Time) h = Hour(Time) + m / 60 LineS.x2 = LineS.x1 + len_S * Sin(PI * s / 30) LineS.y2 = LineS.y1 - len_S * Cos(PI * s / 30) LineM.x2 = LineM.x1 + len_M * Sin(PI * m / 30) LineM.y2 = LineM.y1 - len_M * Cos(PI * m / 30) If h >= 12 Then h = h - 12 LineH.x2 = LineH.x1 + len_H * Sin(PI * h / 6) LineH.y2 = LineH.y1 - len_H * Cos(PI * h / 6) End Sub

[此贴子已经被作者于2005-5-18 23:12:59编辑过]


天津网站建设 http://www./
2005-05-18 23:10
黄鹂
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2005-5-16
得分:0 
我复制了下来 但看不了时 分 秒针  为什么啊?

2005-05-19 10:46
天际流星
Rank: 1
等 级:新手上路
帖 子:121
专家分:0
注 册:2005-3-18
得分:0 
指针是画出来的!!
我是红桥区的!!有QQ吗联系呀!!!

[此贴子已经被作者于2005-5-19 11:36:23编辑过]



2005-05-19 11:32
griefforyou
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:3336
专家分:0
注 册:2004-4-15
得分:0 
你添加了一个Timer控件并设置Interval属性为500吗?

天津网站建设 http://www./
2005-05-19 13:05



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-18083-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.390350 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved