在 VB6 中实现无 Timer 控件的计时器功能






4.64/5 (10投票s)
一个 VB6 项目,描述了如何在没有 Timer 控件的情况下实现计时器功能
引言
假设你有一个 Microsoft Excel 应用程序 (VBA),并且需要在其中使用计时器功能。Excel 中没有名为“Timer”的内置控件。你会怎么做?另外,假设你处于无法使用“Form”(你可以在其中“放置”计时器控件)的情况。但你仍然需要一个计时器功能,它将像计时器控件一样精确地工作。它会定时触发一个事件,并且可以启动和停止等。如果你正处于这种情况下,那么下面的文章就是为你准备的。
背景
我在互联网上看到许多文章,它们使用一组复杂的 Win32 API 来解决计时器功能,而无需使用计时器控件。这些代码使用起来非常危险,你的应用程序随时可能崩溃。而且,在大多数情况下,没有像计时器控件那样可以触发事件的选项。在这篇文章中,我展示了一种非常简单的方法,仅使用一个 Win32 API 来实现计时器功能,而无需使用计时器控件。这是一个单独的 ActiveX DLL 项目。
Using the Code
此应用程序的主要技巧是使用名为“GetTickCount
”的 Win32API。GetTickCount
函数检索自 Windows 启动以来经过的毫秒数。使用此 API,你可以编写一个 ActiveX DLL,它将满足你对计时器的需求。我将项目命名为 TimerLib
,它只有一个名为 TimerEx.cls 的类。请参阅下面的代码
Option Explicit
'* The GetTickCount function retrieves the number of milliseconds
'* that have elapsed since Windows was started.
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private mblnEnabled As Boolean
Private mlngInterval As Long
Private mstrTag As String
Private mlngTickCount As Long
Private mtypIntervalType As IntervalData
'* This is the timer event that will fire in a given interval
Public Event OnTimer()
'* A type that will hold the extended information about the interval you want to set
'* If you set different types of intervals, the total interval will
'* be calculated combining all types
Public Type IntervalData
MilliSecond As Long
Second As Long
Minute As Long
Hour As Long
End Type
'* You can see whether the timer is Enabled by this property
Public Property Get Enabled() As Boolean
Enabled = mblnEnabled
End Property
'* You can start / stop the timer by this property
Public Property Let Enabled(blnEnabled As Boolean)
mblnEnabled = blnEnabled
If blnEnabled Then
mlngTickCount = GetTickCount
Call TimerLoop
End If
End Property
'* Conventional Interval property of the timer, you can check how many milliseconds
'* have been set for the timer
Public Property Get Interval() As Long
Interval = mlngInterval
End Property
'* Conventional Interval property of the timer, you can set interval of the timer
'* in milliseconds
Public Property Let Interval(lngInterval As Long)
mlngInterval = lngInterval
End Property
'* Extended Interval property of the timer, you can check how many
'* milliseconds / seconds / minutes / hours have been set for the timer
Public Property Get IntervalInfo() As IntervalData
IntervalInfo = mtypIntervalType
End Property
'* Extended Interval property of the timer, you can set the interval in
'* milliseconds / seconds / minutes / hours
Public Property Let IntervalInfo(typIntervalType As IntervalData)
mtypIntervalType = typIntervalType
mlngInterval = mtypIntervalType.MilliSecond + typIntervalType.Second * 1000 + _
typIntervalType.Minute * 60 * 1000 + typIntervalType.Hour * 60 * 60 * 1000
End Property
'* Check what info is in the Tag property in the timer, you can store any string data
'* into this property
Public Property Get Tag() As String
Tag = mstrTag
End Property
'* You can store any string data into this property as extra info of your timer
Public Property Let Tag(strTag As String)
mstrTag = strTag
End Property
'* Core of the timer. It fires the OnTimer event in a timely fashion according to
'* the Interval / IntervalInfo you have set
Private Sub TimerLoop()
Do While Not mblnEnabled = False
If GetTickCount - mlngTickCount >= mlngInterval Then
RaiseEvent OnTimer
mlngTickCount = GetTickCount
'* Like GetTickCount has exceeded its capacity,
'* run over from the beginning
ElseIf GetTickCount = 0 Then
mlngTickCount = 0
ElseIf GetTickCount < mlngTickCount Then
mlngTickCount = 0
End If
DoEvents
Loop
End Sub
'* ENJOY!!
如何使用该库
你可以从任何 COM 兼容的高级语言中使用此库。在示例代码中,我使用了一个标准的 VB6 EXE 应用程序,该应用程序在表单中有两个按钮,分别命名为“cmdEnableTimer
”和“cmdDisableTimer
”。我从“项目>引用”中获取了计时器库的引用,并使用... 使用 TimerEx
类
WithEvents
... 关键字,以便可以跟踪计时器事件。在我的库中,计时器事件是 OnTimer
。请参阅下面的代码
Option Explicit
Private WithEvents myTimer As TimerEx
Private mlngTick As Long
Private myIntervalInfo As IntervalData
Private Sub cmdDisableTimer_Click()
myTimer.Enabled = False
End Sub
Private Sub cmdEnableTimer_Click()
myTimer.Enabled = True
End Sub
Private Sub Form_Load()
myIntervalInfo.Second = 5
Set myTimer = New TimerLib.TimerEx
myTimer.IntervalInfo = myIntervalInfo
End Sub
Private Sub Form_Unload(Cancel As Integer)
myTimer.Enabled = False
Set myTimer = Nothing
End Sub
Private Sub myTimer_OnTimer()
mlngTick = mlngTick + 1
Me.Caption = mlngTick
End Sub
关注点
你可以从任何 COM 兼容的语言中使用 TimerLib
。你将从代码中看到,我将 VB6 计时器控件的 Interval
属性从传统的毫秒扩展到秒/分钟甚至小时。希望你喜欢使用这段代码。
历史
- 2008 年 9 月 14 日:初始发布