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