在CSDN的VB论坛里呆了一年,交了不少朋友,受到了不少启发,非常感谢大家,同时也为自己在答问时错误的解答而向大家致歉。俺原本想在VB版里混上两颗星就算了,现在看来离目标不远了,所以散分,并把自己写的一个在VB的类模块中使用计时器的代码贴出来,谈不上分享,只供大家指正:一、类模块代码如下:Option Explicit
'* ******************************************** *
'* 模块名称:Timer.cls
'* 功能:在VB类模块中使用计时器
'* 作者:lyserver
'* ******************************************** *Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, _
Source As Any, ByVal Length As Long)
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 LongDim m_idTimer As Long
Dim m_Enabled As Boolean
Dim m_Interval As Long
Private Sub Class_Initialize()
m_Interval = 0
End SubPrivate Sub Class_Terminate()
If m_idTimer <> 0 Then KillTimer 0, m_idTimer
End SubPublic Property Get Interval() As Long
Interval = m_Interval
End Property
Public Property Let Interval(ByVal New_Value As Long)
If New_Value >= 0 Then m_Interval = New_Value
End PropertyPublic Property Get Enabled() As Boolean
Enabled = m_Enabled
End Property
Public Property Let Enabled(ByVal New_Value As Boolean)
m_Enabled = New_Value
If m_idTimer <> 0 Then KillTimer 0, m_idTimer
If New_Value And m_Interval > 0 Then
m_idTimer = SetTimer(0, 0, m_Interval, GetFuncAddr(8))
End If
End PropertyPrivate Function GetFuncAddr(ByVal IndexOfFunc As Long) As Long
Static AsmCode(33) As Byte
Dim pThis As Long, pVtbl As Long, pFunc As Long
pThis = ObjPtr(Me)
CopyMemory pVtbl, ByVal pThis, 4
CopyMemory pFunc, ByVal pVtbl + (6 + IndexOfFunc) * 4, 4
AsmCode(0) = &H55
AsmCode(1) = &H8B: AsmCode(2) = &HEC
CopyMemory AsmCode(3), &H1475FF, 3
CopyMemory AsmCode(6), &H1075FF, 3
CopyMemory AsmCode(9), &HC75FF, 3
CopyMemory AsmCode(12), &H875FF, 3
AsmCode(15) = &HB9
CopyMemory AsmCode(16), pThis, 4
AsmCode(20) = &H51
AsmCode(21) = &HE8
CopyMemory AsmCode(22), pFunc - VarPtr(AsmCode(21)) - 5, 4
AsmCode(26) = &H8B: AsmCode(27) = &HE5
AsmCode(28) = &H5D
AsmCode(29) = &HC2
CopyMemory AsmCode(30), 16, 4
GetFuncAddr = VarPtr(AsmCode(0))
End FunctionPrivate Sub TimerProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
Debug.Print "类模板中的计时器:", uMsg, idEvent, dwTime
End Sub二、调用代码如下:Dim m_tm As TimerPrivate Sub Form_Load()
Set m_tm = New Timer
End SubPrivate Sub Form_Unload(Cancel As Integer)
Set m_tm = Nothing
End SubPrivate Sub Command1_Click()
m_tm.Interval = 1000
m_tm.Enabled = True
End SubPrivate Sub Command2_Click()
m_tm.Enabled = False
End Sub
'* ******************************************** *
'* 模块名称:Timer.cls
'* 功能:在VB类模块中使用计时器
'* 作者:lyserver
'* ******************************************** *Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, _
Source As Any, ByVal Length As Long)
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 LongDim m_idTimer As Long
Dim m_Enabled As Boolean
Dim m_Interval As Long
Private Sub Class_Initialize()
m_Interval = 0
End SubPrivate Sub Class_Terminate()
If m_idTimer <> 0 Then KillTimer 0, m_idTimer
End SubPublic Property Get Interval() As Long
Interval = m_Interval
End Property
Public Property Let Interval(ByVal New_Value As Long)
If New_Value >= 0 Then m_Interval = New_Value
End PropertyPublic Property Get Enabled() As Boolean
Enabled = m_Enabled
End Property
Public Property Let Enabled(ByVal New_Value As Boolean)
m_Enabled = New_Value
If m_idTimer <> 0 Then KillTimer 0, m_idTimer
If New_Value And m_Interval > 0 Then
m_idTimer = SetTimer(0, 0, m_Interval, GetFuncAddr(8))
End If
End PropertyPrivate Function GetFuncAddr(ByVal IndexOfFunc As Long) As Long
Static AsmCode(33) As Byte
Dim pThis As Long, pVtbl As Long, pFunc As Long
pThis = ObjPtr(Me)
CopyMemory pVtbl, ByVal pThis, 4
CopyMemory pFunc, ByVal pVtbl + (6 + IndexOfFunc) * 4, 4
AsmCode(0) = &H55
AsmCode(1) = &H8B: AsmCode(2) = &HEC
CopyMemory AsmCode(3), &H1475FF, 3
CopyMemory AsmCode(6), &H1075FF, 3
CopyMemory AsmCode(9), &HC75FF, 3
CopyMemory AsmCode(12), &H875FF, 3
AsmCode(15) = &HB9
CopyMemory AsmCode(16), pThis, 4
AsmCode(20) = &H51
AsmCode(21) = &HE8
CopyMemory AsmCode(22), pFunc - VarPtr(AsmCode(21)) - 5, 4
AsmCode(26) = &H8B: AsmCode(27) = &HE5
AsmCode(28) = &H5D
AsmCode(29) = &HC2
CopyMemory AsmCode(30), 16, 4
GetFuncAddr = VarPtr(AsmCode(0))
End FunctionPrivate Sub TimerProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
Debug.Print "类模板中的计时器:", uMsg, idEvent, dwTime
End Sub二、调用代码如下:Dim m_tm As TimerPrivate Sub Form_Load()
Set m_tm = New Timer
End SubPrivate Sub Form_Unload(Cancel As Integer)
Set m_tm = Nothing
End SubPrivate Sub Command1_Click()
m_tm.Interval = 1000
m_tm.Enabled = True
End SubPrivate Sub Command2_Click()
m_tm.Enabled = False
End Sub
解决方案 »
- 怎么将窗体式的代码转到模块中运行啊?
- 关于vb的MSDN示例中的一个问题。
- vb Run-time error error '-214767259 (80004005)' ;Data provider or other service returned an E_FAIL status.Thanks for any suggest
- 浏览器插件的开发
- 请教各位大虾! 我想做一个系统起动画面(像QQ2004登录那样的) 该怎么做?
- 我的系统是Win2K,有什么API函数可以执行里面的"搜索计算机"
- 请问,哪里有数字签名控件下载
- 关于ADO的怪问题
- 请问各位,有什么软件可用一般的光驱完整的播DVCD影碟,或者是自己编程实现?
- 关于打印报表问题,来者一定给分!还望各位大虾多多指点!
- vb6.0 activex 路径/文件访问错误
- 关于Instr函数
Dim m_tm As TimerPrivate Sub Form_Load()
Set m_tm = New Timer
End SubPrivate Sub Form_Unload(Cancel As Integer)
Set m_tm = Nothing
End SubPrivate Sub Command1_Click()
m_tm.Interval = 1000
m_tm.Enabled = True
End SubPrivate Sub Command2_Click()
m_tm.Enabled = False
End Sub
学习一下,呵呵
Set m_tm = New Timer
End SubPrivate Sub Form_Unload(Cancel As Integer)
Set m_tm = Nothing
End SubPrivate Sub Command1_Click()
m_tm.Interval = 1000
m_tm.Enabled = True
End SubPrivate Sub Command2_Click()
m_tm.Enabled = False
End Sub