Index: trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/Application.ab
===================================================================
--- trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/Application.ab	(revision 551)
+++ trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/Application.ab	(revision 559)
@@ -20,6 +20,7 @@
 	*/
 	Static Sub Run(form As Form)
-		f.Show(SW_SHOW)
 		If IsNothing(form) = False Then
+			form.Show(SW_SHOW)
+			form.Update()
 			form.AddMessageEvent(WM_DESTROY, AddressOf(Application.OnMainFormClosed))
 		End If
@@ -34,5 +35,5 @@
 			DispatchMessage(m)
 		Loop
-'		OnThraedExit(EventArgs.Empty)
+
 	End Sub
 
@@ -55,12 +56,31 @@
 	End Sub
 
-'#include "ApplicationEvent.sbp"
+	/*!
+	@brief メッセージキューに溜まったメッセージを処理する。
+	@date 2008/07/18
+	@author Egtra
+	ここのコードから改変。
+	http://www.activebasic.com/forum/viewtopic.php?t=426
+	*/
+	Static Sub DoEvents()
+		Dim msg As MSG
+		While PeekMessage(msg, 0, 0, 0, PM_REMOVE) <> FALSE
+			Select Case msg.message
+				Case WM_QUIT
+					PostQuitMessage(0) 'Run()で捕まえてくれるようPostしなおす。
+				Case Else
+					TranslateMessage(msg)
+					DispatchMessage(msg)
+			End Select
+		Wend
+	End Sub
+
+#include "ApplicationEvent.sbp"
 
 Private
 	Static Sub OnMainFormClosed(sender As Object, e As Args)
+		OnThreadExit(Args.Empty)
 		ExitThread()
 	End Sub
-
-	main As Form
 End Class
 
Index: trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/ApplicationEvent.sbp
===================================================================
--- trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/ApplicationEvent.sbp	(revision 551)
+++ trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/ApplicationEvent.sbp	(revision 559)
@@ -1,4 +1,4 @@
 Public
-	Sub AddThreadExit(h As Handler)
+	Static Sub AddThreadExit(h As Handler)
 		If IsNothing(threadExit) Then
 			threadExit = h
@@ -7,5 +7,5 @@
 		End If
 	End Sub
-	Sub RemoveThreadExit(h As Handler)
+	Static Sub RemoveThreadExit(h As Handler)
 		If Not IsNothing(threadExit) Then
 			threadExit -= h
@@ -13,5 +13,5 @@
 	End Sub
 Protected
-	Sub OnThreadExit(e As Args)
+	Static Sub OnThreadExit(e As Args)
 		If Not IsNothing(threadExit) Then
 			threadExit(This, e)
@@ -19,4 +19,4 @@
 	End Sub
 Private
-	threadExit As Handler
+	Static threadExit As Handler
 
Index: trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/Control.ab
===================================================================
--- trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/Control.ab	(revision 551)
+++ trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/Control.ab	(revision 559)
@@ -91,4 +91,8 @@
 		Imports System.Runtime.InteropServices
 
+		If hwnd <> 0 Then
+			Throw New System.InvalidOperationException("Window already created.")
+		End If
+
 		Dim gch = GCHandle.Alloc(This)
 		TlsSetValue(tlsIndex, GCHandle.ToIntPtr(gch) As VoidPtr)
@@ -101,4 +105,5 @@
 				.x, .y, .cx, .cy, .hwndParent, .hMenu, .hInstance, .lpCreateParams)
 			If hwnd = 0 Then
+				Debug
 				ActiveBasic.Windows.ThrowByWindowsError(GetLastError())
 			End If
@@ -251,5 +256,5 @@
 
 	Sub OnCreateBase(sender As Object, e As MessageArgs)
-'		OnCreate(New CreateArgs(e.LParam As *CREATESTRUCT))
+		OnCreate(New CreateArgs(e.LParam As *CREATESTRUCT))
 	End Sub
 
@@ -333,7 +338,5 @@
 			' ウィンドウが作られて最初にWndProcFirstが呼ばれたとき
 
-			If AssociateHWnd(gch, hwnd) = False Then
-				Goto *InstanceIsNotFound
-			End If
+			AssociateHWnd(gch, hwnd)
 		End If
 		If msg = WM_NCDESTROY Then
@@ -349,5 +352,7 @@
 
 	*InstanceIsNotFound
-		OutputDebugString(Ex"ActiveBasic.Windows.UI.Control.WndProcFirst: The attached instance is not found.\r\n")
+		Dim err = "ActiveBasic.Windows.UI.Control.WndProcFirst: The attached instance is not found. msg = &h" _
+			+ Hex$(msg) + Ex"\r\n"
+		OutputDebugString(ToTCStr(err))
 		WndProcFirst = DefWindowProc(hwnd, msg, wp, lp)
 	End Function
@@ -360,13 +365,13 @@
 	これを実行することで、UnassociateHWndされるまでControlがGC対象にならなくなる。
 	*/
-	Static Function AssociateHWnd(gch As System.Runtime.InteropServices.GCHandle, hwnd As HWND) As Boolean
+	Static Sub AssociateHWnd(gch As System.Runtime.InteropServices.GCHandle, hwnd As HWND)
 		Imports System.Runtime.InteropServices
 		Dim rThis = gch.Target As Control
 		If IsNothing(rThis) Then
-			Exit Function
+			Exit Sub
 		End If
 		rThis.hwnd = hwnd
 		rThis.Prop[PropertyInstance] = GCHandle.ToIntPtr(gch) As HANDLE
-	End Function
+	End Sub
 
 	/*!
Index: trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/ControlEvent.sbp
===================================================================
--- trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/ControlEvent.sbp	(revision 551)
+++ trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/ControlEvent.sbp	(revision 559)
@@ -328,5 +328,5 @@
 Private
 	keyPress As KeyPressHandler
-/*
+
 Public
 	Sub AddCreate(h As CreateHandler)
@@ -372,3 +372,3 @@
 Private
 	destroy As Handler
-*/
+
Index: trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/Form.ab
===================================================================
--- trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/Form.ab	(revision 551)
+++ trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/Form.ab	(revision 559)
@@ -45,60 +45,2 @@
 End Namespace 'Widnows
 End Namespace 'ActiveBasic
-
-'----------
-'テスト実行用
-
-#require <Classes/ActiveBasic/Windows/UI/Application.ab>
-#require <Classes/ActiveBasic/Windows/UI/Button.ab>
-
-Imports ActiveBasic.Windows.UI
-
-'OleInitialize()
-Control.Initialize(GetModuleHandle(0))
-
-Sub Paint(sender As Object, e As PaintDCArgs)
-	TextOut(e.Handle, 10, 10, "Hello world!", 12)
-End Sub
-
-Class MyForm
-	Inherits Form
-Public
-	Sub MyForm()
-		AddPaintDC(AddressOf (Paint))
-		AddMouseClick(AddressOf (Mouse))
-		s = "aaa"
-	End Sub
-
-'	Sub Paint(sender As Object, e As PaintDCArgs)
-'		TextOut(e.Handle, 10, 10, ToTCStr(s), s.Length)
-'	End Sub
-
-	Sub Mouse(sender As Object, e As MouseArgs)
-		Invalidate()
-	End Sub
-
-	Sub OnClick(sender As Object, e As Args)
-		OutputDebugString(Ex"====OnClick====\r\n")
-	End Sub
-
-	s As String
-End Class
-
-Dim f = New MyForm
-f.Create()
-f.Text = "Hello"
-
-Dim b = New Button
-b.Create(f)
-b.Move(50, 50, 100, 100)
-b.Text = "Ok"
-b.AddClick(AddressOf(f.OnClick))
-
-Application.Run(f)
-f = Nothing
-System.GC.Collect()
-
-Control.Uninitialize()
-'OleUninitialize()
-
-End
Index: trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/MakeControlEventHandler.ab
===================================================================
--- trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/MakeControlEventHandler.ab	(revision 551)
+++ trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/UI/MakeControlEventHandler.ab	(revision 559)
@@ -17,13 +17,15 @@
 End Function
 
-Sub OutputEventHandlerCode(out As TextWriter, eventName As String, argBase As String, comment As String)
+Sub OutputEventHandlerCode(out As TextWriter, eventName As String, argBase As String, comment As String, isStatic As Boolean)
 	Dim eventMember = EventNameToMemberVariableName(eventName)
 	Dim handlerType = argBase & "Handler"
 	Dim argsType = argBase & "Args"
+	Dim staticKeyword = Nothing As String
+	If isStatic Then staticKeyword = "Static "
 	out.WriteLine("Public")
 '	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" & staticKeyword & "Sub Add" & eventName & "(h As " & handlerType & ")")
 	out.WriteLine(Ex"\t\tIf IsNothing(" & eventMember & ") Then")
 	out.WriteLine(Ex"\t\t\t" & eventMember & " = h")
@@ -35,5 +37,5 @@
 '	out.WriteLine(Ex"\t@brief " & eventName & "イベントハンドラを削除する")
 '	out.WriteLine(Ex"\t*/")
-	out.WriteLine(Ex"\tSub Remove" & eventName & "(h As " & handlerType & ")")
+	out.WriteLine(Ex"\t" & staticKeyword & "Sub Remove" & eventName & "(h As " & handlerType & ")")
 	out.WriteLine(Ex"\t\tIf Not IsNothing(" & eventMember & ") Then")
 	out.WriteLine(Ex"\t\t\t" & eventMember & " -= h")
@@ -44,5 +46,5 @@
 '	out.WriteLine(Ex"\t@brief " & comment)
 '	out.WriteLine(Ex"\t*/")
-	out.WriteLine(Ex"\tSub On" & eventName & "(e As " & argsType & ")")
+	out.WriteLine(Ex"\t" & staticKeyword & "Sub On" & eventName & "(e As " & argsType & ")")
 	out.WriteLine(Ex"\t\tIf Not IsNothing(" & eventMember & ") Then")
 	out.WriteLine(Ex"\t\t\t" & eventMember & "(This, e)")
@@ -50,5 +52,5 @@
 	out.WriteLine(Ex"\tEnd Sub")
 	out.WriteLine("Private")
-	out.WriteLine(Ex"\t" & eventMember & " As " & handlerType)
+	out.WriteLine(Ex"\t" & staticKeyword & eventMember & " As " & handlerType)
 	out.WriteLine()
 End Sub
@@ -57,5 +59,5 @@
 '	"ウィンドウの描画が必要なときに呼び出されます。")
 
-Sub MakeControlEvent(t As String)
+Sub MakeControlEvent(t As String, isStatic = False As Boolean)
 	Dim event As String, handler As String, comment As String
 	Dim n As DWord, i As DWord
@@ -70,5 +72,5 @@
 				Dim a = ActiveBasic.Strings.Detail.Split(s, 9) 'Tab
 				If a.Count >= 3 Then
-					OutputEventHandlerCode(out, a[0], a[1], a[2])
+					OutputEventHandlerCode(out, a[0], a[1], a[2], isStatic)
 				End If
 			Loop
@@ -79,4 +81,4 @@
 MakeControlEvent("Control")
 MakeControlEvent("Form")
-MakeControlEvent("Application")
+MakeControlEvent("Application", True)
 End
