vb.net+gdi时钟的简单介绍

怎样用vb.net作一个指针转动的钟表?(可设置时间日期,有闹铃功能)你需要会用GDI ,也就是那个System.Drawing命名空间下的类.
给你说个思路,设Timer,到时间就用Form.Invalidate()函数重画窗口,在重画窗口的Form_Paint事件下面编写代码得到当前时间,再根据当前时间用GDI 画时钟.
VB用GDI使桌面时钟指针图片PNG旋转!'秒、分、时
Dim Second, Minute, Hour As Integer
'表的中心位置
Dim X0, Y0 As Integer
'
Dim X, Y As Variant
Private Sub Form_MouseMove(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
'按下鼠标左键
If Button = 1 Then
'鼠标指针在表盘内
If XX0 - 950 And XX0950 Then
If YY0 - 950 And YY0950 Then
'移动表的位置
X0 = X
Y0 = Y
End If
End If
End If
Form1.Refresh
End Sub
Sub Form_Load()
'设置表的中心位置
Let X0 = 1100
Let Y0 = 1100
'时刻与对应坐标的转换表
X = Array(0, 104, 207, 309, 406, 500, 587, _
669, 743, 809, 866, 913, 951, 978, 994, 1000, _
994, 978, 951, 913, 866, 809, 743, 669, 587, 500, 406, 309, 207, 104, 0, -105, -208, -310, -407, -500, -588, -670, -744, -810, -867, -914, -952, -979, -995, -1000, -995, -979, -952, -914, -867, -810, -744, -670, -588, -500, -407, -310, -208, -105, -1)
Y = Array(1000, 994, 978, 951, 913, 866, 809, 743, 669, 587, 500, 406, 309, 207, 104, 0, -105, -208, -310, -407, -500, -588, -670, -744, -810, -867, -914, -952, -979, -995, -1000, -995, -979, -952, -914, -867, -810, -744, -670, -588, -500, -407, -310, -208, -105, -1, 104, 207, 309, 406, 500, 587, 669, 743, 809, 866, 913, 951, 978, 994, 1000)
'初始化定时器
Timer1.Enabled = True
Timer1.Interval = 1
End Sub
Private Sub Timer1_Timer()
'绘制表盘
Circle (X0, Y0), 950, RGB(255, 255, 255)
'绘制刻度
DrawPlots
'定位时、分、秒针的起点
Line1.X1 = X0
Line1.Y1 = Y0
Line2.Y1 = Y0
Line2.X1 = X0
Line3.X1 = X0
Line3.Y1 = Y0
'秒
Second = Mid(Time, 7, 2)
'分
Minute = Mid(Time, 4, 2)
'时
Hour = Mid(Time, 1, 2)
If Hour11 Then Hour = Hour - 12
Hour = Hour * 5(Hour / 12)
'定位时、分、秒针的终点
Line1.X2 = X(Second) * 0.9X0
Line1.Y2 = Y0 - Y(Second) * 0.9
Line2.X2 = X(Minute) * 0.8X0
Line2.Y2 = Y0 - Y(Minute) * 0.8
Line3.X2 = X(Hour) * 0.4X0
Line3.Y2 = Y0 - Y(Hour) * 0.4
End Sub
'绘制刻度
Sub DrawPlots()
For xx = 0 To 60
If (xx Mod 5) = 0 Then
Line (X(xx)X0, Y(xx)Y0)-(X(xx) * 1.1X0, Y(xx) * 1.1Y0), RGB(256, 256, 256)
End If
Next xx
End Sub希望可以帮到你.!
vb.net开发简单的时钟程序??高手救救我!就




vb.net gdi时钟!
Hand类的代码vb.net gdi时钟:
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 ‘中心位置
‘构造器vb.net gdi时钟,得到窗体中心位置
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()
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, 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
【vb.net gdi时钟的简单介绍】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 当然是全部重画 。
层只不过是制图软件弄出来的一个方便的东西而已 。
就像你画画,画上去如果你要擦掉当然是擦到底色咯 。(当然GDI 也可以像你画画一样只擦一部分)
GDI 时钟我写过一个VB6的 。代码详见我博客 。地址显然百度不让贴上= = 。所以你可以看下我的资料 。
你可以模拟层,就是把所有绘制信息都保存起来 。你的流程应当是:
如果要绘制了 , 更新绘制信息(可以是数组啥的 。),交给一个Draw过程
Draw过程:根据绘制信息,全部绘制 。
By vIstaswx ,before junior school graduation exam.
用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,如何用TIMER控件计时这种功能用不到TIMER,TIMER控件用在这种地方也不适合 。(假如你所统计的时间很短,在几分中内话 , 可以使用,假如你统计的时间很长:几小时、几天几夜,建议改用以下方式):
在你需要开始计时的地方加入一个记录当前时间,在你想结束的地方也得到一个当前时间 。然后将两个时间相减 。
希望以上思路可以帮到你 。
关于vb.net gdi时钟和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读