vb.net自制时钟的简单介绍
怎样用vb.net作一个指针转动的钟表?(可设置时间日期,有闹铃功能)
你需要会用GDI+,也就是那个System.Drawing命名空间下的类.
创新互联公司客户idc服务中心,提供资阳主机托管、成都服务器、成都主机托管、成都双线服务器等业务的一站式服务。通过各地的服务中心,我们向成都用户提供优质廉价的产品以及开放、透明、稳定、高性价比的服务,资深网络工程师在机房提供7*24小时标准级技术保障。
给你说个思路,设Timer,到时间就用Form.Invalidate()函数重画窗口,在重画窗口的Form_Paint事件下面编写代码得到当前时间,再根据当前时间用GDI+画时钟.
vb.net开发简单的时钟程序??高手救救我!
就
是
这
个
了
!
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()
怎样用VB编写电子时钟?还要带上秒表计时器!
第一步,打开vb
第二部,添加一个计时器timer,一个文字标签label
第三部,输入代码,代码如下
Private Sub Form_Load()
picClock.Move (Me.ScaleWidth - 8 * 210) / 2, (Me.ScaleHeight - 315) / 2, 8 * 210, 315
Dim i
For i = 0 To 7
picClock.PaintPicture picNumber.Picture, i * 210, 0, 210, 315, IIf((i = 2) Or (i = 5), 12, 10) * 210, 0, 210, 315
Next i
End Sub
Private Sub Timer1_Timer()
Dim cNow$, i
cNow = Format(Now, "hh:mm:ss")
For i = 0 To 7
If Mid(cNow, i + 1, 1) ":" Then
picClock.PaintPicture picNumber.Picture, i * 210, 0, 210, 315, Val(Mid(cNow, i + 1, 1)) * 210, 0, 210, 315
End If
Next i
End Sub
望采纳
上面的代码错了,下面是正确代码
Private Sub Form_Load()
Timer1.Enabled = True
Timer1.Interval = 300
End Sub
Private Sub Timer1_Timer()
Label1 = Now
End Sub
VB程序设计怎么做数字时钟
1、添加一个“label控件”命名为label
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
用VB.net做一个时间计时器
'添加一个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
标题名称:vb.net自制时钟的简单介绍
本文路径:http://ybzwz.com/article/hhjjcs.html