| 1 | 'Classes/ActiveBasic/Windows/UI/Control.ab
|
|---|
| 2 |
|
|---|
| 3 | #require <Classes/ActiveBasic/Windows/UI/EventArgs.ab>
|
|---|
| 4 | #require <Classes/ActiveBasic/COM/ComClassBase.ab>
|
|---|
| 5 |
|
|---|
| 6 | Namespace ActiveBasic
|
|---|
| 7 | Namespace Windows
|
|---|
| 8 | Namespace UI
|
|---|
| 9 |
|
|---|
| 10 | Namespace Detail
|
|---|
| 11 | TypeDef PTrackMouseEvent = *Function(ByRef tme As TRACKMOUSEEVENT) As BOOL
|
|---|
| 12 | End Namespace
|
|---|
| 13 |
|
|---|
| 14 | /*
|
|---|
| 15 | @brief Windowsのウィンドウを管理する基底クラス
|
|---|
| 16 | @auther Egtra
|
|---|
| 17 | */
|
|---|
| 18 | Class Control
|
|---|
| 19 | Inherits WindowHandle
|
|---|
| 20 | Implements ActiveBasic.COM.InterfaceQuerable
|
|---|
| 21 | Public
|
|---|
| 22 | /*!
|
|---|
| 23 | @brief Destroyイベントよりも後に呼ばれるDestroyイベントデリゲート
|
|---|
| 24 | @date 2008/07/16
|
|---|
| 25 | */
|
|---|
| 26 | finalDestroy As ActiveBasic.Windows.UI.Handler
|
|---|
| 27 |
|
|---|
| 28 | Sub Control()
|
|---|
| 29 | comImpl = New COM.ComClassDelegationImpl(This)
|
|---|
| 30 | End Sub
|
|---|
| 31 |
|
|---|
| 32 | Function Handle() As HWND
|
|---|
| 33 | Handle = hwnd
|
|---|
| 34 | End Function
|
|---|
| 35 |
|
|---|
| 36 | /*!
|
|---|
| 37 | @brief HWNDからControlインスタンスを取得する。
|
|---|
| 38 | @param[in] hwnd 対象のウィンドウハンドル
|
|---|
| 39 | @return 対応するControlインスタンス。存在しなければ作成される。ただし、hwndがNULLならNothing。
|
|---|
| 40 | */
|
|---|
| 41 | Static Function FromHWnd(hwnd As HWND) As Control
|
|---|
| 42 | FromHWnd = Nothing
|
|---|
| 43 | If IsWindow(hwnd) Then
|
|---|
| 44 | FromHWnd = FromHWndCore(hwnd)
|
|---|
| 45 | If ActiveBasic.IsNothing(FromHWnd) Then
|
|---|
| 46 | Dim lock = New ActiveBasic.Windows.CriticalSectionLock(_System_CriticalSection)
|
|---|
| 47 | Try
|
|---|
| 48 | FromHWnd = New Control
|
|---|
| 49 | FromHWnd.registerStandardEvent()
|
|---|
| 50 | FromHWnd.AssociateHWnd(hwnd)
|
|---|
| 51 | Finally
|
|---|
| 52 | lock.Dispose()
|
|---|
| 53 | End Try
|
|---|
| 54 | End If
|
|---|
| 55 | End If
|
|---|
| 56 | End Function
|
|---|
| 57 |
|
|---|
| 58 | Private
|
|---|
| 59 | Static Function FromHWndCore(hwnd As HWND) As Control
|
|---|
| 60 | FromHWndCore = _System_PtrObj(GetProp(hwnd, PropertyInstance As ULONG_PTR As PCTSTR) As VoidPtr) As Control
|
|---|
| 61 | End Function
|
|---|
| 62 |
|
|---|
| 63 | '--------------------------------
|
|---|
| 64 | ' ウィンドウ作成
|
|---|
| 65 |
|
|---|
| 66 | Public
|
|---|
| 67 | /*!
|
|---|
| 68 | @brief ウィンドウを作成する(詳細版)。
|
|---|
| 69 | @date 2008/08/02
|
|---|
| 70 | 通常はCreateやCreateFormその他を使ってください。
|
|---|
| 71 | */
|
|---|
| 72 | Sub CreateEx(parent As Control, style As DWord, exStyle As DWord, hmenu As HMENU)
|
|---|
| 73 | Dim cs As CREATESTRUCT
|
|---|
| 74 | With cs
|
|---|
| 75 | .dwExStyle = exStyle
|
|---|
| 76 | .lpszClass = (atom As ULONG_PTR) As LPCTSTR
|
|---|
| 77 | .lpszName = 0
|
|---|
| 78 | .style = style
|
|---|
| 79 | .x = 0
|
|---|
| 80 | .y = 0
|
|---|
| 81 | .cx = 0
|
|---|
| 82 | .cy = 0
|
|---|
| 83 | If IsNothing(parent) Then
|
|---|
| 84 | .hwndParent = 0
|
|---|
| 85 | Else
|
|---|
| 86 | .hwndParent = parent As HWND
|
|---|
| 87 | End If
|
|---|
| 88 | .hMenu = hmenu
|
|---|
| 89 | .hInstance = hInstance
|
|---|
| 90 | End With
|
|---|
| 91 | GetCreateStruct(cs)
|
|---|
| 92 | createImpl(cs, parent)
|
|---|
| 93 | End Sub
|
|---|
| 94 |
|
|---|
| 95 | /*!
|
|---|
| 96 | @brief ウィンドウを作成する(子ウィンドウ以外)。
|
|---|
| 97 | @date 2008/08/02
|
|---|
| 98 | */
|
|---|
| 99 | Sub CreateForm(style As DWord, exStyle As DWord, owner = Nothing As Control, hmenu = 0 As HMENU)
|
|---|
| 100 | CreateEx(owner, style, exStyle, hmenu)
|
|---|
| 101 | End Sub
|
|---|
| 102 |
|
|---|
| 103 | Sub CreateForm()
|
|---|
| 104 | CreateEx(Nothing, 0, 0, 0)
|
|---|
| 105 | End Sub
|
|---|
| 106 |
|
|---|
| 107 | /*!
|
|---|
| 108 | @brief 子ウィンドウを作成する。
|
|---|
| 109 | @date 2008/08/02
|
|---|
| 110 | */
|
|---|
| 111 | Sub Create(parent As Control, style = 0 As DWord, exStyle = 0 As DWord, id = 0 As Long)
|
|---|
| 112 | CreateEx(parent, style Or WS_CHILD Or WS_VISIBLE, exStyle, id As ULONG_PTR As HMENU)
|
|---|
| 113 | End Sub
|
|---|
| 114 |
|
|---|
| 115 | Protected
|
|---|
| 116 | Virtual Sub GetCreateStruct(ByRef cs As CREATESTRUCT)
|
|---|
| 117 | End Sub
|
|---|
| 118 |
|
|---|
| 119 | Sub createImpl(ByRef cs As CREATESTRUCT, parent As Control)
|
|---|
| 120 | throwIfAlreadyCreated()
|
|---|
| 121 |
|
|---|
| 122 | StartWndProc()
|
|---|
| 123 |
|
|---|
| 124 | With cs
|
|---|
| 125 | 'よそのウィンドウクラスの場合、WndProcFirst内のhwndの代入が行われないため、ここでも行っておく。
|
|---|
| 126 | hwnd = CreateWindowEx(.dwExStyle, .lpszClass, .lpszName, .style,
|
|---|
| 127 | .x, .y, .cx, .cy, .hwndParent, .hMenu, .hInstance, .lpCreateParams)
|
|---|
| 128 | If hwnd = 0 Then
|
|---|
| 129 | ThrowWithLastErrorNT("Control.CreateEx")
|
|---|
| 130 | End If
|
|---|
| 131 |
|
|---|
| 132 | If IsNothing(FromHWndCore(hwnd)) <> False Then
|
|---|
| 133 | AssociateHWnd(hwnd)
|
|---|
| 134 | TlsSetValue(tlsIndex, 0)
|
|---|
| 135 | End If
|
|---|
| 136 | End With
|
|---|
| 137 |
|
|---|
| 138 | If IsNothing(parent) = False Then
|
|---|
| 139 | RegisterUnassociateHWnd(parent)
|
|---|
| 140 | End If
|
|---|
| 141 | End Sub
|
|---|
| 142 |
|
|---|
| 143 | Public
|
|---|
| 144 | /*
|
|---|
| 145 | Sub Attach(hwndNew As HWND)
|
|---|
| 146 | throwIfAlreadyCreated()
|
|---|
| 147 | If hwndNew = 0 Then
|
|---|
| 148 | Throw New System.ArgumentNullException("Control.Attach")
|
|---|
| 149 | End If
|
|---|
| 150 | registerStandardEvent()
|
|---|
| 151 | AssociateHWnd(hwndNew)
|
|---|
| 152 | End Sub
|
|---|
| 153 | */
|
|---|
| 154 | Sub BeginSubclass()
|
|---|
| 155 | throwIfNotCreated()
|
|---|
| 156 | prevWndProc = SetWindowLongPtr(hwnd, GWLP_WNDPROC, AddressOf(WndProcFirst) As LONG_PTR) As WNDPROC
|
|---|
| 157 | End Sub
|
|---|
| 158 |
|
|---|
| 159 | Private
|
|---|
| 160 | Sub throwIfAlreadyCreated()
|
|---|
| 161 | If hwnd <> 0 Then
|
|---|
| 162 | Throw New System.InvalidOperationException("Window is already created.")
|
|---|
| 163 | End If
|
|---|
| 164 | End Sub
|
|---|
| 165 |
|
|---|
| 166 | Sub throwIfNotCreated()
|
|---|
| 167 | If hwnd = 0 Then
|
|---|
| 168 | Throw New System.InvalidOperationException("Window is not already created.")
|
|---|
| 169 | End If
|
|---|
| 170 | End Sub
|
|---|
| 171 |
|
|---|
| 172 | '--------------------------------
|
|---|
| 173 | ' ウィンドウプロシージャ
|
|---|
| 174 |
|
|---|
| 175 | Protected
|
|---|
| 176 | Virtual Function WndProc(msg As DWord, wp As WPARAM, lp As LPARAM) As LRESULT
|
|---|
| 177 | If Not ProcessMessage(msg, wp, lp, WndProc) Then
|
|---|
| 178 | WndProc = DefWndProc(msg, wp, lp)
|
|---|
| 179 | End If
|
|---|
| 180 | End Function
|
|---|
| 181 |
|
|---|
| 182 | Virtual Function DefWndProc(msg As DWord, wp As WPARAM, lp As LPARAM) As LRESULT
|
|---|
| 183 | If prevWndProc Then
|
|---|
| 184 | DefWndProc = CallWindowProc(prevWndProc, hwnd, msg, wp, lp)
|
|---|
| 185 | Else
|
|---|
| 186 | DefWndProc = DefWindowProc(hwnd, msg, wp, lp)
|
|---|
| 187 | End If
|
|---|
| 188 | End Function
|
|---|
| 189 | Protected
|
|---|
| 190 | Function ProcessMessage(msg As DWord, wp As WPARAM, lp As LPARAM, ByRef lr As LRESULT) As Boolean
|
|---|
| 191 | Dim h = Nothing As MessageHandler
|
|---|
| 192 | Dim b = messageMap.TryGetValue(Hex$(msg), h)
|
|---|
| 193 | If b Then
|
|---|
| 194 | If Not IsNothing(h) Then
|
|---|
| 195 | Dim a = New MessageArgs(hwnd, msg, wp, lp)
|
|---|
| 196 | h(This, a)
|
|---|
| 197 | If a.Handled Then
|
|---|
| 198 | lr = a.LResult
|
|---|
| 199 | ProcessMessage = True
|
|---|
| 200 | Exit Function
|
|---|
| 201 | End If
|
|---|
| 202 | End If
|
|---|
| 203 | End If
|
|---|
| 204 | ProcessMessage = False
|
|---|
| 205 | End Function
|
|---|
| 206 | Private
|
|---|
| 207 | Static Function makeKeysFormMsg(e As MessageArgs) As Keys
|
|---|
| 208 | Dim t As DWord
|
|---|
| 209 | t = e.WParam And Keys.KeyCode
|
|---|
| 210 | t Or= (GetKeyState(VK_SHIFT) As Word And &h8000) << 1
|
|---|
| 211 | t Or= (GetKeyState(VK_CONTROL) As Word And &h8000) << 2
|
|---|
| 212 | t Or= (GetKeyState(VK_MENU) As Word And &h8000) << 3
|
|---|
| 213 | makeKeysFormMsg = t As Keys
|
|---|
| 214 | End Function
|
|---|
| 215 |
|
|---|
| 216 | Static Function makeMouseEventFromMsg(e As MessageArgs) As MouseArgs
|
|---|
| 217 | Dim wp = e.WParam
|
|---|
| 218 | Dim lp = e.LParam
|
|---|
| 219 |
|
|---|
| 220 | /*************************************************/
|
|---|
| 221 | Dim mb As MouseButtons
|
|---|
| 222 | If LOWORD(wp) = 0 Then mb = MouseButtons.None
|
|---|
| 223 | If LOWORD(wp) and MK_LBUTTON Then mb or = MouseButtons.Left
|
|---|
| 224 | If LOWORD(wp) and MK_RBUTTON Then mb or = MouseButtons.Right
|
|---|
| 225 | If LOWORD(wp) and MK_MBUTTON Then mb or = MouseButtons.Middle
|
|---|
| 226 | If LOWORD(wp) and MK_XBUTTON1 Then mb or = MouseButtons.XButton1
|
|---|
| 227 | If LOWORD(wp) and MK_XBUTTON2 Then mb or = MouseButtons.XButton2
|
|---|
| 228 | If LOWORD(wp) and MK_SHIFT Then mb or = MouseButtons.Shift
|
|---|
| 229 | If LOWORD(wp) and MK_CONTROL Then mb or = MouseButtons.Control
|
|---|
| 230 | makeMouseEventFromMsg = New MouseArgs(mb, 1, GET_X_LPARAM(lp), GET_Y_LPARAM(lp), 0)
|
|---|
| 231 | /*************************************************/
|
|---|
| 232 |
|
|---|
| 233 | 'makeMouseEventFromMsg = New MouseArgs(LOWORD(wp) As MouseButtons, 1, GET_X_LPARAM(lp), GET_Y_LPARAM(lp), 0)
|
|---|
| 234 | End Function
|
|---|
| 235 |
|
|---|
| 236 | Protected
|
|---|
| 237 | /*!
|
|---|
| 238 | @brief 最初にウィンドウプロシージャを使うための前処理を行う関数
|
|---|
| 239 | @date 2008/07/11
|
|---|
| 240 | WndProcFirstを使うときは、この関数を呼んでおく必要がある。
|
|---|
| 241 | */
|
|---|
| 242 | Sub StartWndProc()
|
|---|
| 243 | TlsSetValue(tlsIndex, ObjPtr(This))
|
|---|
| 244 | registerStandardEvent()
|
|---|
| 245 | End Sub
|
|---|
| 246 | Private
|
|---|
| 247 | /*!
|
|---|
| 248 | @brief 主なメッセージハンドラの登録を行う関数
|
|---|
| 249 | @date 2008/08/02
|
|---|
| 250 | */
|
|---|
| 251 | Sub registerStandardEvent()
|
|---|
| 252 | AddMessageEvent(WM_ERASEBKGND, AddressOf(OnEraseBackground))
|
|---|
| 253 | Dim md = New MessageHandler(AddressOf(OnMouseDownBase))
|
|---|
| 254 | AddMessageEvent(WM_LBUTTONDOWN, md)
|
|---|
| 255 | AddMessageEvent(WM_RBUTTONDOWN, md)
|
|---|
| 256 | AddMessageEvent(WM_MBUTTONDOWN, md)
|
|---|
| 257 | AddMessageEvent(WM_XBUTTONDOWN, md)
|
|---|
| 258 | Dim mu = New MessageHandler(AddressOf(OnMouseUpBase))
|
|---|
| 259 | AddMessageEvent(WM_LBUTTONUP, mu)
|
|---|
| 260 | AddMessageEvent(WM_RBUTTONUP, mu)
|
|---|
| 261 | AddMessageEvent(WM_MBUTTONUP, mu)
|
|---|
| 262 | AddMessageEvent(WM_XBUTTONUP, mu)
|
|---|
| 263 | Dim mb = New MessageHandler(AddressOf(OnMouseDblClkBase))
|
|---|
| 264 | AddMessageEvent(WM_LBUTTONDBLCLK, mb)
|
|---|
| 265 | AddMessageEvent(WM_RBUTTONDBLCLK, mb)
|
|---|
| 266 | AddMessageEvent(WM_MBUTTONDBLCLK, mb)
|
|---|
| 267 | AddMessageEvent(WM_XBUTTONDBLCLK, mb)
|
|---|
| 268 |
|
|---|
| 269 | AddMessageEvent(WM_MOUSEMOVE, AddressOf(OnMouseMoveBase))
|
|---|
| 270 | AddMessageEvent(WM_MOUSELEAVE, AddressOf(OnMouseLeaveBase))
|
|---|
| 271 | AddMessageEvent(WM_MOUSEHOVER, AddressOf(OnMouseHoverBase))
|
|---|
| 272 | AddMessageEvent(WM_PAINT, AddressOf(OnPaintBase))
|
|---|
| 273 | AddMessageEvent(WM_KEYDOWN, AddressOf(OnKeyDownBase))
|
|---|
| 274 | AddMessageEvent(WM_CHAR, AddressOf(OnChar))
|
|---|
| 275 | AddMessageEvent(WM_CREATE, AddressOf(OnCreateBase))
|
|---|
| 276 | AddMessageEvent(WM_SIZE, AddressOf(OnSize))
|
|---|
| 277 | End Sub
|
|---|
| 278 |
|
|---|
| 279 | Sub OnEraseBackground(sender As Object, e As MessageArgs)
|
|---|
| 280 | Dim a = New PaintBackgroundArgs(e.WParam, e.LParam)
|
|---|
| 281 | e.Handled = e.Handled And OnPaintBackground(a)
|
|---|
| 282 | e.LResult = a.Painted
|
|---|
| 283 | End Sub
|
|---|
| 284 |
|
|---|
| 285 | Sub OnMouseDownBase(sender As Object, e As MessageArgs)
|
|---|
| 286 | e.Handled = e.Handled And OnMouseDown(makeMouseEventFromMsg(e))
|
|---|
| 287 | End Sub
|
|---|
| 288 |
|
|---|
| 289 | Sub OnMouseUpBase(sender As Object, e As MessageArgs)
|
|---|
| 290 | Dim me = makeMouseEventFromMsg(e)
|
|---|
| 291 | If doubleClickFired = False Then
|
|---|
| 292 | OnClick(Args.Empty)
|
|---|
| 293 | OnMouseClick(me)
|
|---|
| 294 | Else
|
|---|
| 295 | doubleClickFired = False
|
|---|
| 296 | End If
|
|---|
| 297 | e.Handled = e.Handled And OnMouseUp(me)
|
|---|
| 298 | End Sub
|
|---|
| 299 |
|
|---|
| 300 | Sub OnMouseDblClkBase(sender As Object, e As MessageArgs)
|
|---|
| 301 | Dim me = makeMouseEventFromMsg(e)
|
|---|
| 302 | doubleClickFired = True
|
|---|
| 303 | OnMouseDown(me)
|
|---|
| 304 | OnDoubleClick(Args.Empty)
|
|---|
| 305 | e.Handled = e.Handled And OnMouseDoubleClick(me)
|
|---|
| 306 | End Sub
|
|---|
| 307 |
|
|---|
| 308 | Sub OnMouseMoveBase(sender As Object, e As MessageArgs)
|
|---|
| 309 | Dim me = makeMouseEventFromMsg(e)
|
|---|
| 310 | If mouseEntered = False Then
|
|---|
| 311 | mouseEntered = True
|
|---|
| 312 | OnMouseEnter(me)
|
|---|
| 313 | trackMouseEvent(TME_LEAVE Or TME_HOVER)
|
|---|
| 314 | End If
|
|---|
| 315 | e.Handled = e.Handled And OnMouseMove(me)
|
|---|
| 316 | End Sub
|
|---|
| 317 |
|
|---|
| 318 | Sub OnMouseLeaveBase(sender As Object, e As MessageArgs)
|
|---|
| 319 | e.Handled = e.Handled And OnMouseLeave(Args.Empty)
|
|---|
| 320 | mouseEntered = False
|
|---|
| 321 | End Sub
|
|---|
| 322 |
|
|---|
| 323 | Sub OnMouseHoverBase(sender As Object, e As MessageArgs)
|
|---|
| 324 | Dim me = makeMouseEventFromMsg(e)
|
|---|
| 325 | e.Handled = e.Handled And OnMouseHover(me)
|
|---|
| 326 | End Sub
|
|---|
| 327 |
|
|---|
| 328 | Sub OnPaintBase(sender As Object, e As MessageArgs)
|
|---|
| 329 | If ActiveBasic.IsNothing(paintDC) Then
|
|---|
| 330 | e.Handled = False
|
|---|
| 331 | Else
|
|---|
| 332 | Dim ps As PAINTSTRUCT
|
|---|
| 333 | BeginPaint(hwnd, ps)
|
|---|
| 334 | Try
|
|---|
| 335 | OnPaintDC(New PaintDCArgs(ps.hdc, ps.rcPaint))
|
|---|
| 336 | Finally
|
|---|
| 337 | EndPaint(hwnd, ps)
|
|---|
| 338 | End Try
|
|---|
| 339 | End If
|
|---|
| 340 | End Sub
|
|---|
| 341 |
|
|---|
| 342 | Sub OnKeyDownBase(sender As Object, e As MessageArgs)
|
|---|
| 343 | e.Handled = e.Handled And OnKeyDown(New KeyArgs(makeKeysFormMsg(e)))
|
|---|
| 344 | End Sub
|
|---|
| 345 |
|
|---|
| 346 | Sub OnKeyUpBase(sender As Object, e As MessageArgs)
|
|---|
| 347 | e.Handled = e.Handled And OnKeyUp(New KeyArgs(makeKeysFormMsg(e)))
|
|---|
| 348 | End Sub
|
|---|
| 349 |
|
|---|
| 350 | Sub OnChar(sender As Object, e As MessageArgs)
|
|---|
| 351 | e.Handled = e.Handled And OnKeyPress(New KeyPressArgs(e.WParam As Char))
|
|---|
| 352 | End Sub
|
|---|
| 353 |
|
|---|
| 354 | Sub OnCreateBase(sender As Object, e As MessageArgs)
|
|---|
| 355 | e.Handled = e.Handled And OnCreate(New CreateArgs(e.LParam As *CREATESTRUCT))
|
|---|
| 356 | End Sub
|
|---|
| 357 |
|
|---|
| 358 | Sub OnSize(sender As Object, e As MessageArgs)
|
|---|
| 359 | e.Handled = e.Handled And OnResize(New ResizeArgs(e.WParam, e.LParam))
|
|---|
| 360 | End Sub
|
|---|
| 361 |
|
|---|
| 362 | messageMap As System.Collections.Generic.Dictionary<Object /*DWord*/, MessageHandler>
|
|---|
| 363 |
|
|---|
| 364 | Public
|
|---|
| 365 | /*!
|
|---|
| 366 | @biref メッセージイベントハンドラを登録する。
|
|---|
| 367 | @date 2007/12/04
|
|---|
| 368 | */
|
|---|
| 369 | Sub AddMessageEvent(message As DWord, h As MessageHandler)
|
|---|
| 370 | If Not IsNothing(h) Then
|
|---|
| 371 | If IsNothing(messageMap) Then
|
|---|
| 372 | messageMap = New System.Collections.Generic.Dictionary<Object, MessageHandler>
|
|---|
| 373 | End If
|
|---|
| 374 | Dim msg = Hex$(message)
|
|---|
| 375 | Dim m = Nothing As MessageHandler
|
|---|
| 376 | If messageMap.TryGetValue(msg, m) Then
|
|---|
| 377 | messageMap.Item[msg] = m + h
|
|---|
| 378 | Else
|
|---|
| 379 | messageMap.Item[msg] = h
|
|---|
| 380 | End If
|
|---|
| 381 | End If
|
|---|
| 382 | End Sub
|
|---|
| 383 |
|
|---|
| 384 | /*!
|
|---|
| 385 | @biref メッセージイベントハンドラ登録を解除する。
|
|---|
| 386 | @date 2007/12/04
|
|---|
| 387 | */
|
|---|
| 388 | Sub RemoveMessageEvent(message As DWord, a As MessageHandler)
|
|---|
| 389 | If Not IsNothing(a) Then
|
|---|
| 390 | If Not IsNothing(messageMap) Then
|
|---|
| 391 | Dim msg = Hex$(message)
|
|---|
| 392 | Dim m = messageMap.Item[msg]
|
|---|
| 393 | If Not IsNothing(m) Then
|
|---|
| 394 | messageMap.Item[msg] = m - a
|
|---|
| 395 | End If
|
|---|
| 396 | End If
|
|---|
| 397 | End If
|
|---|
| 398 | End Sub
|
|---|
| 399 |
|
|---|
| 400 | '--------------------------------
|
|---|
| 401 | ' ウィンドウメッセージ処理
|
|---|
| 402 |
|
|---|
| 403 |
|
|---|
| 404 | '--------
|
|---|
| 405 | 'イベント
|
|---|
| 406 |
|
|---|
| 407 | #include "ControlEvent.sbp"
|
|---|
| 408 |
|
|---|
| 409 | '--------------------------------
|
|---|
| 410 | ' インスタンスメンバ変数
|
|---|
| 411 | Private
|
|---|
| 412 | /*!
|
|---|
| 413 | @brief サブクラス化前のウィンドウプロシージャ
|
|---|
| 414 | @date 2008/08/23
|
|---|
| 415 | サブクラス化していなければNULL
|
|---|
| 416 | */
|
|---|
| 417 | prevWndProc As WNDPROC
|
|---|
| 418 | /*!
|
|---|
| 419 | @brief マウスカーソルがクライアント領域に入っているかどうかのフラグ
|
|---|
| 420 | 外から中に入ってきたときにMouseEnterイベントを発生させるために用意している。
|
|---|
| 421 | */
|
|---|
| 422 | mouseEntered As Boolean
|
|---|
| 423 | /*!
|
|---|
| 424 | @brief ダブルクリックが起こったかどうかのフラグ
|
|---|
| 425 | Click/MouseClickイベントのために用意している。
|
|---|
| 426 | @date 2008/07/12
|
|---|
| 427 | */
|
|---|
| 428 | doubleClickFired As Boolean
|
|---|
| 429 |
|
|---|
| 430 | '--------------------------------
|
|---|
| 431 | ' 初期ウィンドウクラス
|
|---|
| 432 | Protected
|
|---|
| 433 | Static Function WndProcFirst(hwnd As HWND, msg As DWord, wp As WPARAM, lp As LPARAM) As LRESULT
|
|---|
| 434 | Imports System.Runtime.InteropServices
|
|---|
| 435 |
|
|---|
| 436 | Dim rThis = FromHWndCore(hwnd)
|
|---|
| 437 | If IsNothing(rThis) Then
|
|---|
| 438 | rThis = _System_PtrObj(TlsGetValue(tlsIndex)) As Control
|
|---|
| 439 | TlsSetValue(tlsIndex, 0)
|
|---|
| 440 | If IsNothing(rThis) Then
|
|---|
| 441 | Goto *InstanceIsNotFound
|
|---|
| 442 | End If
|
|---|
| 443 | ' ウィンドウが作られて最初にWndProcFirstが呼ばれたとき
|
|---|
| 444 | rThis.AssociateHWnd(hwnd)
|
|---|
| 445 | End If
|
|---|
| 446 | If msg = WM_NCDESTROY Then
|
|---|
| 447 | rThis.UnassociateHWnd()
|
|---|
| 448 | rThis.hwnd = 0
|
|---|
| 449 | End If
|
|---|
| 450 | If msg = WM_DESTROY And IsNothing(rThis.finalDestroy) = False Then
|
|---|
| 451 | Dim f = rThis.finalDestroy
|
|---|
| 452 | f(rThis, Args.Empty)
|
|---|
| 453 | End If
|
|---|
| 454 | WndProcFirst = rThis.WndProc(msg, wp, lp)
|
|---|
| 455 | Exit Function
|
|---|
| 456 |
|
|---|
| 457 | *InstanceIsNotFound
|
|---|
| 458 | Dim err = String.Concat("Control.WndProcFirst: The attached instance is not found. msg = &h",
|
|---|
| 459 | Hex$(msg), Ex"\r\n")
|
|---|
| 460 | OutputDebugString(ToTCStr(err))
|
|---|
| 461 | WndProcFirst = DefWindowProc(hwnd, msg, wp, lp)
|
|---|
| 462 | End Function
|
|---|
| 463 |
|
|---|
| 464 | /*!
|
|---|
| 465 | @brief Controlインスタンスとウィンドウハンドルを結び付ける。
|
|---|
| 466 | @param[in] hwndNew 結び付けるウィンドウハンドル
|
|---|
| 467 | @date 2008/07/16
|
|---|
| 468 | これを実行することで、UnassociateHWndされるまでControlがGC対象にならなくなると共に、
|
|---|
| 469 | FromHWndでウィンドウハンドルからControlインスタンスの取得が可能となる。
|
|---|
| 470 | */
|
|---|
| 471 | Sub AssociateHWnd(hwndNew As HWND)
|
|---|
| 472 | SetHWnd(hwndNew)
|
|---|
| 473 | Prop[PropertyInstance] = ObjPtr(This) As HANDLE
|
|---|
| 474 | comImpl.AddRef()
|
|---|
| 475 | End Sub
|
|---|
| 476 |
|
|---|
| 477 | /*!
|
|---|
| 478 | @brief オーナーの破棄時にこのControlインスタンスとウィンドウハンドルの結び付けを解除するよう登録する。
|
|---|
| 479 | @param[in] owner 結び付けの解除を連動させるControl
|
|---|
| 480 | @date 2008/07/16
|
|---|
| 481 | ownerのFinalDestoryでこのControlとHWNDとの結び付けを解除する。
|
|---|
| 482 | */
|
|---|
| 483 | Sub RegisterUnassociateHWnd(owner As Control)
|
|---|
| 484 | If IsNothing(owner) = False Then
|
|---|
| 485 | Dim e = New Handler(AddressOf(UnassociateHWndOnEvent))
|
|---|
| 486 | If IsNothing(finalDestroy) Then
|
|---|
| 487 | owner.finalDestroy = e
|
|---|
| 488 | Else
|
|---|
| 489 | owner.finalDestroy += e
|
|---|
| 490 | End If
|
|---|
| 491 | End If
|
|---|
| 492 | End Sub
|
|---|
| 493 |
|
|---|
| 494 | Sub UnassociateHWndOnEvent(sender As Object, e As Args)
|
|---|
| 495 | UnassociateHWnd()
|
|---|
| 496 | hwnd = 0
|
|---|
| 497 | End Sub
|
|---|
| 498 |
|
|---|
| 499 | Sub UnassociateHWnd()
|
|---|
| 500 | comImpl.Release()
|
|---|
| 501 | End Sub
|
|---|
| 502 |
|
|---|
| 503 | ' Static Const WM_CONTROL_INVOKE = WM_USER + 0 As DWord
|
|---|
| 504 | ' Static Const WM_CONTROL_BEGININVOKE = WM_USER + 1 As DWord
|
|---|
| 505 |
|
|---|
| 506 | '--------------------------------
|
|---|
| 507 | ' インタフェース実装
|
|---|
| 508 |
|
|---|
| 509 | Public
|
|---|
| 510 | Virtual Function QueryInterfaceImpl(ByRef iid As IID, ByRef pv As VoidPtr) As HRESULT
|
|---|
| 511 | QueryInterfaceImpl = E_NOTIMPL
|
|---|
| 512 | End Function
|
|---|
| 513 |
|
|---|
| 514 | Private
|
|---|
| 515 | /*!
|
|---|
| 516 | @brief ウィンドウの寿命管理
|
|---|
| 517 | Controlには次のAddRef-Releaseの対がある。
|
|---|
| 518 | @li createImpl - WM_NCDESTROY(ウィンドウプロシージャがWndProcFirstの場合)
|
|---|
| 519 | @li createImpl - UnassociateHWnd←UnassociateHWndOnEvent←RegisterUnassociateHWnd(その他のウィンドウクラスの場合)
|
|---|
| 520 | @li Attach - WM_NCDESTROY(サブクラス化された場合)
|
|---|
| 521 | なお、Control派生クラスをサブクラス化すると、後ろ2つが両方適用される。
|
|---|
| 522 | */
|
|---|
| 523 | comImpl As COM.ComClassDelegationImpl
|
|---|
| 524 |
|
|---|
| 525 | '--------------------------------
|
|---|
| 526 | ' その他の補助関数
|
|---|
| 527 | Private
|
|---|
| 528 | Function trackMouseEvent(flags As DWord) As BOOL
|
|---|
| 529 | If pTrackMouseEvent <> 0 Then
|
|---|
| 530 | Dim tme As TRACKMOUSEEVENT
|
|---|
| 531 | With tme
|
|---|
| 532 | .cbSize = Len(tme)
|
|---|
| 533 | .dwFlags = flags
|
|---|
| 534 | .hwndTrack = hwnd
|
|---|
| 535 | .dwHoverTime = HOVER_DEFAULT
|
|---|
| 536 | End With
|
|---|
| 537 | trackMouseEvent = pTrackMouseEvent(tme)
|
|---|
| 538 | End If
|
|---|
| 539 | End Function
|
|---|
| 540 |
|
|---|
| 541 | '--------------------------------
|
|---|
| 542 | ' 初期化終了関連(特にウィンドウクラス)
|
|---|
| 543 | Private
|
|---|
| 544 | 'ウィンドウ作成時にウィンドウプロシージャへThisを伝えるためのもの
|
|---|
| 545 | Static tlsIndex As DWord
|
|---|
| 546 |
|
|---|
| 547 | Static hInstance As HINSTANCE
|
|---|
| 548 | Static atom As ATOM
|
|---|
| 549 | Static hmodComctl As HMODULE
|
|---|
| 550 | Static pTrackMouseEvent As Detail.PTrackMouseEvent
|
|---|
| 551 |
|
|---|
| 552 | Static Const WindowClassName = "ActiveBasic.Windows.UI.Control"
|
|---|
| 553 | Static Const PropertyInstance = 0 As ATOM
|
|---|
| 554 | Public
|
|---|
| 555 | Static Sub Initialize(hinst As HINSTANCE)
|
|---|
| 556 | tlsIndex = TlsAlloc()
|
|---|
| 557 | If tlsIndex = TLS_OUT_OF_INDEXES Then
|
|---|
| 558 | ThrowWithLastError("Control.Initialize: TlsAlloc failed.")
|
|---|
| 559 | End If
|
|---|
| 560 | hInstance = hinst
|
|---|
| 561 | hmodComctl = LoadLibrary("comctl32.dll")
|
|---|
| 562 | pTrackMouseEvent = GetProcAddress(hmodComctl, ToMBStr("_TrackMouseEvent")) As Detail.PTrackMouseEvent
|
|---|
| 563 |
|
|---|
| 564 | Dim PropertyInstanceString = WindowClassName + " " + Hex$(GetCurrentProcessId())
|
|---|
| 565 | PropertyInstance = GlobalAddAtom(ToTCStr(PropertyInstanceString))
|
|---|
| 566 | If PropertyInstance = 0 Then
|
|---|
| 567 | ThrowWithLastError("Control.Initialize: GlobalAddAtom failed.")
|
|---|
| 568 | End If
|
|---|
| 569 |
|
|---|
| 570 | Dim wcx As WNDCLASSEX
|
|---|
| 571 | With wcx
|
|---|
| 572 | .cbSize = Len (wcx)
|
|---|
| 573 | .style = CS_HREDRAW Or CS_VREDRAW Or CS_DBLCLKS
|
|---|
| 574 | .lpfnWndProc = AddressOf (WndProcFirst)
|
|---|
| 575 | .cbClsExtra = 0
|
|---|
| 576 | .cbWndExtra = 0
|
|---|
| 577 | .hInstance = hinst
|
|---|
| 578 | .hIcon = 0
|
|---|
| 579 | .hCursor = LoadImage(0, MAKEINTRESOURCE(IDC_ARROW), IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE Or LR_SHARED) As HCURSOR
|
|---|
| 580 | .hbrBackground = 0
|
|---|
| 581 | .lpszMenuName = 0
|
|---|
| 582 | .lpszClassName = ToTCStr(WindowClassName)
|
|---|
| 583 | .hIconSm = 0
|
|---|
| 584 | End With
|
|---|
| 585 | atom = RegisterClassEx(wcx)
|
|---|
| 586 | If atom = 0 Then
|
|---|
| 587 | ThrowWithLastErrorNT("Control.Initialize: RegisterClasseEx failed.")
|
|---|
| 588 | End If
|
|---|
| 589 | End Sub
|
|---|
| 590 |
|
|---|
| 591 | Static Sub Uninitialize()
|
|---|
| 592 | If atom <> 0 Then
|
|---|
| 593 | UnregisterClass(atom As ULONG_PTR As PCSTR, hInstance)
|
|---|
| 594 | End If
|
|---|
| 595 | If tlsIndex <> 0 And tlsIndex <> &hffffffff Then
|
|---|
| 596 | TlsFree(tlsIndex)
|
|---|
| 597 | End If
|
|---|
| 598 | If hmodComctl <> 0 Then
|
|---|
| 599 | FreeLibrary(hmodComctl)
|
|---|
| 600 | End If
|
|---|
| 601 | If PropertyInstance <> 0 Then
|
|---|
| 602 | GlobalDeleteAtom(PropertyInstance)
|
|---|
| 603 | End If
|
|---|
| 604 | End Sub
|
|---|
| 605 | End Class
|
|---|
| 606 |
|
|---|
| 607 | /*!
|
|---|
| 608 | @brief WM_COMMANDで通知を行うコントロールの基底クラス
|
|---|
| 609 | @date 2008/08/28
|
|---|
| 610 | @auther Egtra
|
|---|
| 611 | 親がWM_COMMANDを受け取ったら、RaiseCommandEventを呼ぶことを意図している。
|
|---|
| 612 | (Formで実装済み)
|
|---|
| 613 | */
|
|---|
| 614 | Class WmCommandControl
|
|---|
| 615 | Inherits Control
|
|---|
| 616 | Public
|
|---|
| 617 | Abstract Function RaiseCommandEvent(notificationCode As Word) As Boolean
|
|---|
| 618 | End Class
|
|---|
| 619 |
|
|---|
| 620 | End Namespace 'UI
|
|---|
| 621 | End Namespace 'Widnows
|
|---|
| 622 | End Namespace 'ActiveBasic
|
|---|