- Timestamp:
- Jul 17, 2008, 11:20:10 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/Control.ab
r547 r551 14 14 Inherits WindowHandle 15 15 Public 16 /*! 17 @brief Destroyイベントよりも後に呼ばれるDestroyイベントデリゲート 18 @date 2008/07/16 19 */ 20 finalDestroy As ActiveBasic.Windows.UI.Handler 16 21 17 22 Sub Control() … … 34 39 Private 35 40 Static Function FromHWndCore(hwnd As HWND) As Control 36 If _System_GetClassLongPtr(hwnd, GCW_ATOM) = atom Then 37 Dim gchValue = GetProp(hwnd, PropertyInstance As ULONG_PTR As PCTSTR) As ULONG_PTR 38 If gchValue <> 0 Then 39 Dim gch = System.Runtime.InteropServices.GCHandle.FromIntPtr(gchValue) 40 FromHWndCore = gch.Target As Control 41 Exit Function 42 End If 41 Dim gchValue = GetProp(hwnd, PropertyInstance As ULONG_PTR As PCTSTR) As ULONG_PTR 42 If gchValue <> 0 Then 43 Dim gch = System.Runtime.InteropServices.GCHandle.FromIntPtr(gchValue) 44 FromHWndCore = gch.Target As Control 45 Exit Function 43 46 End If 44 47 End Function … … 55 58 56 59 Public 57 Sub Create( )60 Sub Create(parent = Nothing As Control, style = 0 As DWord, exStyle = 0 As DWord, hmenu = 0 As HMENU) 58 61 Dim cs As CREATESTRUCT 59 cs.hInstance = hInstance 60 cs.lpszClass = (atom As ULONG_PTR) As LPCTSTR 62 With cs 63 .dwExStyle = exStyle 64 .lpszClass = (atom As ULONG_PTR) As LPCTSTR 65 .lpszName = 0 66 .style = style Or WS_CHILD Or WS_VISIBLE 67 .x = CW_USEDEFAULT 68 .y = CW_USEDEFAULT 69 .cx = CW_USEDEFAULT 70 .cy = CW_USEDEFAULT 71 If IsNothing(parent) Then 72 .hwndParent = 0 73 Else 74 .hwndParent = parent As HWND 75 .style Or= WS_CHILD 76 End If 77 .hMenu = hmenu 78 .hInstance = hInstance 79 End With 61 80 GetCreateStruct(cs) 62 createImpl(cs) 63 End Sub 64 81 createImpl(cs, parent) 82 End Sub 83 84 Sub Create(parent As Control, style As DWord, exStyle As DWord, id As Long) 85 Create(parent, style, exStyle, id As HMENU) 86 End Sub 65 87 Protected 66 88 Abstract Sub GetCreateStruct(ByRef cs As CREATESTRUCT) 67 89 68 Sub createImpl(ByRef cs As CREATESTRUCT )90 Sub createImpl(ByRef cs As CREATESTRUCT, parent As Control) 69 91 Imports System.Runtime.InteropServices 70 92 … … 75 97 76 98 With cs 77 Dim hwnd = CreateWindowEx(.dwExStyle, .lpszClass, .lpszName, .style, 99 'よそのクラスの場合、WndProcFirst内のhwndの代入が行われないため、ここでも行っておく。 100 hwnd = CreateWindowEx(.dwExStyle, .lpszClass, .lpszName, .style, 78 101 .x, .y, .cx, .cy, .hwndParent, .hMenu, .hInstance, .lpCreateParams) 79 102 If hwnd = 0 Then 80 103 ActiveBasic.Windows.ThrowByWindowsError(GetLastError()) 81 104 End If 105 106 If IsNothing(FromHWndCore(hwnd)) <> False Then 107 AssociateHWnd(gch, hwnd) 108 TlsSetValue(tlsIndex, 0) 109 End If 82 110 End With 111 112 If IsNothing(parent) = False Then 113 RegisterUnassociateHWnd(parent) 114 End If 83 115 End Sub 84 116 … … 88 120 Public 89 121 Virtual Function WndProc(msg As DWord, wp As WPARAM, lp As LPARAM) As LRESULT 90 Dim h = Nothing As Message EventHandler122 Dim h = Nothing As MessageHandler 91 123 Dim b = messageMap.TryGetValue(Hex$(msg), h) 92 124 If b Then 93 125 If Not IsNothing(h) Then 94 Dim a = New Message EventArgs(hwnd, msg, wp, lp)126 Dim a = New MessageArgs(hwnd, msg, wp, lp) 95 127 h(This, a) 96 128 WndProc = a.LResult … … 106 138 107 139 Private 108 Static Function makeKeysFormMsg(e As Message EventArgs) As Keys140 Static Function makeKeysFormMsg(e As MessageArgs) As Keys 109 141 Dim t As DWord 110 142 t = e.WParam And Keys.KeyCode … … 115 147 End Function 116 148 117 Static Function makeMouseEventFromMsg(e As Message EventArgs) As MouseEventArgs149 Static Function makeMouseEventFromMsg(e As MessageArgs) As MouseArgs 118 150 Dim wp = e.WParam 119 151 Dim lp = e.LParam 120 makeMouseEventFromMsg = New Mouse EventArgs(LOWORD(wp) As MouseButtons, 1, GET_X_LPARAM(lp), GET_Y_LPARAM(lp), 0)152 makeMouseEventFromMsg = New MouseArgs(LOWORD(wp) As MouseButtons, 1, GET_X_LPARAM(lp), GET_Y_LPARAM(lp), 0) 121 153 End Function 122 154 … … 128 160 Sub StartWndProc() 129 161 AddMessageEvent(WM_ERASEBKGND, AddressOf(OnEraseBackground)) 130 Dim md = New Message EventHandler(AddressOf(OnMouseDownBase))162 Dim md = New MessageHandler(AddressOf(OnMouseDownBase)) 131 163 AddMessageEvent(WM_LBUTTONDOWN, md) 132 164 AddMessageEvent(WM_RBUTTONDOWN, md) 133 165 AddMessageEvent(WM_MBUTTONDOWN, md) 134 166 AddMessageEvent(WM_XBUTTONDOWN, md) 135 Dim mu = New Message EventHandler(AddressOf(OnMouseUpBase))167 Dim mu = New MessageHandler(AddressOf(OnMouseUpBase)) 136 168 AddMessageEvent(WM_LBUTTONUP, mu) 137 169 AddMessageEvent(WM_RBUTTONUP, mu) 138 170 AddMessageEvent(WM_MBUTTONUP, mu) 139 171 AddMessageEvent(WM_XBUTTONUP, mu) 140 Dim mb = New Message EventHandler(AddressOf(OnMouseDblClkBase))172 Dim mb = New MessageHandler(AddressOf(OnMouseDblClkBase)) 141 173 AddMessageEvent(WM_LBUTTONDBLCLK, mu) 142 174 AddMessageEvent(WM_RBUTTONDBLCLK, mu) … … 148 180 AddMessageEvent(WM_PAINT, AddressOf(OnPaintBase)) 149 181 AddMessageEvent(WM_KEYDOWN, AddressOf(OnKeyDownBase)) 150 'AddMessageEvent(WM_CHAR, AddressOf(OnChar))182 AddMessageEvent(WM_CHAR, AddressOf(OnChar)) 151 183 AddMessageEvent(WM_CREATE, AddressOf(OnCreateBase)) 152 184 End Sub 153 185 154 Sub OnEraseBackground(sender As Object, e As Message EventArgs)186 Sub OnEraseBackground(sender As Object, e As MessageArgs) 155 187 Dim rc = ClientRect 156 188 FillRect(e.WParam As HDC, rc, (COLOR_3DFACE + 1) As HBRUSH) … … 158 190 End Sub 159 191 160 Sub OnMouseDownBase(sender As Object, e As Message EventArgs)192 Sub OnMouseDownBase(sender As Object, e As MessageArgs) 161 193 OnMouseDown(makeMouseEventFromMsg(e)) 162 194 End Sub 163 195 164 Sub OnMouseUpBase(sender As Object, e As Message EventArgs)196 Sub OnMouseUpBase(sender As Object, e As MessageArgs) 165 197 Dim me = makeMouseEventFromMsg(e) 166 198 If doubleClickFired = False Then 167 ' OnClick(System. EventArgs.Empty)199 ' OnClick(System.Args.Empty) 168 200 OnMouseClick(me) 169 201 doubleClickFired = False … … 172 204 End Sub 173 205 174 Sub OnMouseDblClkBase(sender As Object, e As Message EventArgs)206 Sub OnMouseDblClkBase(sender As Object, e As MessageArgs) 175 207 Dim me = makeMouseEventFromMsg(e) 176 208 doubleClickFired = True 177 209 OnMouseDown(me) 178 ' OnDoubleClick(System. EventArgs.Empty)210 ' OnDoubleClick(System.Args.Empty) 179 211 OnMouseDoubleClick(me) 180 212 End Sub 181 213 182 Sub OnMouseMoveBase(sender As Object, e As Message EventArgs)214 Sub OnMouseMoveBase(sender As Object, e As MessageArgs) 183 215 Dim me = makeMouseEventFromMsg(e) 184 216 If mouseEntered Then … … 190 222 End Sub 191 223 192 Sub OnMouseLeaveBase(sender As Object, e As Message EventArgs)224 Sub OnMouseLeaveBase(sender As Object, e As MessageArgs) 193 225 Dim me = makeMouseEventFromMsg(e) 194 226 OnMouseLeave(me) … … 196 228 End Sub 197 229 198 Sub OnPaintBase(sender As Object, e As Message EventArgs)230 Sub OnPaintBase(sender As Object, e As MessageArgs) 199 231 Dim ps As PAINTSTRUCT 200 232 BeginPaint(ps) 201 233 Try 202 OnPaintDC(New PaintDC EventArgs(ps.hdc, ps.rcPaint))234 OnPaintDC(New PaintDCArgs(ps.hdc, ps.rcPaint)) 203 235 Finally 204 236 EndPaint(ps) … … 206 238 End Sub 207 239 208 Sub OnKeyDownBase(sender As Object, e As MessageEventArgs) 209 OnKeyDown(New KeyEventArgs(makeKeysFormMsg(e))) 210 End Sub 211 212 Sub OnKeyUpBase(sender As Object, e As MessageEventArgs) 213 OnKeyUp(New KeyEventArgs(makeKeysFormMsg(e))) 214 End Sub 215 216 ' コメントアウト解除のときはStartWndProcのコメントアウト解除も忘れないこと 217 ' Sub OnChar(sender As Object, e As MessageEventArgs) 218 ' OnKeyPress(New KeyPressEventArgs(e.WParam As Char)) 219 ' End Sub 220 221 Sub OnCreateBase(sender As Object, e As MessageEventArgs) 222 OnCreate(New CreateEventArgs(e.LParam As *CREATESTRUCT)) 223 End Sub 224 225 messageMap As System.Collections.Generic.Dictionary<Object /*DWord*/, MessageEventHandler> 240 Sub OnKeyDownBase(sender As Object, e As MessageArgs) 241 OnKeyDown(New KeyArgs(makeKeysFormMsg(e))) 242 End Sub 243 244 Sub OnKeyUpBase(sender As Object, e As MessageArgs) 245 OnKeyUp(New KeyArgs(makeKeysFormMsg(e))) 246 End Sub 247 248 Sub OnChar(sender As Object, e As MessageArgs) 249 OnKeyPress(New KeyPressArgs(e.WParam As Char)) 250 End Sub 251 252 Sub OnCreateBase(sender As Object, e As MessageArgs) 253 ' OnCreate(New CreateArgs(e.LParam As *CREATESTRUCT)) 254 End Sub 255 256 messageMap As System.Collections.Generic.Dictionary<Object /*DWord*/, MessageHandler> 226 257 227 258 Public … … 230 261 @date 2007/12/04 231 262 */ 232 Sub AddMessageEvent(message As DWord, h As Message EventHandler)263 Sub AddMessageEvent(message As DWord, h As MessageHandler) 233 264 If Not IsNothing(h) Then 234 265 If IsNothing(messageMap) Then 235 messageMap = New System.Collections.Generic.Dictionary<Object, Message EventHandler>266 messageMap = New System.Collections.Generic.Dictionary<Object, MessageHandler> 236 267 End If 237 268 Dim msg = Hex$(message) 238 Dim m = Nothing As Message EventHandler269 Dim m = Nothing As MessageHandler 239 270 If messageMap.TryGetValue(msg, m) Then 240 271 messageMap.Item[msg] = m + h … … 249 280 @date 2007/12/04 250 281 */ 251 Sub RemoveMessageEvent(message As DWord, a As Message EventHandler)282 Sub RemoveMessageEvent(message As DWord, a As MessageHandler) 252 283 If Not IsNothing(a) Then 253 284 If Not IsNothing(messageMap) Then … … 302 333 ' ウィンドウが作られて最初にWndProcFirstが呼ばれたとき 303 334 304 If IsNothing(rThis)Then335 If AssociateHWnd(gch, hwnd) = False Then 305 336 Goto *InstanceIsNotFound 306 337 End If 307 rThis.hwnd = hwnd 308 rThis.Prop[PropertyInstance] = gchValue As HANDLE 338 End If 339 If msg = WM_NCDESTROY Then 340 rThis.UnassociateHWnd() 341 End If 342 If msg = WM_DESTROY And IsNothing(rThis.finalDestroy) = False Then 343 Dim f = rThis.finalDestroy 344 f(rThis, Args.Empty) 345 ' finalDestroy(This, Args.Empty) 309 346 End If 310 347 WndProcFirst = rThis.WndProc(msg, wp, lp) 311 If msg = WM_NCDESTROY Then312 Dim gchValue = rThis.Prop(PropertyInstance) As ULONG_PTR313 If gchValue <> 0 Then314 GCHandle.FromIntPtr(gchValue).Free()315 End If316 End If317 318 348 Exit Function 319 349 … … 322 352 WndProcFirst = DefWindowProc(hwnd, msg, wp, lp) 323 353 End Function 354 355 /*! 356 @brief Controlインスタンスとウィンドウハンドルを結び付ける。 357 @param[in] 結び付けられるControlインスタンスを格納したGCHandle 358 @param[in] hwnd 結び付けるウィンドウハンドル 359 @date 2008/07/16 360 これを実行することで、UnassociateHWndされるまでControlがGC対象にならなくなる。 361 */ 362 Static Function AssociateHWnd(gch As System.Runtime.InteropServices.GCHandle, hwnd As HWND) As Boolean 363 Imports System.Runtime.InteropServices 364 Dim rThis = gch.Target As Control 365 If IsNothing(rThis) Then 366 Exit Function 367 End If 368 rThis.hwnd = hwnd 369 rThis.Prop[PropertyInstance] = GCHandle.ToIntPtr(gch) As HANDLE 370 End Function 371 372 /*! 373 @brief オーナーの破棄時にこのControlインスタンスとウィンドウハンドルの結び付けを解除するよう登録する。 374 @param[in] owner 結び付けの解除を連動させるControl 375 @date 2008/07/16 376 ownerのFinalDestoryでこのControlとHWNDとの結び付けを解除する。 377 */ 378 Sub RegisterUnassociateHWnd(owner As Control) 379 If IsNothing(owner) = False Then 380 Dim e = New Handler(AddressOf(UnassociateHWndOnEvent)) 381 If IsNothing(finalDestroy) Then 382 owner.finalDestroy = e 383 Else 384 owner.finalDestroy += e 385 End If 386 End If 387 End Sub 388 389 Sub UnassociateHWndOnEvent(sender As Object, e As Args) 390 UnassociateHWnd() 391 End Sub 392 393 Sub UnassociateHWnd() 394 Imports System.Runtime.InteropServices 395 Dim gchValue = Prop(PropertyInstance) As ULONG_PTR 396 If gchValue <> 0 Then 397 GCHandle.FromIntPtr(gchValue).Free() 398 End If 399 End Sub 324 400 325 401 ' Static Const WM_CONTROL_INVOKE = WM_USER + 0 As DWord
Note:
See TracChangeset
for help on using the changeset viewer.