'添加一個(gè)label標(biāo)簽名字label1 用來(lái)顯示時(shí)間
專注于為中小企業(yè)提供網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)沾益免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了成百上千企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
'再添加一個(gè)timer控件 名字timer1 interval屬性=1000 用來(lái)計(jì)時(shí)
'窗體添加代碼
Dim t As Date '用來(lái)記錄時(shí)間
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles Timer1.Tick
t = t.AddSeconds(1)
Label1.Text = "登錄時(shí)間:" t.TimeOfDay.ToString
End Sub
就
是
這
個(gè)
了
!
Hand類的代碼:
Public MustInherit Class Hand
Protected gp As GraphicsPath = New GraphicsPath()
Protected gpBase As GraphicsPath = Nothing
Protected midX As Integer = 150 ‘默認(rèn)的窗體
Protected midY As Integer = 150 ‘中心位置
‘構(gòu)造器,得到窗體中心位置
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
‘使用矩陣實(shí)現(xiàn)路徑(gp)的旋轉(zhuǎn)
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é)省篇幅,上面的代碼省略了引入命名空間的語(yǔ)句。
下面是分針(MinuteHand)類的定義:
Public Class MinuteHand
Inherits Hand
‘構(gòu)造器,生成繪制分針的路徑(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)當(dāng)前時(shí)間,并旋轉(zhuǎn)時(shí)鐘指針。
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
對(duì)所有的指針旋轉(zhuǎn)的方法都是相同的,因此在基類中實(shí)現(xiàn)。由于時(shí)針和秒針的實(shí)現(xiàn)與分針相似,所不同者,只在于構(gòu)造器中繪制的指針路徑不同和Transform方法中轉(zhuǎn)動(dòng)的角度不同,在這里就不在贅述了。
另外還需要提一下的是畫(huà)時(shí)鐘表面的代碼,時(shí)鐘表面用ClockFace類來(lái)實(shí)現(xiàn)。這個(gè)類首先畫(huà)一個(gè)圓代表時(shí)鐘,然后畫(huà)上米老鼠的圖案,最后在相應(yīng)的位置畫(huà)上數(shù)字1~12代表12個(gè)小時(shí)。
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方法用來(lái)畫(huà)時(shí)鐘表面:
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對(duì)象的DrawImage方法畫(huà)出米老鼠的圖片:
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ù)字在時(shí)鐘上的位置是用sin和cos函數(shù)計(jì)算的:
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對(duì)象
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
‘計(jì)時(shí)器事件
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()
定時(shí)器中處理
Private?Sub?Form1_Load(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?MyBase.Load
Timer1.Interval?=?10?'10毫秒刷新一次
Timer1.Start()?'開(kāi)啟定時(shí)器
End?Sub
Private?Sub?Timer1_Tick(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?Timer1.Tick
Label1.Text?=?Now
End?Sub
將時(shí)間轉(zhuǎn)換為指定的字符串格式用:Now.ToString("yyyyMMddHHmmss")就Ok了。
例如:年份用小寫(xiě)yyyy(大寫(xiě)不識(shí)別),大寫(xiě)MM識(shí)別為月份,小寫(xiě)mm識(shí)別為分鐘,日期天為大寫(xiě)DD(小寫(xiě)不識(shí)別),
小時(shí)大寫(xiě)HH識(shí)別為24小時(shí)制,小寫(xiě)hh識(shí)別為12小時(shí)制,秒用小寫(xiě)ss(大寫(xiě)不識(shí)別)
補(bǔ)充:Visual Basic.NET是基于微軟.NET Framework之上的面向?qū)ο蟮木幊陶Z(yǔ)言。
其在調(diào)試時(shí)是以解釋型語(yǔ)言方式運(yùn)作,而輸出為EXE程序是是以編譯型語(yǔ)言方式運(yùn)作。
可以看作是Visual Basic在.Net Framework平臺(tái)上的升級(jí)版本,增強(qiáng)了對(duì)面向?qū)ο蟮闹С帧4蠖嗟腣B.Net程序員使用Visual Studio .Net作為IDE(integrated development environment).SharpDevelop是另一種可用的開(kāi)源的IDE。
VB.Net需要在.Net Framework平臺(tái)上才能執(zhí)行。