1 |
|
---|
2 | #require <Classes/ActiveBasic/Windows/UI/Control.ab>
|
---|
3 | /*
|
---|
4 | @brief タイマ
|
---|
5 | */
|
---|
6 |
|
---|
7 | Namespace ActiveBasic
|
---|
8 | Namespace Windows
|
---|
9 | Namespace UI
|
---|
10 |
|
---|
11 | Class Timer
|
---|
12 | Public
|
---|
13 | 'Timer クラスの新しいインスタンスを初期化します。
|
---|
14 | Sub Timer(control As Control)
|
---|
15 | If IsNothing(control) Then
|
---|
16 | Throw New System.ArgumentNullException("control")
|
---|
17 | EndIf
|
---|
18 | ctrl = control
|
---|
19 | End Sub
|
---|
20 |
|
---|
21 | 'タイマが実行されているかどうかを取得または設定します。
|
---|
22 | Sub Enabled(value As Boolean)
|
---|
23 | If value Then
|
---|
24 | If This.handle=0 Then This.Start()
|
---|
25 | Else
|
---|
26 | If This.handle Then This.Stop()
|
---|
27 | End If
|
---|
28 | End Sub
|
---|
29 | Function Enabled() As Boolean
|
---|
30 | Return handle <> 0
|
---|
31 | End Function
|
---|
32 |
|
---|
33 | 'Tick イベントが発生してから次の Tick イベントが発生するまでの時間 (ミリ秒単位) を取得または設定します。
|
---|
34 | Sub Interval(value As Long)
|
---|
35 | This.interval = value
|
---|
36 | End Sub
|
---|
37 | Function Interval() As Long
|
---|
38 | Return This.interval
|
---|
39 | End Function
|
---|
40 |
|
---|
41 | /* 'なんらかの種類のユーザー状態を表す任意の文字列を取得または設定します。
|
---|
42 | Sub Tag(value As Object)
|
---|
43 | This.tag = value
|
---|
44 | End Sub
|
---|
45 | Function Tag() As Object
|
---|
46 | Return This.tag
|
---|
47 | End Function*/
|
---|
48 |
|
---|
49 | 'オーバーロードされます。
|
---|
50 | Sub Dispose()
|
---|
51 | killImpl()
|
---|
52 | End Sub
|
---|
53 |
|
---|
54 | 'タイマを起動します。
|
---|
55 | Sub Start()
|
---|
56 | If handle = 0 Then
|
---|
57 | handle = AllocObjectHandle(This)
|
---|
58 | If SetTimer(ctrl As HWND, handle, interval, AddressOf(timerProc)) = 0 Then
|
---|
59 | ThrowWithLastError()
|
---|
60 | End If
|
---|
61 | End If
|
---|
62 | End Sub
|
---|
63 |
|
---|
64 | 'タイマを停止します。
|
---|
65 | Sub Stop()
|
---|
66 | If killImpl() = 0 Then
|
---|
67 | ThrowWithLastError()
|
---|
68 | End If
|
---|
69 | End Sub
|
---|
70 |
|
---|
71 | 'Timer を表す文字列を返します。
|
---|
72 | Override Function ToString() As String
|
---|
73 | End Function
|
---|
74 |
|
---|
75 | Protected
|
---|
76 | 'Tick イベントを発生させます。
|
---|
77 | Virtual Sub OnTick(e As System.EventArgs)
|
---|
78 | If Not IsNothing(Tick) Then
|
---|
79 | Tick(This, e)
|
---|
80 | End If
|
---|
81 | End Sub
|
---|
82 |
|
---|
83 | Public
|
---|
84 | Sub AddTick(h As System.EventHandler)
|
---|
85 | If IsNothing(Tick) Then
|
---|
86 | Tick = h
|
---|
87 | Else
|
---|
88 | Tick += h
|
---|
89 | End If
|
---|
90 | End Sub
|
---|
91 | Sub RemoveTick(h As System.EventHandler)
|
---|
92 | If Not IsNothing(Tick) Then
|
---|
93 | Tick -= h
|
---|
94 | End If
|
---|
95 | End Sub
|
---|
96 | Private
|
---|
97 | Static Sub timerProc(hwnd As HWND, msg As DWord, id As ULONG_PTR, time As DWord)
|
---|
98 | Dim timer = GetObjectFromHandle(id) As Timer
|
---|
99 | timer.OnTick(Nothing)
|
---|
100 | End Sub
|
---|
101 |
|
---|
102 | Function killImpl() As Boolean
|
---|
103 | If handle <> 0 Then
|
---|
104 | killImpl = KillTimer(ctrl As HWND, handle) <> 0
|
---|
105 | ReleaseObjectHandle(handle)
|
---|
106 | handle = 0
|
---|
107 | End If
|
---|
108 | End Function
|
---|
109 |
|
---|
110 | Tick As System.EventHandler
|
---|
111 | ctrl As Control
|
---|
112 | handle As LONG_PTR 'タイマ実行中のGC回避およびタイマIDとして使用
|
---|
113 | interval As DWord
|
---|
114 | End Class
|
---|
115 |
|
---|
116 | End Namespace 'UI
|
---|
117 | End Namespace 'Widnows
|
---|
118 | End Namespace 'ActiveBasic
|
---|