我们在项目中,有时需要定时执行某些功能,比如每天定时备份数据,或处理一些日终程序等。这里我主要是就VB中的Timer控件来介绍下。Timer控件的最重要的一个属性就是时间间隔属性(Interval)。它是以ms(微秒)为单位,范围0~65535。
这里有个时间的例子,如下:
主要用到的控件有PictureBox,名称属性设置为“PicTime”,还有三个文本框,分别为TxtYear,TextMth,TextDay,还有个用来显示时间的Label,名称为LblTime。布局如下:
定义全局变量:
Dim day,year,month,ddate,ttime As String
Dim alf(0 To 11)
Dim rr
Dim nHourLen,nMinLen,nSecLen As Integer
Const Pi = 3.1415926
设定Timer间隔时间为1秒:
Private Sub Form_Load()
Timer1.Interval = 1000
End Sub
Timer事件:
Private Sub Timer1_Timer()
rr = PicTime.Height / 2
ddate = Format(Now,"mm:dd:yy")
ttime = Format(Now,"hh:mm:ss")
month = Left(ddate,2)
day = Mid(ddate,4,2)
year = Right(ddate,2)
hh = Left(ttime,2)
mm = Mid(ttime,2)
ss = Right(ttime,2)
TxtYear.Text = "20" & year
TxtMth.Text = month
TxtDay.Text = day
LblTime.Caption = Time
nWidth = PicTime.Width - 40
nHourLen = nWidth * 4 / 18
nMinLen = nWidth * 6 / 18
nSecLen = nWidth * 8 / 18
alfsec = ((ss - 15) / 30) * Pi
alfmin = ((mm + ss / 60 - 15) / 30) * Pi
alfhour = ((hh + mm / 60 + ss / 3600 - 15) / 6) * Pi
PicTime.Refresh
For I = 0 To 11
alf(I) = I * 30 * Pi / 180
PicTime.DrawWidth = 1
If I = 0 Or I = 3 Or I = 6 Or I = 9 Then
PicTime.DrawWidth = 3
End If
PicTime.Line (rr + (rr - 100) * Cos(alf(I)),rr + (rr - 100) * Sin(alf(I)))-(rr + (rr - 300) * Cos(alf(I)),rr + (rr - 300) * Sin(alf(I))),RGB(255,255)
Next I
PicTime.DrawWidth = 3
PicTime.Line (rr,rr)-(rr + nHourLen * Cos(alfhour),rr + nHourLen * Sin(alfhour))
PicTime.DrawWidth = 2
PicTime.Line (rr,rr)-(rr + nMinLen * Cos(alfmin),rr + nMinLen * Sin(alfmin))
PicTime.DrawWidth = 1
PicTime.Line (rr,rr)-(rr + nSecLen * Cos(alfsec),rr + nSecLen * Sin(alfsec))
PicTime.DrawWidth = 5
PicTime.PSet (rr,rr),255)
End Sub
运行结果如下:
参考:《Visual Basic从入门到精通》
原文链接:https://www.f2er.com/vb/261684.html