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