热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

VB6.0调用SetTimer实现定时器

Timer.bas:OptionExplicitDeclareFunctionSetTimerLib"user32"(ByVal
Timer.bas:
Option Explicit

Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long

Public Sub TimerProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
MsgBox Now()
End Sub
窗体代码:
Option Explicit

Dim lngTimerID As Long
Dim BlnTimer As Boolean

Private Sub Form_Load()
BlnTimer
= False
Command1.Caption
= "定时开始"
End Sub

Private Sub Form_Unload(Cancel As Integer)
KillTimer
0, lngTimerID
End Sub

Private Sub Command1_Click()
If BlnTimer = False Then
'每5秒钟调用一次函数
lngTimerID = SetTimer(0, 0, 5000, AddressOf TimerProc)
BlnTimer
= True
Command1.Caption
= "定时结束"
Else
KillTimer
0, lngTimerID
BlnTimer
= False
Command1.Caption
= "定时开始"
End If
End Sub
说明:
TimerProc函数定义一定要放在bas模块文件中,否则运行代码"lngTimerID = SetTimer(0, 0, 5000, AddressOf TimerProc)"会报错,
提示:操作符 AddressOf 使用无效。

推荐阅读
author-avatar
流纸香p_455
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有