明晨网络

电话: 136-6532-7492 QQ: 给我发送消息 8507-0741

VB代码:可在非窗体模块中使用的定时器

admin,2009-06-12 19:31, 文章标签: VB 定时器

    通常在VB窗体中拖入一个Timer控件来实现定时功能,但在类模块 或者ACTIVEX DLL中如何定时呢?明晨网络Mingchennet.com从国外收集到一段代码,包括一个bas模块和cls类模块,没有窗体也能使用定时器了。

bas模块modXTimerSupport

 

  1. Option Explicit  
  2.      
  3.      
  4. Const MAXTIMERINCREMEMT = 5  
  5.      
  6.      
  7. Private Type XTIMERINFO  
  8.     xt As clsXTimer  
  9.     id As Long 
  10.     blnReentered As Boolean 
  11. End Type  
  12.      
  13.      
  14. Declare Function SetTimer Lib "user32" (ByVal hWnd As LongByVal nIDEvent As LongByVal uElapse As LongByVal lpTimerProc As LongAs Long 
  15. Declare Function KillTimer Lib "user32" (ByVal hWnd As LongByVal nIDEvent As LongAs Long 
  16.      
  17.      
  18. Private maxti() As XTIMERINFO  
  19.      
  20.      
  21. Private mintMaxTimers As Integer 
  22.    
  23.    
  24. Public Function BeginTimer(ByVal xt As clsXTimer, ByVal Interval As Long)  
  25.     Dim lngTimerID As Long 
  26.     Dim intTimerNumber As Integer 
  27.      
  28.     lngTimerID = SetTimer(0, 0, Interval, AddressOf TimerProc)  
  29.     If lngTimerID = 0 Then Err.Raise vbObjectError + 31013, , "没有可用的定时器" 
  30.      
  31.     For intTimerNumber = 1 To mintMaxTimers  
  32.         If maxti(intTimerNumber).id = 0 Then Exit For 
  33.     Next 
  34.      
  35.     If intTimerNumber > mintMaxTimers Then 
  36.         mintMaxTimers = mintMaxTimers + MAXTIMERINCREMEMT  
  37.         ReDim Preserve maxti(1 To mintMaxTimers)  
  38.     End If 
  39.      
  40.     Set maxti(intTimerNumber).xt = xt  
  41.      
  42.     maxti(intTimerNumber).id = lngTimerID  
  43.     maxti(intTimerNumber).blnReentered = False 
  44.     BeginTimer = lngTimerID  
  45. End Function 
  46.    
  47.    
  48. Public Sub TimerProc(ByVal hWnd As LongByVal uMsg As LongByVal idEvent As LongByVal lngSysTime As Long)  
  49.     Dim intCt As Integer 
  50.      
  51.      
  52.     For intCt = 1 To mintMaxTimers  
  53.         If maxti(intCt).id = idEvent Then 
  54.             If maxti(intCt).blnReentered Then Exit Sub 
  55.             maxti(intCt).blnReentered = True 
  56.             On Error Resume Next 
  57.             maxti(intCt).xt.RaiseTick  
  58.             If Err.Number <> 0 Then 
  59.                 KillTimer 0, idEvent  
  60.                 maxti(intCt).id = 0  
  61.                 Set maxti(intCt).xt = Nothing 
  62.             End If 
  63.             maxti(intCt).blnReentered = False 
  64.             Exit Sub 
  65.         End If 
  66.     Next 
  67.     KillTimer 0, idEvent  
  68. End Sub 
  69.    
  70.    
  71. Public Sub EndTimer(ByVal xt As clsXTimer)  
  72.     Dim lngTimerID As Long 
  73.     Dim intCt As Integer 
  74.      
  75.     lngTimerID = xt.TimerID  
  76.     If lngTimerID = 0 Then Exit Sub 
  77.     For intCt = 1 To mintMaxTimers  
  78.         If maxti(intCt).id = lngTimerID Then 
  79.             KillTimer 0, lngTimerID  
  80.             Set maxti(intCt).xt = Nothing 
  81.             maxti(intCt).id = 0  
  82.             Exit Sub 
  83.         End If 
  84.     Next 
  85. End Sub 
  86.    
  87.    
  88. Public Sub Scrub()  
  89.     Dim intCt As Integer 
  90.     For intCt = 1 To mintMaxTimers  
  91.         If maxti(intCt).id <> 0 Then KillTimer 0, maxti(intCt).id  
  92.     Next 
  93. End Sub 

cls类模块clsXTimer

 

  1. Option Explicit  
  2.      
  3.      
  4. Private mlngTimerID As Long 
  5. Private mlngInterval As Long 
  6. Private mblnEnabled As Boolean 
  7.      
  8.      
  9. Event Tick()  
  10.    
  11.    
  12. Friend Property Get TimerID() As Long 
  13.     TimerID = mlngTimerID  
  14. End Property 
  15.    
  16.    
  17. Public Property Get Enabled() As Boolean 
  18.     Enabled = mblnEnabled  
  19. End Property 
  20.    
  21.    
  22. Public Property Let Enabled(ByVal NewValue As Boolean)  
  23.     If NewValue = mblnEnabled Then Exit Property 
  24.     mblnEnabled = NewValue  
  25.     If mlngInterval = 0 Then Exit Property 
  26.     If mblnEnabled Then 
  27.         Debug.Assert mlngTimerID = 0  
  28.         mlngTimerID = BeginTimer(Me, mlngInterval)  
  29.     Else 
  30.         If mlngTimerID <> 0 Then 
  31.             Call EndTimer(Me)  
  32.             mlngTimerID = 0  
  33.         End If 
  34.     End If 
  35. End Property 
  36.    
  37.    
  38. Public Property Get Interval() As Long 
  39.     Interval = mlngInterval  
  40. End Property 
  41.    
  42.    
  43. Public Property Let Interval(ByVal NewInterval As Long)  
  44.     If NewInterval = mlngInterval Then Exit Property 
  45.     mlngInterval = NewInterval  
  46.     If mlngTimerID <> 0 Then 
  47.         Call EndTimer(Me)  
  48.         mlngTimerID = 0  
  49.     End If 
  50.     If (NewInterval <> 0) And mblnEnabled Then 
  51.         mlngTimerID = BeginTimer(Me, NewInterval)  
  52.     End If 
  53. End Property 
  54.    
  55.    
  56. Public Sub RaiseTick()  
  57.     RaiseEvent Tick  
  58. End Sub 
  59.    
  60.    
  61. Private Sub Class_Terminate()  
  62.     On Error Resume Next 
  63.     If mlngTimerID <> 0 Then KillTimer 0, mlngTimerID  
  64.      
  65.      
  66. End Sub 

文章源自:明晨网络,admin,《VB代码:可在非窗体模块中使用的定时器》,http://www.mingchennet.com/tec/code/vb/30.htm