没有一个计时器控制在VB6计时器功能

介绍 认为你有一个Microsoft Excel应用程序(VBA),你必须使用计时器功能。没有这样的本地控制在Excel中称为“定时器”。你会做什么?也认为你在一个情况下你不能使用“形式”,您可以在“把”一个计时器控制。但是仍然需要一个计时器功能,将定时器控件完全一样。它会触发一个事件及时成形,可以启动和停止,等。如果你在这种情况下,下面的这篇文章是给你的。 背景 我看过很多文章在互联网上解决定时器功能没有一个计时器控制使用一组复杂的Win32 api。代码是非常危险的使用和应用程序可以随时崩溃。也在大多数情况下,没有像计时器控制事件触发选项。在这篇文章中,我展示了一个非常简单的方法实现定时器功能没有一个计时器控制只使用一个Win32 API。它是一个类AxtiveX DLL项目。 使用的代码 这个应用程序的主要棘手的方式使用Win32API名叫“GetTickCount”。GetTickCount函数检索后经过的毫秒数,Windows就开始了。使用该API,您可以编写一个ActiveX DLL将为您服务需要一个计时器。我已经将该项目命名为“TimerLib”,它只有一个类TimerEx.cls命名。看下面的代码: 隐藏,收缩,复制Code

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”。我已经从Project>计时器库的引用;引用。和TimerEx类使用…… 隐藏,复制Code

WithEvents

…关键字的计时器事件可以被跟踪。在我的图书馆,定时计时器事件。看下面的代码: 隐藏,收缩,复制Code

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定时器的间隔属性控制从传统毫秒到秒/分钟,即使在小时。希望你会喜欢使用这段代码。 历史 2008年9月14日:初始post  本文转载于:http://www.diyabc.com/frontweb/news2257.html

posted @ 2020-08-08 13:10  Dincat  阅读(313)  评论(0编辑  收藏  举报