打印本文 打印本文 关闭窗口 关闭窗口
用VB设计一个定时闹钟程序
作者:武汉SEO闵涛  文章来源:敏韬网  点击数586  更新时间:2009/4/23 14:58:22  文章录入:mintao  责任编辑:mintao
         使用Win98的人一定知道其中有一个“计划任务"的应用程序,它能够到指定时间便开始工作。不要认为它很神秘,其实用VB来设计这样一个程序是非常容易的事,以下是一个简单的VB闹钟程序,可供各位使用VB的朋友参考,如有何不适之处,请多指点!
  首先在form1上建立三个命令按钮,分别为command1(打开预启动的文件);command2(设定时间的按钮);第三个command3(即可启动)按钮可有可无;一个label1用来显示你预打开的文件名的路径;一个lbltime用来显示现在的时间;还有一个common dialog,它在工程菜单的部件中,你可把它加到工具箱中再开始使用;关于对话框你不必自己建,只要从应用程序向导中添加即可;一个timer,它的interval=500。
  其次在form1的属性中设置为:startupposition=2-centerscreen和maxbutton=false;label1的属性中设置为:alignment=2-center。
  外观大致是如图所示:
  下面我们开始编程
  Option Explicit
  Dim AlarmTime
  申明变量
  ---------
  Private Sub Command1_Click()
  Call dialog
  调用dialog子程序
  End Sub
  --------
  Private Sub Command2_Click()
   AlarmTime = InputBox(“请输入你想设定的时间,例如(19:12:00)", “小闹钟")
   If AlarmTime = “" Then Exit Sub
  If Not IsDate(AlarmTime) Then
  MsgBox “你所输入的不是时间格式,请重试!", ,“Wrong"
  Else
  AlarmTime = Cdate(AlarmTime)
  End If
  判断输入的是否可转换成time格式
  isdate函数是判断输入的是否可转换成date格式
  End Sub
  --------------
  Private Sub Command3_Click()
  Call deng
  调用deng子程序
  End Sub
  ---------------
  Private Sub Form_Click()
  frmAbout.Show
  显示关于对话框
  End Sub
  -------------
  Private Sub Form_Load()
   Command3.Enabled = 0
   AlarmTime = “"
  初始化时command3为不可用的
  End Sub
  ---------------
  Private Sub Form_Resize()
   If WindowState = 1 Then
   mintime
   else
   caption=? 闹钟"
   End If
  如果窗口被最小化,则调用mintime程序
  End Sub
  ---------------
  Private Sub mintime()
  Caption = Format(Time, “long Time")
  使用长时间格式来显示时间
  End Sub
  ---------------
  Private Sub Timer1_Timer()
  If lblTime.Caption $#@60;$#@62; CStr(Time) Then
  lblTime.Caption = Time
  End If
  显示时间每秒钟的变化
  If Time $#@62;= AlarmTime Then
  Call deng
  End If
  判断如果现在的时间超过了设定的时间,则调用deng子程序
  If WindowState = 1 Then
  If Minute(Cdate(Caption)) $#@60;$#@62; Minute(Time) Then
  mintime
  End If
  End If
  最小化时显示时间每分钟的变化
  End Sub
  -------------
  Sub dialog()
  CommonDialog1.Flags = cdlCFBoth
  CommonDialog1.ShowOpen
  Label1.Caption = CommonDialog1.filename
  If Label1 $#@60;$#@62; “" Then
  Command3.Enabled = -1
  Else
  Exit Sub
  End If
  把打开的文件名给于label1
  如果label1不为空时,则command3即可用
  End Sub
  --------------
  Sub deng()
  Dim ss
  ss = Shell(Label1.Caption, 1)
  End
  启动指定的文件,并且结束小闹钟程序
  End Sub
  最后在about对话框中要提的是:在form_load中app.title表示你的应用程序的名字;app.major、minor、revision是关于应用程序的版本信息;lblDescription.Caption 用于对本程序的一些说明描述之类的话;lblDisclaimer.Caption用于版权信息或警告等。
  好了,至此我们已完成了一个简单的VB程序的编写,希望你能设计出更好的闹钟程序来!
  以上在windows98,中文VB5.0企业版中通过。

打印本文 打印本文 关闭窗口 关闭窗口