用VB.net做一个时间计时器'添加一个label标签名字label1 用来显示时间
'再添加一个timer控件 名字timer1interval属性=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开发简单的时钟程序??高手救救我!就
是
这
个
了
!
Hand类的代码vb.net实现小时钟:
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.LefttheForm.ClientRectangle.Right) / 2
midY = (theForm.ClientRectangle.ToptheForm.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()
【包含vb.net实现小时钟的词条】mTransform.RotateAt(CType(angle,Single),NewPointF(midX,midY))
gp.Transform(mTransform)
End Sub
End Class
为了节省篇幅,上面的代码省略了引入命名空间的语句 。
下面是分针(MinuteHand)类的定义vb.net实现小时钟:
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, midX3, 50)
gp.AddLine(midX3, 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.Left10, ClockRectangle.Top10, ClockRectangle.Width - 20, ClockRectangle.Height - 20)
g.DrawEllipse(Pens.Black, ClockRectangle.Left10, ClockRectangle.Top10, 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) / 225
Dim y As Double = (ClockRectangle.Width - 70) / 2 * Math.Sin(a - Math.PI / 3)(ClockRectangle.Width - 70) / 220
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.net作一个指针转动的钟表?(可设置时间日期,有闹铃功能)你需要会用GDI ,也就是那个System.Drawing命名空间下的类.
给你说个思路,设Timer,到时间就用Form.Invalidate()函数重画窗口,在重画窗口的Form_Paint事件下面编写代码得到当前时间,再根据当前时间用GDI 画时钟.
想用vb.net做一个登陆界面 在这个界面显示一个指针时钟 。就像下面的 。谁有现成的例子么?这个控件不是.net Fremawork自带的,是用户自己添加的控件,你可以去网上找这样的控件,在VS里添加引用就可以了
vb中怎样做出时钟?VB可使用Timer控件、Line控件和绘图或加载图片等制作指针式时钟 。
Timer 控件,通过引发 Timer 事件,Timer 控件可以有规律地隔一段时间执行一次代码 。
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) - bsPointX0 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) - bsPointX0 Then
If OriAngle0 Then
OriAngle = PI - Abs(OriAngle)
Else
OriAngle = PIAbs(OriAngle)
End If
End If
DestAngle = OriAngleRotateAngle
r = Sqr((X(i) - bsPointX) ^ 2(Y(i) - bsPointY) ^ 2)
X(i) = bsPointXr * Cos(DestAngle)
Y(i) = bsPointYr * 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
关于vb.net实现小时钟和的介绍到此就结束了 , 不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。
推荐阅读
- gis如何更新字段,gis怎么新增字段
- vb.net二维表头 二维表包括表头和表的内容,表头相当于关系的
- 580是什么cpu,580是什么号码
- 如何切换成html5,如何切换成IE浏览器
- 奥特曼格斗最强卡牌游戏,奥特曼格斗最强形态
- linux用命令怎么抓包 linux抓包工具有哪些
- 微信视频号认证有必要吗,微信视频号认证有必要吗安全吗
- 3366射击小游戏,43993d射击小游戏
- 虚拟机win10的安装,虚拟机win10安装不了