1、添加一個“label控件”命名為label
創新互聯長期為成百上千家客戶提供的網站建設服務,團隊從業經驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯網生態環境。為武侯企業提供專業的網站設計、做網站,武侯網站改版等技術服務。擁有10余年豐富建站經驗和眾多成功案例,為您定制開發。
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
就
是
這
個
了
!
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
‘使用矩陣實現路徑(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
為了節省篇幅,上面的代碼省略了引入命名空間的語句。
下面是分針(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方法取得系統當前時間,并旋轉時鐘指針。
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
對所有的指針旋轉的方法都是相同的,因此在基類中實現。由于時針和秒針的實現與分針相似,所不同者,只在于構造器中繪制的指針路徑不同和Transform方法中轉動的角度不同,在這里就不在贅述了。
另外還需要提一下的是畫時鐘表面的代碼,時鐘表面用ClockFace類來實現。這個類首先畫一個圓代表時鐘,然后畫上米老鼠的圖案,最后在相應的位置畫上數字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
數字在時鐘上的位置是用sin和cos函數計算的:
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()
Private?Sub?Form_Load()
Me.Caption?=?"數字時鐘"
Timer1.Interval?=?1000
Timer1.Enabled?=?True
End?Sub
Private?Sub?Timer1_Timer()
Label1.Caption?=?Format(Now,?"yyyy年m月d日")
Label2.Caption?=?Format(Now,?"hh時nn分ss秒")
End?Sub
網站標題:關于vb點虐
設計數字時鐘的信息
鏈接URL:http://vcdvsql.cn/article10/ddihpgo.html
成都網站建設公司_創新互聯,為您提供定制網站、網站改版、域名注冊、企業建站、建站公司、動態網站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯