VB可使用Timer控件、Line控件和繪圖或加載圖片等制作指針式時鐘。
公司專注于為企業(yè)提供成都網(wǎng)站建設、網(wǎng)站制作、微信公眾號開發(fā)、電子商務商城網(wǎng)站建設,微信小程序定制開發(fā),軟件定制制作等一站式互聯(lián)網(wǎng)企業(yè)服務。憑借多年豐富的經驗,我們會仔細了解各客戶的需求而做出多方面的分析、設計、整合,為客戶設計出具風格及創(chuàng)意性的商業(yè)解決方案,創(chuàng)新互聯(lián)建站更提供一系列網(wǎng)站制作和網(wǎng)站推廣的服務。
Timer 控件,通過引發(fā) Timer 事件,Timer 控件可以有規(guī)律地隔一段時間執(zhí)行一次代碼。
Line 控件,Line 控件是圖形控件,它顯示水平線、垂直線或者對角線。
運行時不能使用 Move 方法移動 Line 控件,但是可以通過改變 X1、X2、Y1 和 Y2
屬性來移動它或者調整它的大小。
Circle 方法,在對象上畫圓、橢圓或弧。
以下是通過加載圖片的指針式時鐘代碼:
Option?Explicit
Private?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
'添加一個label標簽名字label1 用來顯示時間
'再添加一個timer控件 名字timer1 interval屬性=1000 用來計時
'窗體添加代碼
Dim t As Date '用來記錄時間
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles Timer1.Tick
t = t.AddSeconds(1)
Label1.Text = "登錄時間:" t.TimeOfDay.ToString
End Sub
你需要會用GDI+,也就是那個System.Drawing命名空間下的類.
給你說個思路,設Timer,到時間就用Form.Invalidate()函數(shù)重畫窗口,在重畫窗口的Form_Paint事件下面編寫代碼得到當前時間,再根據(jù)當前時間用GDI+畫時鐘.
就
是
這
個
了
!
Hand類的代碼:
Public MustInherit Class Hand
Protected gp As GraphicsPath = New GraphicsPath()
Protected gpBase As GraphicsPath = Nothing
Protected midX As Integer = 150 ‘默認的窗體
Protected midY As Integer = 150 ‘中心位置
‘構造器,得到窗體中心位置
Public Sub New(ByVal theForm As Form1)
midX = (theForm.ClientRectangle.Left + theForm.ClientRectangle.Right) / 2
midY = (theForm.ClientRectangle.Top + theForm.ClientRectangle.Bottom) / 2
End Sub
MustOverride Sub Transform(ByVal d As DateTime)
‘繪制指針路徑
Overridable Sub Draw(ByVal g As Graphics)
Dim aPen As Pen = New Pen(Brushes.Black, 4F)
g.DrawPath(aPen, gp)
g.FillPath(Brushes.Black, gp)
aPen.Dispose()
End Sub
‘使用矩陣實現(xiàn)路徑(gp)的旋轉
Public Sub Rotate(ByVal angle As Double)
gp = CType(gpBase.Clone(), GraphicsPath)
Dim mTransform As Matrix = New Matrix()
mTransform.RotateAt(CType(angle,Single),NewPointF(midX,midY))
gp.Transform(mTransform)
End Sub
End Class
為了節(jié)省篇幅,上面的代碼省略了引入命名空間的語句。
下面是分針(MinuteHand)類的定義:
Public Class MinuteHand
Inherits Hand
‘構造器,生成繪制分針的路徑(gp)
Public Sub New(ByVal myForm As Form1)
MyBase.New(myForm)
gp.AddLine(midX, midY, midX, 45)
gp.AddLine(midX, 45, midX - 3, 50)
gp.AddLine(midX - 3, 50, midX + 3, 50)
gp.AddLine(midX + 3, 50, midX, 45)
gpBase = CType(gp.Clone(), GraphicsPath)
End Sub
‘Transform方法取得系統(tǒng)當前時間,并旋轉時鐘指針。
Public Overrides Sub Transform(ByVal d As DateTime)
Dim minuteTime As Double = (CDbl(d.Minute) + CDbl(d.Second / 60))
Dim angle As Double = (CDbl(minuteTime) / 60) * 360
Rotate(angle)
End Sub
End Class
對所有的指針旋轉的方法都是相同的,因此在基類中實現(xiàn)。由于時針和秒針的實現(xiàn)與分針相似,所不同者,只在于構造器中繪制的指針路徑不同和Transform方法中轉動的角度不同,在這里就不在贅述了。
另外還需要提一下的是畫時鐘表面的代碼,時鐘表面用ClockFace類來實現(xiàn)。這個類首先畫一個圓代表時鐘,然后畫上米老鼠的圖案,最后在相應的位置畫上數(shù)字1~12代表12個小時。
Public Sub Draw(ByVal g As Graphics)
DrawClockFace(g)
DrawImage(g)
DrawNumbers(g)
DrawPin(g)
End Sub
下面是ClockFace類的屬性:
Private ClockRectangle As Rectangle
Private ClockFont As Font = New Font("Arial", 12)
Private midPoint As Point
Private ClockImage As Bitmap
Private Const IMAGEX As Integer = 50
Private Const IMAGEY As Integer = 50
DrawClockFace方法用來畫時鐘表面:
Private Sub DrawClockFace(ByVal g As Graphics)
g.FillEllipse(Brushes.White, ClockRectangle.Left + 10, ClockRectangle.Top + 10, ClockRectangle.Width - 20, ClockRectangle.Height - 20)
g.DrawEllipse(Pens.Black, ClockRectangle.Left + 10, ClockRectangle.Top + 10, ClockRectangle.Width - 20, ClockRectangle.Height - 20)
End Sub
然后用Graphics對象的DrawImage方法畫出米老鼠的圖片:
Private Sub DrawImage(ByVal g As Graphics)
Dim nWidth As Integer = ClockImage.Width
Dim nHeight As Integer = ClockImage.Height
Dim destRect As Rectangle = New Rectangle(midPoint.X - IMAGEX / 2, midPoint.Y - IMAGEY / 2, IMAGEX, IMAGEY)
g.DrawImage(ClockImage, destRect)
End Sub
數(shù)字在時鐘上的位置是用sin和cos函數(shù)計算的:
Private Sub DrawNumbers(ByVal g As Graphics)
Dim count As Integer = 1
Dim a As Double
For a = 0 To 2 * Math.PI Step 2 * Math.PI / 12
Dim x As Double = (ClockRectangle.Width - 70) / 2 * Math.Cos(a - Math.PI / 3) + (ClockRectangle.Width - 70) / 2 + 25
Dim y As Double = (ClockRectangle.Width - 70) / 2 * Math.Sin(a - Math.PI / 3) + (ClockRectangle.Width - 70) / 2 + 20
g.DrawString(Convert.ToString(count), ClockFont, Brushes.Black, CType(x, Single), CType(y, Single), New StringFormat())
count += 1
Next
End Sub
最后是窗體文件(Form1.vb):
Public Class Form1
Inherits System.Windows.Forms.Form
Private MyMinuteHand As MinuteHand
Private MyHourHand As HourHand
Private MySecondHand As SecondHand
Private TheClockFace As ClockFace
Private FirstTick As Boolean = False
‘在窗體的OnPaint事件中取得Graphics對象
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
If (FirstTick = False) Then Exit Sub
Dim g As Graphics = e.Graphics
TheClockFace.Draw(g)
MyHourHand.Draw(g)
MyMinuteHand.Draw(g)
MySecondHand.Draw(g)
TheClockFace.DrawPin(g)
End Sub
‘計時器事件
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
MySecondHand.Transform(DateTime.Now)
MyHourHand.Transform(DateTime.Now)
MyMinuteHand.Transform(DateTime.Now)
FirstTick = True
Invalidate()