タイトル | : Re^3: クラス内のタイマーイベントについて |
記事No | : 15141 |
投稿日 | : 2011/03/11(Fri) 05:27 |
投稿者 | : 魔界の仮面弁士 |
> 機能的にはSetTimerでも全く問題ないのですが、SetTimerでクラス側のメソッドを > 呼ぶことは可能なのでしょうか?
AddressOf には Module が必要です。 一応、下記のような手法もありますが…Form の Timer の方が楽ですね。
'------ 標準モジュール Module1 ------ Option Explicit Public Sub TimerProc(ByVal hWnd As OLE_HANDLE, ByVal uMsg As Long, ByVal idEvent As ITimer, ByVal dwTime As Long) If Not idEvent Is Nothing Then idEvent.OnTick dwTime End If End Sub
'------ クラス ITimer ------ Option Explicit Public Sub OnTick(ByVal uElapse As Long) End Sub Private Sub Class_Initialize() Err.Raise 419 End Sub
'------ クラス CTimer ------ Option Explicit Implements ITimer Public Event Tick(ByVal uElapse As Long) Private Declare Function SetTimer Lib "user32" (ByVal hWnd As OLE_HANDLE, ByVal nIDEvent As ITimer, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long Private Declare Function KillTimer Lib "user32" (ByVal hWnd As OLE_HANDLE, ByVal nIDEvent As ITimer) As Long Private m_owner As OLE_HANDLE Public Function StartTimer(ByVal owner As OLE_HANDLE, ByVal elapse As Long) As Boolean If m_owner = 0 And owner <> 0 Then m_owner = owner SetTimer owner, Me, elapse, AddressOf Module1.TimerProc StartTimer = True Else StartTimer = False End If End Function Public Sub StopTimer() If m_owner <> 0 Then KillTimer m_owner, Me m_owner = 0 End If End Sub Private Sub ITimer_OnTick(ByVal uElapse As Long) RaiseEvent Tick(uElapse) End Sub
'------ フォーム Form1 ------ Option Explicit Private WithEvents tm As CTimer Private Sub Form_Load() Set tm = New CTimer Command1.Enabled = True Command2.Enabled = False Command1.Caption = "開始" Command2.Caption = "停止" End Sub Private Sub Command1_Click() Command1.Enabled = False Command2.Enabled = True tm.StartTimer Me.hWnd, 100 End Sub Private Sub Command2_Click() Command1.Enabled = True Command2.Enabled = False tm.StopTimer End Sub Private Sub tm_Tick(ByVal uElapse As Long) Debug.Print uElapse End Sub
|