关于vb.net钟的信息( 二 )


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.net作一个指针转动的钟表?(可设置时间日期,有闹铃功能)你需要会用GDI+,也就是那个System.Drawing命名空间下的类.
给你说个思路,设Timer,到时间就用Form.Invalidate()函数重画窗口,在重画窗口的Form_Paint事件下面编写代码得到当前时间,再根据当前时间用GDI+画时钟.
VB.NET中怎么实现当时间达到设定时间时响铃??急急急 。。在线等!谢谢了,大神帮忙啊首先在form1上建立三个命令按钮,分别为command1(打开预启动的文件);command2(设定时间的按钮);第三个command3(即可启动)按钮可有可无;一个label1用来显示你预打开的文件名的路径;一个lbltime用来显示现在的时间;还有一个commondialog,它在工程菜单的部件中,你可把它加到工具箱中再开始使用;关于对话框你不必自己建,只要从应用程序向导中添加即可;一个timer,它的interval=500 。其次在form1的属性中设置为:startupposition=2-centerscreen和maxbutton=false;label1的属性中设置为:alignment=2-center 。外观大致是如图所示: 下面我们开始编程: OptionExplicit DimAlarmTime '申明变量 --------- PrivateSubCommand1_Click() Calldialog '调用dialog子程序 EndSub -------- PrivateSubCommand2_Click() AlarmTime=InputBox(“请输入你想设定的时间,例如(19:12:00)",“小闹钟") IfAlarmTime=“"ThenExitSub IfNotIsDate(AlarmTime)Then MsgBox“你所输入的不是时间格式,请重试!",,“Wrong" Else AlarmTime=CDate(AlarmTime) EndIf '判断输入的是否可转换成time格式 'isdate函数是判断输入的是否可转换成date格式 EndSub -------------- PrivateSubCommand3_Click() Calldeng '调用deng子程序 EndSub --------------- PrivateSubForm_Click() frmAbout.Show '显示关于对话框 EndSub ------------- PrivateSubForm_Load() Command3.Enabled=0 AlarmTime=“" '初始化时command3为不可用的 EndSub --------------- PrivateSubForm_Resize() IfWindowState=1Then mintime else caption=“小闹钟" EndIf '如果窗口被最小化,则调用mintime程序 EndSub --------------- PrivateSubmintime() Caption=Format(Time,“longTime") '使用长时间格式来显示时间 EndSub --------------- PrivateSubTimer1_Timer() IflblTime.CaptionCStr(Time)Then lblTime.Caption=Time EndIf '显示时间每秒钟的变化 IfTime=AlarmTimeThen Calldeng EndIf '判断如果现在的时间超过了设定的时间,则调用deng子程序 IfWindowState=1Then IfMinute(CDate(Caption))Minute(Time)Then mintime EndIf EndIf '最小化时显示时间每分钟的变化 EndSub ------------- Subdialog() CommonDialog1.Flags=cdlCFBoth CommonDialog1.ShowOpen Label1.Caption=CommonDialog1.filename IfLabel1“"Then Command3.Enabled=-1 Else ExitSub EndIf '把打开的文件名给于label1 '如果label1不为空时,则command3即可用 EndSub -------------- Subdeng() Dimss ss=Shell(Label1.Caption,1) End '启动指定的文件,并且结束小闹钟程序 EndSub 最后在about对话框中要提的是:在form_load中app.title表示你的应用程序的名字;app.major、minor、revision是关于应用程序的版本信息;lblDescription.Caption用于对本程序的一些说明描述之类的话;lblDisclaimer.Caption用于版权信息或警告等 。好了,至此我们已完成了一个简单的VB程序的编写 , 希望你能设计出更好的闹钟程序来!