appi101
Jul 2nd, 2001, 09:21 AM
Hi
I want to create a control to replace the standard timer control which comes with VB as its interval is too small. So I built an ActiveX Control and put the relevant settimer and killtimer functions. But to recieve the timer events I must call the Addressof Function and this function only works with functions in a module. But if I move the timerproc function (the function which received notifications of the Timer) to a module then I cannot raise the timer event for the control. So How do i get out of this Catch 22
Thanks in advance
Appi
Aaron Young
Jul 2nd, 2001, 10:11 PM
You can use the Friend keyword and a reference to the usercontrol, i.e.
In a standard module in the ActiveX Control Project:Private Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Private Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long
Private lTimerHnd As Long
' Store a reference to the Usercontrol
' so we can call "Friendly" Subs/Functions.
Private oTimerCtrl As ayTimer
' Public Sub to Set the Timer, Requires an Interval in ms and
' a reference to the calling Usercontrol
Public Sub modSetTimer(ByVal Interval As Long, ByRef oTmr As ayTimer)
' Only set the timer if the existing one has been terminated
If lTimerHnd = 0 Then
Set oTimerCtrl = oTmr
lTimerHnd = SetTimer(0, 1, Interval, AddressOf TimerProc)
End If
End Sub
Public Sub modStopTimer()
' Stop the existing Timer
If lTimerHnd Then
Call KillTimer(0, lTimerHnd)
lTimerHnd = 0
Set oTimerCtrl = Nothing
End If
End Sub
Private Sub TimerProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
' Raise the "Friend" Sub in the Usercontrol which in turn
' Raises the TimerFire Event for the Control
oTimerCtrl.RaiseTimerEvent
End SubIn the User Control (I've named it ayTimer):Private bFiring As Boolean
Private bEnabled As Boolean
Private lInterval As Long
' Timer Event
Event TimerFire()
' Friendly Sub used to Raise the Event from the standard module
Friend Sub RaiseTimerEvent()
RaiseEvent TimerFire
End Sub
' Retreive the Timer Control Properties
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
bEnabled = PropBag.ReadProperty("Enabled", True)
lInterval = PropBag.ReadProperty("Interval", 0)
StartTimer
End Sub
' Store the Timer Control Properties
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
Call PropBag.WriteProperty("Enabled", bEnabled)
Call PropBag.WriteProperty("Interval", lInterval)
End Sub
' Stop the Timer if it's going when the control is destroyed
Private Sub UserControl_Terminate()
If bFiring Then
modStopTimer
bFiring = False
End If
End Sub
' Return the Timer Interval
Public Property Get Interval() As Long
Interval = lInterval
End Property
' Set the Timer Interval
Public Property Let Interval(ByVal vNewValue As Long)
lInterval = vNewValue
If lInterval = 0 Then
StopTimer
Else
StartTimer
End If
End Property
' Return the Timers "Enabled" status
Public Property Get Enabled() As Boolean
Enabled = bEnabled
End Property
' Set the Timers "Enabled" status
Public Property Let Enabled(ByVal vNewValue As Boolean)
bEnabled = vNewValue
If Not bEnabled Then
StopTimer
Else
StartTimer
End If
End Property
' Start the Timer (only at runtime)
Private Sub StartTimer()
If Not UserControl.Ambient.UserMode Then Exit Sub
If bEnabled And lInterval > 0 Then
StopTimer
Call modSetTimer(lInterval, Me)
bFiring = True
End If
End Sub
' Stop the Timer (only at runtime)
Private Sub StopTimer()
If Not UserControl.Ambient.UserMode Then Exit Sub
If bFiring Then
Call modStopTimer
End If
bFiring = False
End Sub