計算機畢業論文-vb中創建超長時間計時器對象
時間:2022-06-07 04:28:00
導語:計算機畢業論文-vb中創建超長時間計時器對象一文來源于網友上傳,不代表本站觀點,若需要原創文章可咨詢客服老師,歡迎參考。
摘要:本文介紹在vb程序中用timer控件創建一個超長時間的計時器對象,將其編譯成activexdll部件,利用代碼的可重用性,在其它應用程序中調用。
關鍵詞:vb計時器對象
vb系統中提供了固有計時器timer控件,通過給計時器控件的interval屬性設置不同的數值,就可以控制計時器運行的時間間隔,但該屬性是一個雙字節的整型變量,最大值只能設置為65535,即只有65535毫秒,約一分鐘多一點,在需要長時間計時時,該控件就無能為力了。本文給出用timer控件創建一個超長時間的計時器對象,并將其編譯成activexdll部件,供其它應用程序調用,可實現最大限度地利用代碼的可重用性。下面給出這個超長計時器對象的編程設計過程,供讀者參考選用。
1.編寫程序
啟動vb,新建一個activexdll工程,將工程命名為newtimer,將類模塊名class1改名為longtimer。在工程中添加一個窗體form1,在窗體上加入一個timer計時器控件timer1。打開longtimer類模塊,加入如下程序代碼:
optionexplicit
publiceventtick()''''聲明事件
privatenzasinteger
privatecsasinteger
privatem_formasform1
privatewitheventsm_timerastimer''''聲明對象類變量
''''返回enable屬性值
publicpropertygetenabled()asboolean
enabled=m_timer.enabled
endproperty
''''設置enable屬性值
publicpropertyletenabled(byvalvnewvalueasboolean)
m_timer.enabled=vnewvalue
endproperty
''''返回interval屬性值
publicpropertygetinterval()aslong
interval=m_timer.interval
endproperty
''''設置interval屬性值
publicpropertyletinterval(byvalvnewvalueaslong)
cs=0
nz=vnewvalue\65536+1
m_timer.interval=vnewvalue\nz
endproperty
''''類初始化事件
privatesubclass_initialize()
setm_form=newform1
loadm_form
setm_timer=m_form.timer1
endsub
''''類終止事件
privatesubclass_terminate()
unloadm_form
setm_timer=nothing
endsub
''''產生tick事件
privatesubm_timer_timer()
cs=cs+1
ifcs>=nzthen
raiseeventtick
cs=0
endif
endsub
窗體form1只用來作計時器timer1的載體,不用于顯示。
2.測試newtimer.dll
對activexdll部件進行測試,與標準的exe程序一樣,可分為單步運行或全運行。在寫完所有程序代碼后,建議用戶先用ctrl+f5進行全編譯執行,發現檢查newtimer程序的語法錯誤。也可建立一個測試工程與newtimer鏈接進行測試,針對由newtimer類模塊提供的每個屬性和事件,添加測試代碼來測試它們的屬性和方法。按f8,進入單步運行狀態,逐個語句察看對newtimer類模塊的調用情況。
3.編譯newtimer.dll
完成測試,排除所有錯誤后,從“文件(f)”菜單中選擇“生成newtimer.dll(k)”命令,編譯生成newtimer.dll文件。編譯成功后newtimer.dll文件將自動注冊到windows注冊表中,
4.引用newtimer.dll
引用activexdll部件同引用其他對象一樣,從“工程(p)”菜單中選擇“引用(n)”命令,打開引用窗口,從引用列表中選中newtimer對象。如果是第一次引用newtimer,則在引用窗體上單擊“游覽(b)”命令按鈕,將newtimer.dll文件添加到可引用列表框中方可使用。
5.調用newtimer.dll
在應用程序中加入下述程序代碼,實現參newtimer.dll的調用:
optionexplicit
dimwitheventslongtimer1aslongtimer
privatesublongtimer1_tick()
print"時間到,此處添加處理程序"
endsub
privatesubcommand1_click()
longtimer1.interval=12000‘兩分鐘
longtimer1.enabled=true
endsub
privatesubform_load()
setlongtimer1=newlongtimer
endsub
參考文獻
[2]東箭工作室.visualbasic5.0中文版程序設計.清華大學出版社,1997
[3]evangelospetroutsos.visualbasic5從入門到精通.北京:電子工業出版社,1997