VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "clsMMTimerEx"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
 
Private Declare Function timeGetTime Lib "winmm.dll" () As Long
Private Const TIME_PERIODIC = 1 'Event occurs every uDelay milliseconds.
Private Const TIME_CALLBACK_FUNCTION = &H0 'When the timer expires, Windows calls the function pointed to by the lpTimeProc parameter. This is the default.
Private Declare Function timeKillEvent Lib "winmm.dll" (ByVal uID As Long) As Long
Private Declare Function timeSetEvent Lib "winmm.dll" (ByVal uDelay As Long, ByVal uResolution As Long, ByVal lpFunction As Long, ByVal dwUser As Long, ByVal uFlags As Long) As Long
Public CountEvents As Long
Private tmrID As Long
Public tmrIDInterval As Long
Private lpFunction As Long
Public Enabled As Boolean
Public Event Timer()

Public Sub CallEvent()
    RaiseEvent Timer
End Sub

Public Sub GetAddressOf(ByVal lpFunctionIn As Long)
    lpFunction = lpFunctionIn
End Sub

Public Function SysMSec(curStartTime As Currency) As Currency
    Dim curNewTime As Currency
    curNewTime = timeGetTime()
    If (curNewTime < curStartTime) Then
        curStartTime = curStartTime
        curNewTime = curNewTime + (2 ^ 32)
        SysMSec = curNewTime - curStartTime
    Else
        SysMSec = curNewTime - curStartTime
    End If
End Function

Public Sub StartStopTimer(isEnabled As Boolean, Optional tmrIDIntervalIn As Long = 1000)
    If (isEnabled) Then
        CountEvents = 0
        tmrIDInterval = tmrIDIntervalIn
        tmrID = timeSetEvent(1, 0, lpFunction, 0, TIME_PERIODIC Or TIME_CALLBACK_FUNCTION)
    Else
        timeKillEvent tmrID
        tmrID = 0
    End If
End Sub

Private Sub Class_Initialize()
    Enabled = False
End Sub

Private Sub Class_Terminate()
    On Error Resume Next
    StartStopTimer False
End Sub
