top
Loading...
用VB設計一個定時鬧鐘程序
∈褂脀in98的人一定知道其中有一個“計劃任務"的應用程序,它能夠到指定時間便開始工作。不要認為它很神秘,其實用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企業版中通過。
作者:http://www.zhujiangroad.com
來源:http://www.zhujiangroad.com
北斗有巢氏 有巢氏北斗