Index: trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/Forms/Control.ab
===================================================================
--- trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/Forms/Control.ab	(revision 540)
+++ trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/Forms/Control.ab	(revision 542)
@@ -8,4 +8,8 @@
 Namespace Forms
 
+'Namespace Detail
+'	TypeDef PTrackMouseEvent = *Function(ByRef tme As TRACKMOUSEEVENT) As BOOL
+'End Namespace
+
 Class Control
 Public
@@ -19,6 +23,6 @@
 	End Sub
 
-	Function Handle() As WindowHandle
-		Handle = wnd
+	Function Handle() As HWND
+		Handle = hwnd
 	End Function
 
@@ -53,4 +57,5 @@
 		menu = 0 As HMENU) As HWND
 */
+
 Public
 	Function Create() As Boolean
@@ -71,4 +76,6 @@
 		TlsSetValue(tlsIndex, GCHandle.ToIntPtr(gch) As VoidPtr)
 
+		StartWndProc()
+
 		With cs
 			Dim hwnd = CreateWindowEx(.dwExStyle, .lpszClass, .lpszName, .style,
@@ -83,24 +90,9 @@
 Public
 	Virtual Function WndProc(msg As DWord, wp As WPARAM, lp As LPARAM) As LRESULT
+/*
 		Select Case msg
-			Case WM_ERASEBKGND
-				Dim rc = wnd.ClientRect
-				Dim e = New PaintDCHandledEventArgs(wp As HDC, rc)
-				OnPaintBackground(e)
-				WndProc = e.Handled
-			Case WM_PAINT
-				Dim ps As PAINTSTRUCT
-				wnd.BeginPaint(ps)
-				Try
-					Dim e = New PaintDCEventArgs(ps.hdc, ps.rcPaint)
-					OnPaintDC(e)
-				Finally
-					wnd.EndPaint(ps)
-				End Try
-			Case WM_LBUTTONDOWN, WM_RBUTTONDOWN, WM_MBUTTONDOWN, WM_XBUTTONDOWN
-				OnMouseDown(New MouseEventArgs(LOWORD(wp) As MouseButtons, 1, GET_X_LPARAM(lp), GET_Y_LPARAM(lp), 0))
-			Case WM_LBUTTONUP, WM_RBUTTONUP, WM_MBUTTONUP, WM_XBUTTONUP
-				OnMouseUp(New MouseEventArgs(LOWORD(wp) As MouseButtons, 1, GET_X_LPARAM(lp), GET_Y_LPARAM(lp), 0))
-/*
+			Case WM_MOUSELEAVE
+				OnMouseLeave(makeMouseEventFromWPLP(wp, lp))
+				mouseEntered = False
 			Case WM_KEYDOWN
 				OnKeyDown(New KeyEventArgs(makeKeysFormWPLP(wp, lp)))
@@ -108,23 +100,31 @@
 				OnKeyUp(New KeyEventArgs(makeKeysFormWPLP(wp, lp)))
 			Case WM_CHAR
-				OnKeyPress(New KeyPressEventArgs(wParam As Char))
-			Case WM_ENABLE
-				OnEnableChanged(EventArgs.Empty)
-			Case WM_MOVE
-				OnMove(EventArgs.Empty)
-			Case WM_SIZE
-				OnResize(EventArgs.Empty)
+				OnKeyPress(New KeyPressEventArgs(wp As Char))
+'			Case WM_CREATE
+			Case WM_DESTROY
+				OnDestroy(EventArgs.Empty)
+			Case Else
+				WndProc = DefWndProc(msg, wp, lp)
+		End Select
 */
-			Case Else
-			WndProc = DefWndProc(msg, wp, lp)
-		End Select
+		Dim h = Nothing As MessageEventHandler
+		Dim b = messageMap.TryGetValue(Hex$(msg), h)
+		If b Then
+			If Not IsNothing(h) Then
+				Dim a = New MessageEventArgs(hwnd, msg, wp, lp)
+				h(This, a)
+				WndProc = a.LResult
+				Exit Function
+			End If
+		End If
+		WndProc = DefWndProc(msg, wp, lp)
 	End Function
 
 	Virtual Function DefWndProc(msg As DWord, wp As WPARAM, lp As LPARAM) As LRESULT
-		DefWndProc = DefWindowProc(wnd.HWnd, msg, wp, lp)
-	End Function
-
-Private
-	Function makeKeysFormWPLP(wp As WPARAM, lp As LPARAM) As Keys
+		DefWndProc = DefWindowProc(hwnd, msg, wp, lp)
+	End Function
+
+Private
+	Static Function makeKeysFormWPLP(wp As WPARAM, lp As LPARAM) As Keys
 		Dim t As DWord
 		t = wp And Keys.KeyCode
@@ -135,21 +135,126 @@
 	End Function
 
-
-'--------------------------------
-' ウィンドウメッセージ処理
-
-'--------
-' 2
-
-Protected
+	Static Function makeMouseEventFromWPLP(wp As WPARAM, lp As LPARAM) As MouseEventArgs
+		makeMouseEventFromWPLP = New MouseEventArgs(LOWORD(wp) As MouseButtons, 1, GET_X_LPARAM(lp), GET_Y_LPARAM(lp), 0)
+	End Function
+
 	/*!
-	@biref	ウィンドウの背景を描画する。
+	@brief	最初にウィンドウプロシージャが呼ばれるときに実行される関数
+	ここでは、主なメッセージハンドラの登録を行っている。
+	@date	2008/07/11
+	*/
+	Sub StartWndProc()
+		Dim t = This '#177対策
+		messageMap = New System.Collections.Generic.Dictionary<Object, MessageEventHandler>
+		AddMessageEvent(WM_ERASEBKGND, AddressOf(t.OnEraseBackground))
+		Dim md = New MessageEventHandler(AddressOf(t.OnMouseDownBase))
+		AddMessageEvent(WM_LBUTTONDOWN, md)
+		AddMessageEvent(WM_RBUTTONDOWN, md)
+		AddMessageEvent(WM_MBUTTONDOWN, md)
+		AddMessageEvent(WM_XBUTTONDOWN, md)
+		Dim mu = New MessageEventHandler(AddressOf(t.OnMouseUpBase))
+		AddMessageEvent(WM_LBUTTONUP, mu)
+		AddMessageEvent(WM_RBUTTONUP, mu)
+		AddMessageEvent(WM_MBUTTONUP, mu)
+		AddMessageEvent(WM_XBUTTONUP, mu)
+		Dim mb = New MessageEventHandler(AddressOf(t.OnMouseDblClkBase))
+		AddMessageEvent(WM_LBUTTONDBLCLK, mu)
+		AddMessageEvent(WM_RBUTTONDBLCLK, mu)
+		AddMessageEvent(WM_MBUTTONDBLCLK, mu)
+		AddMessageEvent(WM_XBUTTONDBLCLK, mu)
+		AddMessageEvent(WM_PAINT, AddressOf(t.OnPaintBase))
+	End Sub
+
+	Sub OnEraseBackground(sender As Object, e As MessageEventArgs)
+		Dim rc As RECT
+		Dim r = GetClientRect(hwnd, rc)
+		FillRect(e.WParam As HDC, rc, (COLOR_3DFACE + 1) As HBRUSH)
+		e.LResult = TRUE
+	End Sub
+
+	Sub OnMouseDownBase(sender As Object, e As MessageEventArgs)
+		OnMouseDown(makeMouseEventFromWPLP(e.WParam, e.LParam))
+	End Sub
+
+	Sub OnMouseUpBase(sender As Object, e As MessageEventArgs)
+		Dim me = makeMouseEventFromWPLP(e.WParam, e.LParam)
+		If doubleClickFired = False Then
+			OnClick(EventArgs.Empty)
+			OnMouseClick(me)
+			doubleClickFired = False
+		End If
+		OnMouseUp(me)
+	End Sub
+
+	Sub OnMouseDblClkBase(sender As Object, e As MessageEventArgs)
+		Dim me = makeMouseEventFromWPLP(e.WParam, e.LParam)
+		doubleClickFired = True
+		OnMouseDown(me)
+		OnDoubleClick(EventArgs.Empty)
+		OnMouseDoubleClick(me)
+	End Sub
+
+	Sub OnMouseMoveBase(sender As Object, e As MessageEventArgs)
+		Dim me = makeMouseEventFromWPLP(e.WParam, e.LParam)
+		If mouseEntered Then
+			OnMouseMove(me)
+		Else
+			mouseEntered = True
+			OnMouseEnter(me)
+		End If
+	End Sub
+
+	Sub OnPaintBase(sender As Object, e As MessageEventArgs)
+		Dim ps As PAINTSTRUCT
+		BeginPaint(hwnd, ps)
+'		Try
+'			OnPaintDC(New PaintDCEventArgs(ps.hdc, ps.rcPaint))
+'		Finally
+			EndPaint(hwnd, ps)
+'		End Try
+	End Sub
+
+
+	messageMap As System.Collections.Generic.Dictionary<Object /*DWord*/, MessageEventHandler>
+
+Public
+	/*!
+	@biref	メッセージイベントハンドラを登録する。
 	@date	2007/12/04
 	*/
-	Virtual Sub OnPaintBackground(e As PaintDCBackGroundEventArgs)
-		Dim hbr = (COLOR_3DFACE + 1) As HBRUSH
-		FillRect(e.Handle, e.ClipRect, hbr)
-		e.Handled = True
-	End Sub
+	Sub AddMessageEvent(message As DWord, h As MessageEventHandler)
+		If Not IsNothing(h) Then
+			If IsNothing(messageMap) Then
+				messageMap = New System.Collections.Generic.Dictionary<Object, MessageEventHandler>
+			End If
+			Dim msg = Hex$(message)
+			Dim m = Nothing As MessageEventHandler
+			If messageMap.TryGetValue(msg, m) Then
+				messageMap.Item[msg] = m + h
+			Else
+				messageMap.Item[msg] = h
+			End If
+		End If
+	End Sub
+
+	/*!
+	@biref	メッセージイベントハンドラ登録を解除する。
+	@date	2007/12/04
+	*/
+	Sub RemoveMessageEvent(message As DWord, a As MessageEventHandler)
+		If Not IsNothing(a) Then
+			If Not IsNothing(messageMap) Then
+				Dim msg = Nothing As Object : msg = New System.UInt32(message)
+				Dim m = messageMap.Item[msg]
+				If Not IsNothing(m) Then
+					messageMap.Item[msg] = m - a
+				End If
+			End If
+		End If
+	End Sub
+
+'--------------------------------
+' ウィンドウメッセージ処理
+
 
 '--------
@@ -162,5 +267,16 @@
 ' 1 インスタンスメンバ変数
 Private
-	wnd As WindowHandle
+	hwnd As HWND
+	/*!
+	@brief マウスカーソルがクライアント領域に入っているかどうかのフラグ
+	外から中に入ってきたときにMouseEnterイベントを発生させるために用意している。
+	*/
+	mouseEntered As Boolean
+	/*!
+	@brief ダブルクリックが起こったかどうかのフラグ
+	Click/MouseClickイベントのために用意している。
+	@date	2008/07/12
+	*/
+	doubleClickFired As Boolean
 
 '--------------------------------
@@ -184,6 +300,6 @@
 				Goto *InstanceIsNotFound
 			End If
-			rThis.wnd = New WindowHandle(hwnd)
-			rThis.wnd.SetProp(PropertyInstance, gchValue As HANDLE)
+			rThis.hwnd = hwnd
+			SetProp(hwnd, PropertyInstance As ULONG_PTR As PCSTR, gchValue As HANDLE)
 		End If
 		WndProcFirst = rThis.WndProc(msg, wp, lp)
@@ -202,11 +318,27 @@
 		WndProcFirst = DefWindowProc(hwnd, msg, wp, lp)
 	End Function
-	
+
 '	Static Const WM_CONTROL_INVOKE = WM_USER + 0 As DWord
 '	Static Const WM_CONTROL_BEGININVOKE = WM_USER + 1 As DWord
 
 '--------------------------------
+'	その他の補助関数
+Private
+'	Sub tracMouseEvent()
+/*		If pTrackMouseEvent <> 0 Then
+			Dim tme As TRACKMOUSEEVENT
+			With tme
+				.cbSize = Len(tme)
+				.dwFlags = TME_HOVER Or TME_LEAVE
+				.hwndTrack = wnd
+				.dwHoverTime = HOVER_DEFAULT
+			End With
+			pTrackMouseEvent(tme)
+		End If
+*/	'End Sub
+
+'--------------------------------
 ' 1 初期化終了関連（特にウィンドウクラス）
-
+Private
 	'ウィンドウ作成時にウィンドウプロシージャへThisを伝えるためのもの
 	Static tlsIndex As DWord
@@ -214,4 +346,6 @@
 	Static hInstance As HINSTANCE
 	Static atom As ATOM
+	Static hmodComctl As HMODULE
+'	Static pTrackMouseEvent As PTrackMouseEvent
 
 	Static Const WindowClassName = "ActiveBasic.Windows.UI.Control"
@@ -221,4 +355,6 @@
 		tlsIndex = TlsAlloc()
 		hInstance = hinst
+'		hmodComctl = LoadLibrary("comctl32.dll")
+'		pTrackMouseEvent = GetProcAddress(hmodComctl, ToMBStr("_TrackMouseEvent"))
 
 		Dim PropertyInstanceString = WindowClassName + " " + Hex$(GetCurrentProcessId())
@@ -251,29 +387,21 @@
 
 	Static Sub Uninitialize()
-		UnregisterClass(atom As ULONG_PTR As PCSTR, hInstance)
-		TlsFree(tlsIndex)
-		GlobalDeleteAtom(PropertyInstance)
-	End Sub
+		If atom <> 0 Then
+			UnregisterClass(atom As ULONG_PTR As PCSTR, hInstance)
+		End If
+		If tlsIndex <> 0 And tlsIndex <> &hffffffff Then
+			TlsFree(tlsIndex)
+		End If
+'		If hmodComctl <> 0 Then
+'			FreeLibrary(hmodComctl)
+'		End If
+		If PropertyInstance <> 0 Then
+			GlobalDeleteAtom(PropertyInstance)
+		End If
+	End Sub
+
 End Class
 
-Namespace Detail
-Class _System_ControlIinitializer
-Public
-	Sub _System_ControlIinitializer(hinst As HINSTANCE)
-		Control.Initialize(hinst)
-	End Sub
-
-	Sub ~_System_ControlIinitializer()
-		Control.Uninitialize()
-	End Sub
-End Class
-
-#ifndef _SYSTEM_NO_INITIALIZE_CONTROL_
-Dim _System_ControlInitializer As _System_ControlIinitializer(GetModuleHandle(0))
-#endif '_SYSTEM_NO_INITIALIZE_CONTROL_
-
-End Namespace 'Detail
-
-Class Form
+Class Form '仮
 	Inherits Control
 Protected
@@ -294,10 +422,9 @@
 		End With
 	End Sub
-Public '仮
+Public 
+
 	Override Function WndProc(msg As DWord, wp As WPARAM, lp As LPARAM) As LRESULT
 		WndProc = 0
 		Select Case msg
-			Case WM_DESTROY
-				PostQuitMessage(0)
 			Case Else
 				WndProc = Super.WndProc(msg, wp, lp)
@@ -311,4 +438,5 @@
 End Namespace 'ActiveBasic
 
+
 '----------
 'テスト実行用
@@ -316,17 +444,40 @@
 Imports ActiveBasic.Windows.UI.Forms
 
-Class Bar
-Public
-Static Sub PaintDCEvent(sender As Object, et As PaintDCEventArgs) 
-	Dim e = et As PaintDCEventArgs
-	TextOut(e.Handle, 10, 10, "Hello, world", 12)
-End Sub
+'OleInitialize()
+Control.Initialize(GetModuleHandle(0))
+
+Class MyForm
+	Inherits Form
+Public
+	Sub NcDestory(sender As Object, et As EventArgs)
+		PostQuitMessage(0)
+	End Sub
 End Class
 
-Dim f = New Form
+Dim f = New MyForm
 f.Create()
-Dim v = New PaintDCEventHandler(AddressOf (Bar.PaintDCEvent))
-f.AddPaintDC(v)
-f.Handle.Show(SW_SHOW)
-
-MessageBox(0, "hello", "", 0)
+Dim h = New MessageEventHandler(AddressOf (f.NcDestory))
+f.AddMessageEvent(WM_NCDESTROY, h)
+ShowWindow(f.Handle, SW_SHOW)
+
+Dim m As MSG
+Do
+	Dim ret = GetMessage(m, 0, 0, 0)
+	If ret = 0 Then
+		Exit Do
+	ElseIf ret = -1 Then
+		ExitProcess(-1)
+	End If
+
+	TranslateMessage(m)
+	DispatchMessage(m)
+Loop
+
+f = Nothing
+System.GC.Collect()
+
+Control.Uninitialize()
+'OleUninitialize()
+
+End
+
Index: trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/Forms/ControlEvent.sbp
===================================================================
--- trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/Forms/ControlEvent.sbp	(revision 540)
+++ trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/Forms/ControlEvent.sbp	(revision 542)
@@ -1,15 +1,11 @@
-Public
-	/*!
-	@brief PaintDCイベントハンドラを追加する
-	*/
+
+Public
 	Sub AddPaintDC(h As PaintDCEventHandler)
 		If IsNothing(paintDC) Then
-			paintDC = New PaintDCEventHandler
-		End If
-		paintDC += h
-	End Sub
-	/*!
-	@brief PaintDCイベントハンドラを削除する
-	*/
+			paintDC = h
+		Else
+			paintDC += h
+		End If
+	End Sub
 	Sub RemovePaintDC(h As PaintDCEventHandler)
 		If Not IsNothing(paintDC) Then
@@ -17,9 +13,6 @@
 		End If
 	End Sub
-Protected
-	/*!
-	@brief ウィンドウの描画が必要なときに呼び出されます。
-	*/
-	Virtual Sub OnPaintDC(e As PaintDCEventArgs)
+Private
+	Sub OnPaintDC(e As PaintDCEventArgs)
 		If Not IsNothing(paintDC) Then
 			paintDC(This, e)
@@ -30,26 +23,18 @@
 
 Public
-	/*!
-	@brief Clickイベントハンドラを追加する
-	*/
-	Sub AddClick(h As EventHandler)
+	Sub AddClick(h As System.EventHandler)
 		If IsNothing(click) Then
-			click = New EventHandler
-		End If
-		click += h
-	End Sub
-	/*!
-	@brief Clickイベントハンドラを削除する
-	*/
-	Sub RemoveClick(h As EventHandler)
+			click = h
+		Else
+			click += h
+		End If
+	End Sub
+	Sub RemoveClick(h As System.EventHandler)
 		If Not IsNothing(click) Then
 			click -= h
 		End If
 	End Sub
-Protected
-	/*!
-	@brief クリックされたときに呼び出されます。
-	*/
-	Virtual Sub OnClick(e As EventArgs)
+Private
+	Sub OnClick(e As System.EventArgs)
 		If Not IsNothing(click) Then
 			click(This, e)
@@ -57,29 +42,21 @@
 	End Sub
 Private
-	click As EventHandler
-
-Public
-	/*!
-	@brief DoubleClickイベントハンドラを追加する
-	*/
-	Sub AddDoubleClick(h As EventHandler)
+	click As System.EventHandler
+
+Public
+	Sub AddDoubleClick(h As System.EventHandler)
 		If IsNothing(doubleClick) Then
-			doubleClick = New EventHandler
-		End If
-		doubleClick += h
-	End Sub
-	/*!
-	@brief DoubleClickイベントハンドラを削除する
-	*/
-	Sub RemoveDoubleClick(h As EventHandler)
+			doubleClick = h
+		Else
+			doubleClick += h
+		End If
+	End Sub
+	Sub RemoveDoubleClick(h As System.EventHandler)
 		If Not IsNothing(doubleClick) Then
 			doubleClick -= h
 		End If
 	End Sub
-Protected
-	/*!
-	@brief ダブルクリックされたときに呼び出されます。
-	*/
-	Virtual Sub OnDoubleClick(e As EventArgs)
+Private
+	Sub OnDoubleClick(e As System.EventArgs)
 		If Not IsNothing(doubleClick) Then
 			doubleClick(This, e)
@@ -87,19 +64,102 @@
 	End Sub
 Private
-	doubleClick As EventHandler
-
-Public
-	/*!
-	@brief MouseDownイベントハンドラを追加する
-	*/
+	doubleClick As System.EventHandler
+
+Public
+	Sub AddMouseEnter(h As MouseEventHandler)
+		If IsNothing(mouseEnter) Then
+			mouseEnter = h
+		Else
+			mouseEnter += h
+		End If
+	End Sub
+	Sub RemoveMouseEnter(h As MouseEventHandler)
+		If Not IsNothing(mouseEnter) Then
+			mouseEnter -= h
+		End If
+	End Sub
+Private
+	Sub OnMouseEnter(e As MouseEventArgs)
+		If Not IsNothing(mouseEnter) Then
+			mouseEnter(This, e)
+		End If
+	End Sub
+Private
+	mouseEnter As MouseEventHandler
+
+Public
+	Sub AddMouseMove(h As MouseEventHandler)
+		If IsNothing(mouseMove) Then
+			mouseMove = h
+		Else
+			mouseMove += h
+		End If
+	End Sub
+	Sub RemoveMouseMove(h As MouseEventHandler)
+		If Not IsNothing(mouseMove) Then
+			mouseMove -= h
+		End If
+	End Sub
+Private
+	Sub OnMouseMove(e As MouseEventArgs)
+		If Not IsNothing(mouseMove) Then
+			mouseMove(This, e)
+		End If
+	End Sub
+Private
+	mouseMove As MouseEventHandler
+
+Public
+	Sub AddMouseHover(h As MouseEventHandler)
+		If IsNothing(mouseHover) Then
+			mouseHover = h
+		Else
+			mouseHover += h
+		End If
+	End Sub
+	Sub RemoveMouseHover(h As MouseEventHandler)
+		If Not IsNothing(mouseHover) Then
+			mouseHover -= h
+		End If
+	End Sub
+Private
+	Sub OnMouseHover(e As MouseEventArgs)
+		If Not IsNothing(mouseHover) Then
+			mouseHover(This, e)
+		End If
+	End Sub
+Private
+	mouseHover As MouseEventHandler
+
+Public
+	Sub AddMouseLeave(h As MouseEventHandler)
+		If IsNothing(mouseLeave) Then
+			mouseLeave = h
+		Else
+			mouseLeave += h
+		End If
+	End Sub
+	Sub RemoveMouseLeave(h As MouseEventHandler)
+		If Not IsNothing(mouseLeave) Then
+			mouseLeave -= h
+		End If
+	End Sub
+Private
+	Sub OnMouseLeave(e As MouseEventArgs)
+		If Not IsNothing(mouseLeave) Then
+			mouseLeave(This, e)
+		End If
+	End Sub
+Private
+	mouseLeave As MouseEventHandler
+
+Public
 	Sub AddMouseDown(h As MouseEventHandler)
 		If IsNothing(mouseDown) Then
-			mouseDown = New MouseEventHandler
-		End If
-		mouseDown += h
-	End Sub
-	/*!
-	@brief MouseDownイベントハンドラを削除する
-	*/
+			mouseDown = h
+		Else
+			mouseDown += h
+		End If
+	End Sub
 	Sub RemoveMouseDown(h As MouseEventHandler)
 		If Not IsNothing(mouseDown) Then
@@ -107,9 +167,6 @@
 		End If
 	End Sub
-Protected
-	/*!
-	@brief マウスボタンが押されたときに呼び出されます。
-	*/
-	Virtual Sub OnMouseDown(e As MouseEventArgs)
+Private
+	Sub OnMouseDown(e As MouseEventArgs)
 		If Not IsNothing(mouseDown) Then
 			mouseDown(This, e)
@@ -120,16 +177,55 @@
 
 Public
-	/*!
-	@brief MouseUpイベントハンドラを追加する
-	*/
+	Sub AddMouseClick(h As MouseEventHandler)
+		If IsNothing(mouseClick) Then
+			mouseClick = h
+		Else
+			mouseClick += h
+		End If
+	End Sub
+	Sub RemoveMouseClick(h As MouseEventHandler)
+		If Not IsNothing(mouseClick) Then
+			mouseClick -= h
+		End If
+	End Sub
+Private
+	Sub OnMouseClick(e As MouseEventArgs)
+		If Not IsNothing(mouseClick) Then
+			mouseClick(This, e)
+		End If
+	End Sub
+Private
+	mouseClick As MouseEventHandler
+
+Public
+	Sub AddMouseDoubleClick(h As MouseEventHandler)
+		If IsNothing(mouseDoubleClick) Then
+			mouseDoubleClick = h
+		Else
+			mouseDoubleClick += h
+		End If
+	End Sub
+	Sub RemoveMouseDoubleClick(h As MouseEventHandler)
+		If Not IsNothing(mouseDoubleClick) Then
+			mouseDoubleClick -= h
+		End If
+	End Sub
+Private
+	Sub OnMouseDoubleClick(e As MouseEventArgs)
+		If Not IsNothing(mouseDoubleClick) Then
+			mouseDoubleClick(This, e)
+		End If
+	End Sub
+Private
+	mouseDoubleClick As MouseEventHandler
+
+Public
 	Sub AddMouseUp(h As MouseEventHandler)
 		If IsNothing(mouseUp) Then
-			mouseUp = New MouseEventHandler
-		End If
-		mouseUp += h
-	End Sub
-	/*!
-	@brief MouseUpイベントハンドラを削除する
-	*/
+			mouseUp = h
+		Else
+			mouseUp += h
+		End If
+	End Sub
 	Sub RemoveMouseUp(h As MouseEventHandler)
 		If Not IsNothing(mouseUp) Then
@@ -137,9 +233,6 @@
 		End If
 	End Sub
-Protected
-	/*!
-	@brief マウスボタンが離されたときに呼び出されます。
-	*/
-	Virtual Sub OnMouseUp(e As MouseEventArgs)
+Private
+	Sub OnMouseUp(e As MouseEventArgs)
 		If Not IsNothing(mouseUp) Then
 			mouseUp(This, e)
@@ -149,2 +242,90 @@
 	mouseUp As MouseEventHandler
 
+Public
+	Sub AddKeyDown(h As KeyEventHandler)
+		If IsNothing(keyDown) Then
+			keyDown = h
+		Else
+			keyDown += h
+		End If
+	End Sub
+	Sub RemoveKeyDown(h As KeyEventHandler)
+		If Not IsNothing(keyDown) Then
+			keyDown -= h
+		End If
+	End Sub
+Private
+	Sub OnKeyDown(e As KeyEventArgs)
+		If Not IsNothing(keyDown) Then
+			keyDown(This, e)
+		End If
+	End Sub
+Private
+	keyDown As KeyEventHandler
+
+Public
+	Sub AddKeyUp(h As KeyEventHandler)
+		If IsNothing(keyUp) Then
+			keyUp = h
+		Else
+			keyUp += h
+		End If
+	End Sub
+	Sub RemoveKeyUp(h As KeyEventHandler)
+		If Not IsNothing(keyUp) Then
+			keyUp -= h
+		End If
+	End Sub
+Private
+	Sub OnKeyUp(e As KeyEventArgs)
+		If Not IsNothing(keyUp) Then
+			keyUp(This, e)
+		End If
+	End Sub
+Private
+	keyUp As KeyEventHandler
+/*
+Public
+	Sub AddKeyPress(h As KeyPressEventHandler)
+		If IsNothing(keyPress) Then
+			keyPress = h
+		Else
+			keyPress += h
+		End If
+	End Sub
+	Sub RemoveKeyPress(h As KeyPressEventHandler)
+		If Not IsNothing(keyPress) Then
+			keyPress -= h
+		End If
+	End Sub
+Private
+	Sub OnKeyPress(e As KeyPressEventArgs)
+		If Not IsNothing(keyPress) Then
+			keyPress(This, e)
+		End If
+	End Sub
+Private
+	keyPress As KeyPressEventHandler
+
+Public
+	Sub AddCreate(h As CreateEventHandler)
+		If IsNothing(create) Then
+			create = h
+		Else
+			create += h
+		End If
+	End Sub
+	Sub RemoveCreate(h As CreateEventHandler)
+		If Not IsNothing(create) Then
+			create -= h
+		End If
+	End Sub
+Private
+	Sub OnCreate(e As CreateEventArgs)
+		If Not IsNothing(create) Then
+			create(This, e)
+		End If
+	End Sub
+Private
+	create As CreateEventHandler
+*/
Index: trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/Forms/ControlEventList.txt
===================================================================
--- trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/Forms/ControlEventList.txt	(revision 540)
+++ trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/Forms/ControlEventList.txt	(revision 542)
@@ -6,16 +6,18 @@
 'Resize	Event	ウィンドウの大きさが変化したときに呼び出されます。
 'VisibleChanged	Event	ウィンドウの表示状態が変化したときに呼び出されます。
-'GotFocus	Event	フォーカスを得たときに呼び出されます。
-'LostFocus	Event	フォーカスを失ったときに呼び出されます。
-'MouseEnter	MouseEvent	マウスカーソルがコントロールに入ってくると呼び出されます。
-'MouseMove	MouseEvent	マウスカーソルがコントロール上で移動すると呼び出されます
-'MouseHover	MouseEvent	マウスカーソルがコントロール上で静止すると呼び出されます。
-'MouseLeave	MouseEvent	マウスカーソルがコントロールから出て行くと呼び出されます。
+'SetFocus	Event	フォーカスを得たときに呼び出されます。
+'KillFocus	Event	フォーカスを失ったときに呼び出されます。
+MouseEnter	MouseEvent	マウスカーソルがコントロールに入ってくると呼び出されます。
+MouseMove	MouseEvent	マウスカーソルがコントロール上で移動すると呼び出されます
+MouseHover	MouseEvent	マウスカーソルがコントロール上で静止すると呼び出されます。
+MouseLeave	MouseEvent	マウスカーソルがコントロールから出て行くと呼び出されます。
 MouseDown	MouseEvent	マウスボタンが押されたときに呼び出されます。
-'MouseClick	MouseEvent	マウスでクリックされたときに呼び出されます。
-'MouseDoubleClick	MouseEvent	マウスでダブルクリックされたときに呼び出されます。
+MouseClick	MouseEvent	マウスでクリックされたときに呼び出されます。
+MouseDoubleClick	MouseEvent	マウスでダブルクリックされたときに呼び出されます。
 MouseUp	MouseEvent	マウスボタンが離されたときに呼び出されます。
 'MouseWheel	MouseEvent	マウスホイールが回されたときに呼び出されます。
-'KeyDown	KeyEvent	キーが押されたときに呼ばれます。
-'KeyUp	KeyEvent	キーが離されたときに呼ばれます。
-'KeyPress	KeyPressEvent	キーが押されて文字が打たれたときに呼ばれます。
+KeyDown	KeyEvent	キーが押されたときに呼ばれます。
+KeyUp	KeyEvent	キーが離されたときに呼ばれます。
+'なぜかコンパイルエラーを起こすのでコメントアウト KeyPress	KeyPressEvent	キーが押されて文字が打たれたときに呼ばれます。
+Create	CreateEvent	ウィンドウが作成されたときに呼ばれます。
+'Destroy	Event	ウィンドウが破棄されるときに呼ばれます。
Index: trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/Forms/EventArgs.ab
===================================================================
--- trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/Forms/EventArgs.ab	(revision 540)
+++ trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/Forms/EventArgs.ab	(revision 542)
@@ -11,4 +11,48 @@
 TypeDef EventArgs = System.EventArgs
 TypeDef EventHandler = System.EventHandler
+
+Class MessageEventArgs
+	Inherits EventArgs
+Public
+	Sub MessageEventArgs(hwndSrc As HWND, message As DWord, wParam As WPARAM, lParam As LPARAM)
+		msg = message
+'		hwnd = hwndSrc
+		wp = wParam
+		lp = lParam
+		lr = 0
+	End Sub
+
+	Function Msg() As DWord
+		Msg = msg
+	End Function
+
+'	Function HWnd() As HWND
+'		HWnd = hwnd
+'	End Function
+
+	Function WParam() As WPARAM
+		WParam = wp
+	End Function
+
+	Function LParam() As LPARAM
+		LParam = lp
+	End Function
+
+	Function LResult() As LRESULT
+		LResult = lr
+	End Function
+
+	Sub LResult(lResult As LRESULT)
+		lr = lResult
+	End Sub
+Private
+	msg As DWord
+'	hwnd As HWND
+	wp As WPARAM
+	lp As LPARAM
+	lr As LRESULT
+End Class
+
+Delegate Sub MessageEventHandler(sender As Object, e As MessageEventArgs)
 
 Class PaintDCEventArgs
@@ -63,4 +107,7 @@
 	XButton1 = MK_XBUTTON1
 	XButton2 = MK_XBUTTON2
+
+	Shift = MK_SHIFT
+	Control = MK_CONTROL
 End Enum
 
@@ -352,5 +399,7 @@
 
 	Function KeyCode() As Keys
-		KeyCode = key And Keys.KeyCode
+		Dim k = key As DWord
+		Dim mask = Keys.KeyCode As DWord
+		KeyCode = (k And mask) As Keys
 	End Function
 
@@ -360,5 +409,7 @@
 
 	Function Modifiers() As Keys
-		Modifiers = key And Keys.Modifiers
+		Dim k = key As DWord
+		Dim mask = Keys.Modifiers As DWord
+		Modifiers = (k And mask) As Keys
 	End Function
 
@@ -381,4 +432,62 @@
 
 Delegate Sub KeyEventHandler(sender As Object, e As KeyEventArgs)
+
+Class CreateEventArgs
+	Inherits EventArgs
+Public
+	Sub CreateEventArgs(pCreateStruct As *CREATESTRUCT)
+		pcs = pCreateStruct
+	End Sub
+
+	Const Function HInstance() As HINSTANCE
+		HInstance = pcs->hInstance
+	End Function
+
+	'Menu: pcs->hMenu
+
+	Const Function Parent() As Control
+		'Parent = Control.FromHandle(pcs->hwndParent)
+	End Function
+
+	Const Function Height() As Long
+		Height = pcs->cy
+	End Function
+
+	Const Function Width() As Long
+		Width = pcs->cx
+	End Function
+
+	Const Function Y() As Long
+		Y = pcs->cy
+	End Function
+
+	Const Function X() As Long
+		X = pcs->cx
+	End Function
+
+	Const Function Style() As DWord
+		Style = pcs->style As DWord
+	End Function
+
+	Const Function Caption() As String
+		Caption = New String(pcs->lpszName)
+	End Function
+
+	Const Function ClassName() As String
+		ClassName = New String(pcs->lpszClass)
+	End Function
+
+	Const Function ExStyle() As DWord
+		ExStyle = pcs->dwExStyle
+	End Function
+
+	Const Function CreateStruct() As *CREATESTRUCT
+		CreateStruct = pcs
+	End Function
+Private
+	pcs As *CREATESTRUCT
+End Class
+
+Delegate Sub CreateEventHandler(sender As Object, e As CreateEventArgs)
 
 End Namespace 'Forms
Index: trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/Forms/MakeControlEventHandler.ab
===================================================================
--- trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/Forms/MakeControlEventHandler.ab	(revision 540)
+++ trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/Forms/MakeControlEventHandler.ab	(revision 542)
@@ -22,16 +22,17 @@
 	Dim argsType = argBase & "Args"
 	out.WriteLine("Public")
-	out.WriteLine(Ex"\t/*!")
-	out.WriteLine(Ex"\t@brief " & eventName & "イベントハンドラを追加する")
-	out.WriteLine(Ex"\t*/")
+'	out.WriteLine(Ex"\t/*!")
+'	out.WriteLine(Ex"\t@brief " & eventName & "イベントハンドラを追加する")
+'	out.WriteLine(Ex"\t*/")
 	out.WriteLine(Ex"\tSub Add" & eventName & "(h As " & handlerType & ")")
 	out.WriteLine(Ex"\t\tIf IsNothing(" & eventMember & ") Then")
-	out.WriteLine(Ex"\t\t\t" & eventMember & " = New " & handlerType)
+	out.WriteLine(Ex"\t\t\t" & eventMember & " = h")
+	out.WriteLine(Ex"\t\tElse")
+	out.WriteLine(Ex"\t\t\t" & eventMember & " += h")
 	out.WriteLine(Ex"\t\tEnd If")
-	out.WriteLine(Ex"\t\t" & eventMember & " += h")
 	out.WriteLine(Ex"\tEnd Sub")
-	out.WriteLine(Ex"\t/*!")
-	out.WriteLine(Ex"\t@brief " & eventName & "イベントハンドラを削除する")
-	out.WriteLine(Ex"\t*/")
+'	out.WriteLine(Ex"\t/*!")
+'	out.WriteLine(Ex"\t@brief " & eventName & "イベントハンドラを削除する")
+'	out.WriteLine(Ex"\t*/")
 	out.WriteLine(Ex"\tSub Remove" & eventName & "(h As " & handlerType & ")")
 	out.WriteLine(Ex"\t\tIf Not IsNothing(" & eventMember & ") Then")
@@ -39,9 +40,9 @@
 	out.WriteLine(Ex"\t\tEnd If")
 	out.WriteLine(Ex"\tEnd Sub")
-	out.WriteLine("Protected")
-	out.WriteLine(Ex"\t/*!")
-	out.WriteLine(Ex"\t@brief " & comment)
-	out.WriteLine(Ex"\t*/")
-	out.WriteLine(Ex"\tVirtual Sub On" & eventName & "(e As " & argsType & ")")
+	out.WriteLine("Private")
+'	out.WriteLine(Ex"\t/*!")
+'	out.WriteLine(Ex"\t@brief " & comment)
+'	out.WriteLine(Ex"\t*/")
+	out.WriteLine(Ex"\tSub On" & eventName & "(e As " & argsType & ")")
 	out.WriteLine(Ex"\t\tIf Not IsNothing(" & eventMember & ") Then")
 	out.WriteLine(Ex"\t\t\t" & eventMember & "(This, e)")
@@ -53,5 +54,5 @@
 End Sub
 
-'OutputEventHandlerCode("PaintDC", "PaintDCEventHandler", 
+'OutputEventHandlerCode("PaintDC", "PaintDCEventHandler",
 '	"ウィンドウの描画が必要なときに呼び出されます。")
 
Index: trunk/ab5.0/ablib/src/Classes/System/Collections/Generic/Dictionary.ab
===================================================================
--- trunk/ab5.0/ablib/src/Classes/System/Collections/Generic/Dictionary.ab	(revision 540)
+++ trunk/ab5.0/ablib/src/Classes/System/Collections/Generic/Dictionary.ab	(revision 542)
@@ -47,5 +47,5 @@
 		Dim a = al[hash] As ArrayList
 
-		If Not Object.ReferenceEquals(a, Nothing) Then
+		If Not ActiveBasic.IsNothing(a) Then
 			Dim i As Long
 			For i = 0 To ELM(a.Count)
@@ -77,4 +77,5 @@
 				If pair.Key.Equals(key) Then
 					pair.Value = value
+					Exit Sub
 				End If
 			Next
@@ -135,6 +136,34 @@
 	End Function
 
-'	Function ContainsKey(key As Key) As Boolean
-'	End Function
+	/*!
+	@biref	指定されたキーが格納されているか調べる。
+	@date	2008/07/11
+	@param[in] key	検索対象のキー
+	@retval True	格納されていたとき
+	@retval False	格納されていなかったとき
+	@throw ArgumentNullException	keyがNothingだったとき
+	@author Egtra
+	*/
+	Function ContainsKey(key As Key) As Boolean
+		If ActiveBasic.IsNothing(key) Then
+			Throw New ArgumentNullException("key")
+		End If
+		ContainsKey = False
+
+		Dim hash = getHash(key)
+		Dim a = al[hash] As ArrayList
+
+		If Not ActiveBasic.IsNothing(a) Then
+			Dim i As Long
+			For i = 0 To ELM(a.Count)
+				Dim pair = a[i] As Detail.Pair
+				If pair.Key.Equals(key) Then
+					ContainsKey = True
+					Exit Function
+				End If
+			Next
+		End If
+	End Function
+
 '	Function ContainsValue(value As T) As Boolean
 '	End Function
@@ -169,6 +198,26 @@
 	End Function
 
-'	Function TryGetValue(key As Key, ByRef value As T) As Boolean
-'	End Function
+	Function TryGetValue(key As Key, ByRef value As T) As Boolean
+		If ActiveBasic.IsNothing(key) Then
+			Throw New ArgumentNullException("key")
+		End If
+		TryGetValue = False
+		value = Nothing
+
+		Dim hash = getHash(key)
+		Dim a = al[hash] As ArrayList
+
+		If Not ActiveBasic.IsNothing(a) Then
+			Dim i As Long
+			For i = 0 To ELM(a.Count)
+				Dim pair = a[i] As Detail.Pair
+				If pair.Key.Equals(key) Then
+					TryGetValue = True
+					value = pair.Value
+					Exit Function
+				End If
+			Next
+		End If
+	End Function
 
 	'Classses
Index: trunk/ab5.0/ablib/src/Classes/System/Delegate.ab
===================================================================
--- trunk/ab5.0/ablib/src/Classes/System/Delegate.ab	(revision 540)
+++ trunk/ab5.0/ablib/src/Classes/System/Delegate.ab	(revision 542)
@@ -29,24 +29,28 @@
 
 	Sub _Add( dg As DelegateBase )
-		Dim i As Long
-		For i=0 To ELM(dg.simpleDelegates.Count)
-			simpleDelegates.Add( dg.simpleDelegates[i] )
-		Next
+		If Not ActiveBasic.IsNothing(dg) Then
+			Dim i As Long
+			For i=0 To ELM(dg.simpleDelegates.Count)
+				simpleDelegates.Add( dg.simpleDelegates[i] )
+			Next
+		End If
 	End Sub
 
 	Sub _Delete( dg As DelegateBase )
-		Dim i As Long
-		For i=0 To ELM(This.simpleDelegates.Count)
-			Dim i2 As Long
-			Dim isExist = False
-			For i2=0 To ELM(dg.simpleDelegates.Count)
-				If This.simpleDelegates[i].IsEqual( dg.simpleDelegates[i2] ) Then
-					isExist = True
+		If Not ActiveBasic.IsNothing(dg) Then
+			Dim i As Long
+			For i=0 To ELM(This.simpleDelegates.Count)
+				Dim i2 As Long
+				Dim isExist = False
+				For i2=0 To ELM(dg.simpleDelegates.Count)
+					If This.simpleDelegates[i].IsEqual( dg.simpleDelegates[i2] ) Then
+						isExist = True
+					End If
+				Next
+				If isExist Then
+					This.simpleDelegates.RemoveAt( i )
 				End If
 			Next
-			If isExist Then
-				This.simpleDelegates.RemoveAt( i )
-			End If
-		Next
+		End If
 	End Sub
 
