'添加一个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
画面添加textbox1 timer1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Interval = 1000
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If IsDate(TextBox1.Text) = True Then
Timer1.Start()
End If
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Format(Now, "yyyy/MM/dd hh:mm:ss") = TextBox1.Text Then
MsgBox("It's time to go")
Timer1.Stop()
这个比较麻烦,我曾经写过这么个程序片段,现在贴上来,代码比较复杂
LineHour,LineMinute,LineSecond分别为时,分,秒针,为Line控件
再画个Timer控件,Interval设置为1000
以上控件位置随便放,代码里调整
Form_Load事件里写
Shape1.Top = 100
Timer事件里写
'利用Label显示数字
If Shape1.Visible True Then Shape1.Visible = True
If LineHour.Visible True Then LineHour.Visible = True
If LineMinute.Visible True Then LineMinute.Visible = True
If LineSecond.Visible True Then LineSecond.Visible = True
Dim cx As Single, cy As Single '圆心坐标
'利用Line控件绘制指针
'利用Line方法绘制刻度
就
是
这
个
了
!
Hand类的代码:
Public MustInherit Class Hand
Protected gp As GraphicsPath = New GraphicsPath()
Protected gpBase As GraphicsPath = Nothing
'构造器,得到窗体中心位置
Public Sub New(ByVal theForm As Form1)
MustOverride Sub Transform(ByVal d As DateTime)
'绘制指针路径
Overridable Sub Draw(ByVal g As Graphics)
g.DrawPath(aPen, gp)
g.FillPath(Brushes.Black, gp)
aPen.Dispose()
'使用矩阵实现路径(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 Class
为了节省篇幅,上面的代码省略了引入命名空间的语句.
下面是分针(MinuteHand)类的定义:
Public Class MinuteHand
Inherits Hand
'构造器,生成绘制分针的路径(gp)
Public Sub New(ByVal myForm As Form1)
MyBase.New(myForm)
gpBase = CType(gp.Clone(), GraphicsPath)
'Transform方法取得系统当前时间,并旋转时钟指针.
Public Overrides Sub Transform(ByVal d As DateTime)
Rotate(angle)
对所有的指针旋转的方法都是相同的,所以呢在基类中实现.由于时针和秒针的实现与分针相似,所不同者,只在于构造器中绘制的指针路径不同和Transform方法中转动的角度不同,今天这一节就不在赘述了.
Public Sub Draw(ByVal g As Graphics)
DrawClockFace(g)
DrawImage(g)
DrawNumbers(g)
DrawPin(g)
下面是ClockFace类的属性:
Private ClockRectangle As Rectangle
Private midPoint As Point
Private ClockImage As Bitmap
DrawClockFace方法用来画时钟表面:
Private Sub DrawClockFace(ByVal g As Graphics)
然后用Graphics对象的DrawImage方法画出米老鼠的图片:
Private Sub DrawImage(ByVal g As Graphics)
Dim nWidth As Integer = ClockImage.Width
Dim nHeight As Integer = ClockImage.Height
g.DrawImage(ClockImage, destRect)
数字在时钟上的位置是用sin和cos函数计算的:
Private Sub DrawNumbers(ByVal g As Graphics)
Dim count As Integer = 1
Dim a As Double
g.DrawString(Convert.ToString(count), ClockFont, Brushes.Black, CType(x, Single), CType(y, Single), New StringFormat())
count += 1
Next
最后是窗体文件(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)
'计时器事件
MySecondHand.Transform(DateTime.Now)
MyHourHand.Transform(DateTime.Now)
MyMinuteHand.Transform(DateTime.Now)
FirstTick = True
Invalidate()
VB可使用Timer控件、Line控件和绘图或加载图片等制作指针式时钟.
Timer 控件,通过引发 Timer 事件,Timer 控件可以有规律地隔一段时间执行一次代码.
Line 控件,Line 控件是图形控件,它显示水平线、垂直线或者对角线.
属性来移动它或者调整它的大小.
Circle 方法,在对象上画圆、椭圆或弧.
以下是通过加载图片的指针式时钟代码:
Option?Explicit
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
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
End?If
If?OriAngle?0?Then
OriAngle?=?PI?-?Abs(OriAngle)
OriAngle?=?PI?+?Abs(OriAngle)
DestAngle?=?OriAngle?+?RotateAngle
X(i)?=?bsPointX?+?r?*?Cos(DestAngle)
Y(i)?=?bsPointY?+?r?*?Sin(DestAngle)
Next?i
.X1?=?X(0)
.Y1?=?Y(0)
End?Sub
Private?Sub?Form_Load()
Timer1.Interval?=?1000
Dim?i?As?Long
Private?Sub?Timer1_Timer()
Me.Caption?=?Time$
①.、添加一个"label控件"命名为label
使用到的代码:
Dim?Hour?As?Integer?'小时
Dim?Min?As?Integer?'分钟
Dim?Sec?As?Integer?'秒
Hour?=?0
Min?=?0
Sec?=?0
Label1.Caption?=?"00?:?00?:?00"
Dim?strHour?As?String
Dim?strMin?As?String
Dim?strSec?As?String
Sec?=?Sec?+?1
Min?=?Min?+?1
Hour?=?Hour?+?1
If?Hour?10?Then
strHour?=?"0"?Hour
strHour?=?Hour
If?Min?10?Then
strMin?=?"0"?Min
strMin?=?Min
If?Sec?10?Then
strSec?=?"0"?Sec
strSec?=?Sec
Label1.Caption?=?strHour?"?:?"?strMin?"?:?"?strSec