#require /* @brief タイマ */ Namespace ActiveBasic Namespace Windows Namespace UI Class Timer Public 'Timer クラスの新しいインスタンスを初期化します。 Sub Timer(control As Control) If IsNothing(control) Then Throw New System.ArgumentNullException("control") EndIf ctrl = control End Sub 'タイマが実行されているかどうかを取得または設定します。 Sub Enabled(value As Boolean) If value Then If This.handle=0 Then This.Start() Else If This.handle Then This.Stop() End If End Sub Function Enabled() As Boolean Return handle <> 0 End Function 'Tick イベントが発生してから次の Tick イベントが発生するまでの時間 (ミリ秒単位) を取得または設定します。 Sub Interval(value As Long) This.interval = value End Sub Function Interval() As Long Return This.interval End Function /* 'なんらかの種類のユーザー状態を表す任意の文字列を取得または設定します。 Sub Tag(value As Object) This.tag = value End Sub Function Tag() As Object Return This.tag End Function*/ 'オーバーロードされます。 Sub Dispose() killImpl() End Sub 'タイマを起動します。 Sub Start() If handle = 0 Then handle = AllocObjectHandle(This) If SetTimer(ctrl As HWND, handle, interval, AddressOf(timerProc)) = 0 Then ThrowWithLastError() End If End If End Sub 'タイマを停止します。 Sub Stop() If killImpl() = 0 Then ThrowWithLastError() End If End Sub 'Timer を表す文字列を返します。 Override Function ToString() As String End Function Protected 'Tick イベントを発生させます。 Virtual Sub OnTick(e As System.EventArgs) If Not IsNothing(Tick) Then Tick(This, e) End If End Sub Public Sub AddTick(h As System.EventHandler) If IsNothing(Tick) Then Tick = h Else Tick += h End If End Sub Sub RemoveTick(h As System.EventHandler) If Not IsNothing(Tick) Then Tick -= h End If End Sub Private Static Sub timerProc(hwnd As HWND, msg As DWord, id As ULONG_PTR, time As DWord) Dim timer = GetObjectFromHandle(id) As Timer timer.OnTick(Nothing) End Sub Function killImpl() As Boolean If handle <> 0 Then killImpl = KillTimer(ctrl As HWND, handle) <> 0 ReleaseObjectHandle(handle) handle = 0 End If End Function Tick As System.EventHandler ctrl As Control handle As LONG_PTR 'タイマ実行中のGC回避およびタイマIDとして使用 interval As DWord End Class End Namespace 'UI End Namespace 'Widnows End Namespace 'ActiveBasic