- Timestamp:
- Jul 13, 2008, 2:29:17 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/Forms/Control.ab
r473 r542 8 8 Namespace Forms 9 9 10 'Namespace Detail 11 ' TypeDef PTrackMouseEvent = *Function(ByRef tme As TRACKMOUSEEVENT) As BOOL 12 'End Namespace 13 10 14 Class Control 11 15 Public … … 19 23 End Sub 20 24 21 Function Handle() As WindowHandle22 Handle = wnd25 Function Handle() As HWND 26 Handle = hwnd 23 27 End Function 24 28 … … 53 57 menu = 0 As HMENU) As HWND 54 58 */ 59 55 60 Public 56 61 Function Create() As Boolean … … 71 76 TlsSetValue(tlsIndex, GCHandle.ToIntPtr(gch) As VoidPtr) 72 77 78 StartWndProc() 79 73 80 With cs 74 81 Dim hwnd = CreateWindowEx(.dwExStyle, .lpszClass, .lpszName, .style, … … 83 90 Public 84 91 Virtual Function WndProc(msg As DWord, wp As WPARAM, lp As LPARAM) As LRESULT 92 /* 85 93 Select Case msg 86 Case WM_ERASEBKGND 87 Dim rc = wnd.ClientRect 88 Dim e = New PaintDCHandledEventArgs(wp As HDC, rc) 89 OnPaintBackground(e) 90 WndProc = e.Handled 91 Case WM_PAINT 92 Dim ps As PAINTSTRUCT 93 wnd.BeginPaint(ps) 94 Try 95 Dim e = New PaintDCEventArgs(ps.hdc, ps.rcPaint) 96 OnPaintDC(e) 97 Finally 98 wnd.EndPaint(ps) 99 End Try 100 Case WM_LBUTTONDOWN, WM_RBUTTONDOWN, WM_MBUTTONDOWN, WM_XBUTTONDOWN 101 OnMouseDown(New MouseEventArgs(LOWORD(wp) As MouseButtons, 1, GET_X_LPARAM(lp), GET_Y_LPARAM(lp), 0)) 102 Case WM_LBUTTONUP, WM_RBUTTONUP, WM_MBUTTONUP, WM_XBUTTONUP 103 OnMouseUp(New MouseEventArgs(LOWORD(wp) As MouseButtons, 1, GET_X_LPARAM(lp), GET_Y_LPARAM(lp), 0)) 104 /* 94 Case WM_MOUSELEAVE 95 OnMouseLeave(makeMouseEventFromWPLP(wp, lp)) 96 mouseEntered = False 105 97 Case WM_KEYDOWN 106 98 OnKeyDown(New KeyEventArgs(makeKeysFormWPLP(wp, lp))) … … 108 100 OnKeyUp(New KeyEventArgs(makeKeysFormWPLP(wp, lp))) 109 101 Case WM_CHAR 110 OnKeyPress(New KeyPressEventArgs(w ParamAs Char))111 Case WM_ENABLE112 OnEnableChanged(EventArgs.Empty)113 Case WM_MOVE114 OnMove(EventArgs.Empty)115 Case WM_SIZE116 OnResize(EventArgs.Empty)102 OnKeyPress(New KeyPressEventArgs(wp As Char)) 103 ' Case WM_CREATE 104 Case WM_DESTROY 105 OnDestroy(EventArgs.Empty) 106 Case Else 107 WndProc = DefWndProc(msg, wp, lp) 108 End Select 117 109 */ 118 Case Else 119 WndProc = DefWndProc(msg, wp, lp) 120 End Select 110 Dim h = Nothing As MessageEventHandler 111 Dim b = messageMap.TryGetValue(Hex$(msg), h) 112 If b Then 113 If Not IsNothing(h) Then 114 Dim a = New MessageEventArgs(hwnd, msg, wp, lp) 115 h(This, a) 116 WndProc = a.LResult 117 Exit Function 118 End If 119 End If 120 WndProc = DefWndProc(msg, wp, lp) 121 121 End Function 122 122 123 123 Virtual Function DefWndProc(msg As DWord, wp As WPARAM, lp As LPARAM) As LRESULT 124 DefWndProc = DefWindowProc( wnd.HWnd, msg, wp, lp)125 End Function 126 127 Private 128 Function makeKeysFormWPLP(wp As WPARAM, lp As LPARAM) As Keys124 DefWndProc = DefWindowProc(hwnd, msg, wp, lp) 125 End Function 126 127 Private 128 Static Function makeKeysFormWPLP(wp As WPARAM, lp As LPARAM) As Keys 129 129 Dim t As DWord 130 130 t = wp And Keys.KeyCode … … 135 135 End Function 136 136 137 138 '-------------------------------- 139 ' ウィンドウメッセージ処理 140 141 '-------- 142 ' 2 143 144 Protected 137 Static Function makeMouseEventFromWPLP(wp As WPARAM, lp As LPARAM) As MouseEventArgs 138 makeMouseEventFromWPLP = New MouseEventArgs(LOWORD(wp) As MouseButtons, 1, GET_X_LPARAM(lp), GET_Y_LPARAM(lp), 0) 139 End Function 140 145 141 /*! 146 @biref ウィンドウの背景を描画する。 142 @brief 最初にウィンドウプロシージャが呼ばれるときに実行される関数 143 ここでは、主なメッセージハンドラの登録を行っている。 144 @date 2008/07/11 145 */ 146 Sub StartWndProc() 147 Dim t = This '#177対策 148 messageMap = New System.Collections.Generic.Dictionary<Object, MessageEventHandler> 149 AddMessageEvent(WM_ERASEBKGND, AddressOf(t.OnEraseBackground)) 150 Dim md = New MessageEventHandler(AddressOf(t.OnMouseDownBase)) 151 AddMessageEvent(WM_LBUTTONDOWN, md) 152 AddMessageEvent(WM_RBUTTONDOWN, md) 153 AddMessageEvent(WM_MBUTTONDOWN, md) 154 AddMessageEvent(WM_XBUTTONDOWN, md) 155 Dim mu = New MessageEventHandler(AddressOf(t.OnMouseUpBase)) 156 AddMessageEvent(WM_LBUTTONUP, mu) 157 AddMessageEvent(WM_RBUTTONUP, mu) 158 AddMessageEvent(WM_MBUTTONUP, mu) 159 AddMessageEvent(WM_XBUTTONUP, mu) 160 Dim mb = New MessageEventHandler(AddressOf(t.OnMouseDblClkBase)) 161 AddMessageEvent(WM_LBUTTONDBLCLK, mu) 162 AddMessageEvent(WM_RBUTTONDBLCLK, mu) 163 AddMessageEvent(WM_MBUTTONDBLCLK, mu) 164 AddMessageEvent(WM_XBUTTONDBLCLK, mu) 165 AddMessageEvent(WM_PAINT, AddressOf(t.OnPaintBase)) 166 End Sub 167 168 Sub OnEraseBackground(sender As Object, e As MessageEventArgs) 169 Dim rc As RECT 170 Dim r = GetClientRect(hwnd, rc) 171 FillRect(e.WParam As HDC, rc, (COLOR_3DFACE + 1) As HBRUSH) 172 e.LResult = TRUE 173 End Sub 174 175 Sub OnMouseDownBase(sender As Object, e As MessageEventArgs) 176 OnMouseDown(makeMouseEventFromWPLP(e.WParam, e.LParam)) 177 End Sub 178 179 Sub OnMouseUpBase(sender As Object, e As MessageEventArgs) 180 Dim me = makeMouseEventFromWPLP(e.WParam, e.LParam) 181 If doubleClickFired = False Then 182 OnClick(EventArgs.Empty) 183 OnMouseClick(me) 184 doubleClickFired = False 185 End If 186 OnMouseUp(me) 187 End Sub 188 189 Sub OnMouseDblClkBase(sender As Object, e As MessageEventArgs) 190 Dim me = makeMouseEventFromWPLP(e.WParam, e.LParam) 191 doubleClickFired = True 192 OnMouseDown(me) 193 OnDoubleClick(EventArgs.Empty) 194 OnMouseDoubleClick(me) 195 End Sub 196 197 Sub OnMouseMoveBase(sender As Object, e As MessageEventArgs) 198 Dim me = makeMouseEventFromWPLP(e.WParam, e.LParam) 199 If mouseEntered Then 200 OnMouseMove(me) 201 Else 202 mouseEntered = True 203 OnMouseEnter(me) 204 End If 205 End Sub 206 207 Sub OnPaintBase(sender As Object, e As MessageEventArgs) 208 Dim ps As PAINTSTRUCT 209 BeginPaint(hwnd, ps) 210 ' Try 211 ' OnPaintDC(New PaintDCEventArgs(ps.hdc, ps.rcPaint)) 212 ' Finally 213 EndPaint(hwnd, ps) 214 ' End Try 215 End Sub 216 217 218 messageMap As System.Collections.Generic.Dictionary<Object /*DWord*/, MessageEventHandler> 219 220 Public 221 /*! 222 @biref メッセージイベントハンドラを登録する。 147 223 @date 2007/12/04 148 224 */ 149 Virtual Sub OnPaintBackground(e As PaintDCBackGroundEventArgs) 150 Dim hbr = (COLOR_3DFACE + 1) As HBRUSH 151 FillRect(e.Handle, e.ClipRect, hbr) 152 e.Handled = True 153 End Sub 225 Sub AddMessageEvent(message As DWord, h As MessageEventHandler) 226 If Not IsNothing(h) Then 227 If IsNothing(messageMap) Then 228 messageMap = New System.Collections.Generic.Dictionary<Object, MessageEventHandler> 229 End If 230 Dim msg = Hex$(message) 231 Dim m = Nothing As MessageEventHandler 232 If messageMap.TryGetValue(msg, m) Then 233 messageMap.Item[msg] = m + h 234 Else 235 messageMap.Item[msg] = h 236 End If 237 End If 238 End Sub 239 240 /*! 241 @biref メッセージイベントハンドラ登録を解除する。 242 @date 2007/12/04 243 */ 244 Sub RemoveMessageEvent(message As DWord, a As MessageEventHandler) 245 If Not IsNothing(a) Then 246 If Not IsNothing(messageMap) Then 247 Dim msg = Nothing As Object : msg = New System.UInt32(message) 248 Dim m = messageMap.Item[msg] 249 If Not IsNothing(m) Then 250 messageMap.Item[msg] = m - a 251 End If 252 End If 253 End If 254 End Sub 255 256 '-------------------------------- 257 ' ウィンドウメッセージ処理 258 154 259 155 260 '-------- … … 162 267 ' 1 インスタンスメンバ変数 163 268 Private 164 wnd As WindowHandle 269 hwnd As HWND 270 /*! 271 @brief マウスカーソルがクライアント領域に入っているかどうかのフラグ 272 外から中に入ってきたときにMouseEnterイベントを発生させるために用意している。 273 */ 274 mouseEntered As Boolean 275 /*! 276 @brief ダブルクリックが起こったかどうかのフラグ 277 Click/MouseClickイベントのために用意している。 278 @date 2008/07/12 279 */ 280 doubleClickFired As Boolean 165 281 166 282 '-------------------------------- … … 184 300 Goto *InstanceIsNotFound 185 301 End If 186 rThis. wnd = New WindowHandle(hwnd)187 rThis.wnd.SetProp(PropertyInstance, gchValue As HANDLE)302 rThis.hwnd = hwnd 303 SetProp(hwnd, PropertyInstance As ULONG_PTR As PCSTR, gchValue As HANDLE) 188 304 End If 189 305 WndProcFirst = rThis.WndProc(msg, wp, lp) … … 202 318 WndProcFirst = DefWindowProc(hwnd, msg, wp, lp) 203 319 End Function 204 320 205 321 ' Static Const WM_CONTROL_INVOKE = WM_USER + 0 As DWord 206 322 ' Static Const WM_CONTROL_BEGININVOKE = WM_USER + 1 As DWord 207 323 208 324 '-------------------------------- 325 ' その他の補助関数 326 Private 327 ' Sub tracMouseEvent() 328 /* If pTrackMouseEvent <> 0 Then 329 Dim tme As TRACKMOUSEEVENT 330 With tme 331 .cbSize = Len(tme) 332 .dwFlags = TME_HOVER Or TME_LEAVE 333 .hwndTrack = wnd 334 .dwHoverTime = HOVER_DEFAULT 335 End With 336 pTrackMouseEvent(tme) 337 End If 338 */ 'End Sub 339 340 '-------------------------------- 209 341 ' 1 初期化終了関連(特にウィンドウクラス) 210 342 Private 211 343 'ウィンドウ作成時にウィンドウプロシージャへThisを伝えるためのもの 212 344 Static tlsIndex As DWord … … 214 346 Static hInstance As HINSTANCE 215 347 Static atom As ATOM 348 Static hmodComctl As HMODULE 349 ' Static pTrackMouseEvent As PTrackMouseEvent 216 350 217 351 Static Const WindowClassName = "ActiveBasic.Windows.UI.Control" … … 221 355 tlsIndex = TlsAlloc() 222 356 hInstance = hinst 357 ' hmodComctl = LoadLibrary("comctl32.dll") 358 ' pTrackMouseEvent = GetProcAddress(hmodComctl, ToMBStr("_TrackMouseEvent")) 223 359 224 360 Dim PropertyInstanceString = WindowClassName + " " + Hex$(GetCurrentProcessId()) … … 251 387 252 388 Static Sub Uninitialize() 253 UnregisterClass(atom As ULONG_PTR As PCSTR, hInstance) 254 TlsFree(tlsIndex) 255 GlobalDeleteAtom(PropertyInstance) 256 End Sub 389 If atom <> 0 Then 390 UnregisterClass(atom As ULONG_PTR As PCSTR, hInstance) 391 End If 392 If tlsIndex <> 0 And tlsIndex <> &hffffffff Then 393 TlsFree(tlsIndex) 394 End If 395 ' If hmodComctl <> 0 Then 396 ' FreeLibrary(hmodComctl) 397 ' End If 398 If PropertyInstance <> 0 Then 399 GlobalDeleteAtom(PropertyInstance) 400 End If 401 End Sub 402 257 403 End Class 258 404 259 Namespace Detail 260 Class _System_ControlIinitializer 261 Public 262 Sub _System_ControlIinitializer(hinst As HINSTANCE) 263 Control.Initialize(hinst) 264 End Sub 265 266 Sub ~_System_ControlIinitializer() 267 Control.Uninitialize() 268 End Sub 269 End Class 270 271 #ifndef _SYSTEM_NO_INITIALIZE_CONTROL_ 272 Dim _System_ControlInitializer As _System_ControlIinitializer(GetModuleHandle(0)) 273 #endif '_SYSTEM_NO_INITIALIZE_CONTROL_ 274 275 End Namespace 'Detail 276 277 Class Form 405 Class Form '仮 278 406 Inherits Control 279 407 Protected … … 294 422 End With 295 423 End Sub 296 Public '仮 424 Public 425 297 426 Override Function WndProc(msg As DWord, wp As WPARAM, lp As LPARAM) As LRESULT 298 427 WndProc = 0 299 428 Select Case msg 300 Case WM_DESTROY301 PostQuitMessage(0)302 429 Case Else 303 430 WndProc = Super.WndProc(msg, wp, lp) … … 311 438 End Namespace 'ActiveBasic 312 439 440 313 441 '---------- 314 442 'テスト実行用 … … 316 444 Imports ActiveBasic.Windows.UI.Forms 317 445 318 Class Bar 319 Public 320 Static Sub PaintDCEvent(sender As Object, et As PaintDCEventArgs) 321 Dim e = et As PaintDCEventArgs 322 TextOut(e.Handle, 10, 10, "Hello, world", 12) 323 End Sub 446 'OleInitialize() 447 Control.Initialize(GetModuleHandle(0)) 448 449 Class MyForm 450 Inherits Form 451 Public 452 Sub NcDestory(sender As Object, et As EventArgs) 453 PostQuitMessage(0) 454 End Sub 324 455 End Class 325 456 326 Dim f = New Form457 Dim f = New MyForm 327 458 f.Create() 328 Dim v = New PaintDCEventHandler(AddressOf (Bar.PaintDCEvent)) 329 f.AddPaintDC(v) 330 f.Handle.Show(SW_SHOW) 331 332 MessageBox(0, "hello", "", 0) 459 Dim h = New MessageEventHandler(AddressOf (f.NcDestory)) 460 f.AddMessageEvent(WM_NCDESTROY, h) 461 ShowWindow(f.Handle, SW_SHOW) 462 463 Dim m As MSG 464 Do 465 Dim ret = GetMessage(m, 0, 0, 0) 466 If ret = 0 Then 467 Exit Do 468 ElseIf ret = -1 Then 469 ExitProcess(-1) 470 End If 471 472 TranslateMessage(m) 473 DispatchMessage(m) 474 Loop 475 476 f = Nothing 477 System.GC.Collect() 478 479 Control.Uninitialize() 480 'OleUninitialize() 481 482 End 483
Note:
See TracChangeset
for help on using the changeset viewer.