[Mono-winforms-list] Final changes re Window registration procedure

Vlad Kaluzhny vkaluzhny@openlinksw.co.uk
Mon, 24 Mar 2003 15:58:47 +0600


This is a multi-part message in MIME format.

------=_NextPart_000_026E_01C2F21E.40E3B240
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hi.
After analizing of the guys wishes, I have modified the Window Class
registration.
The only suggestion - use the class names, which are described in Win32
class
(I put them in win32functions.cs file). Or add there your class name, if you
need it.
This will avoid the class names duplications with  small and/or  capital
chars mixture.

And Second,  be attentive when specify the CreateParams.ClassStyle value.
It is simply duplication of class being registered, when you have the same
style for
different classes. Another to say - when assign new class name, take care,
that
it have the style, which  is not specified for another  window class.

Regards, Vlad

------=_NextPart_000_026E_01C2F21E.40E3B240
Content-Type: application/octet-stream;
	name="res2.diff"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="res2.diff"

Index: Button.cs=0A=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A=
RCS file: =
/mono/mcs/class/System.Windows.Forms/System.Windows.Forms/Button.cs,v=0A=
retrieving revision 1.15=0A=
diff -u -b -B -r1.15 Button.cs=0A=
--- Button.cs	10 Mar 2003 21:06:34 -0000	1.15=0A=
+++ Button.cs	24 Mar 2003 09:21:24 -0000=0A=
@@ -38,11 +38,8 @@=0A=
 			get {
 				// This is a child control, so it must have a parent for creation
 				if( Parent !=3D null) {
-					CreateParams createParams =3D new CreateParams ();
-					// CHECKME: here we must not overwrite window
-					if( window =3D=3D null) {
-						window =3D new ControlNativeWindow (this);
-					}
+					/* there was  variable name duplication - we have it as member of =
parent class. vkaluzhny@openlinksw.co.uk */
+					createParams =3D new CreateParams ();
=20
 					createParams.Caption =3D Text;
 					createParams.ClassName =3D "BUTTON";
@@ -58,10 +55,11 @@=0A=
 						WindowStyles.WS_CHILD |=20
 						WindowStyles.WS_VISIBLE |
 						WindowStyles.WS_CLIPSIBLINGS );
-					if(FlatStyle !=3D FlatStyle.System) {
-						createParams.Style |=3D (int) ButtonStyles.BS_OWNERDRAW;
-					}
-					createParams.Style |=3D =
(int)Win32.ContentAlignment2SystemButtonStyle(TextAlign);
+					//if(FlatStyle !=3D FlatStyle.System) {
+					//	createParams.Style |=3D (int) ButtonStyles.BS_OWNERDRAW;
+					//}
+					//createParams.Style |=3D =
(int)Win32.ContentAlignment2SystemButtonStyle(TextAlign);
+
 					// CHECKME : this call is commented because (IMHO) =
Control.CreateHandle suppose to do this
 					// and this function is CreateParams, not CreateHandle
 					// window.CreateHandle (createParams);
@@ -278,8 +276,7 @@=0A=
 					DRAWITEMSTRUCT dis =3D new DRAWITEMSTRUCT();
 					dis =3D (DRAWITEMSTRUCT)Marshal.PtrToStructure(m.LParam, =
dis.GetType());
 					Rectangle	rect =3D new Rectangle(dis.rcItem.left, dis.rcItem.top, =
dis.rcItem.right - dis.rcItem.left, dis.rcItem.bottom - dis.rcItem.top);
-					DrawItemEventArgs args =3D new =
DrawItemEventArgs(Graphics.FromHdc(dis.hDC), Font,
-						rect, dis.itemID, (DrawItemState)dis.itemState);
+					DrawItemEventArgs args =3D new =
DrawItemEventArgs(Graphics.FromHdc(dis.hDC), Font,=0D	rect, dis.itemID, =
(DrawItemState)dis.itemState);=0D
 					OnDrawItem( args);
 					//Marshal.StructureToPtr(dis, m.LParam, false);
 					m.Result =3D (IntPtr)1;
Index: CheckedListBox.cs=0A=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A=
RCS file: =
/mono/mcs/class/System.Windows.Forms/System.Windows.Forms/CheckedListBox.=
cs,v=0A=
retrieving revision 1.15=0A=
diff -u -b -B -r1.15 CheckedListBox.cs=0A=
Index: ComboBox.cs=0A=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A=
RCS file: =
/mono/mcs/class/System.Windows.Forms/System.Windows.Forms/ComboBox.cs,v=0A=
retrieving revision 1.21=0A=
diff -u -b -B -r1.21 ComboBox.cs=0A=
Index: Control.cs=0A=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A=
RCS file: =
/mono/mcs/class/System.Windows.Forms/System.Windows.Forms/Control.cs,v=0A=
retrieving revision 1.29=0A=
diff -u -b -B -r1.29 Control.cs=0A=
--- Control.cs	15 Mar 2003 19:31:43 -0000	1.29=0A=
+++ Control.cs	24 Mar 2003 09:21:34 -0000=0A=
@@ -527,7 +527,7 @@=0A=
     			get {
   					CreateParams createParams =3D new CreateParams ();
   					createParams.Caption =3D Text;
-  					createParams.ClassName =3D "CONTROL";
+  					createParams.ClassName =3D Win32.FrameClass;=0A=
   					createParams.X =3D Left;
   					createParams.Y =3D Top;
   					createParams.Width =3D Width;
@@ -1188,10 +1189,10 @@=0A=
 							controlsCollection.Add(Handle, this);
 						}
 						SubclassWindow();
-
-						CreatorThreadId_ =3D Win32.GetCurrentThreadId();
-
+						/* vkaluzhny@openlinksw.co.uk =3D FIXME!!!!=0A=
+						CreatorThreadId_ =3D Win32.GetCurrentThreadId(); */=0A=
 						OnHandleCreated (new EventArgs());
+=0A=
 					}
 				}
     		}
@@ -1548,8 +1549,8 @@=0A=
 				//if( font !=3D null) {
 				//	Win32.SendMessage( Handle, Msg.WM_SETFONT, =
font.ToHfont().ToInt32(), 0);
 				//}
-				Win32.SendMessage( Handle, Msg.WM_SETFONT, =
Font.ToHfont().ToInt32(), 0);
-				Win32.SetWindowText( Handle, text);
+				//Win32.SendMessage( Handle, Msg.WM_SETFONT, =
Font.ToHfont().ToInt32(), 0);=0A=
+				//Win32.SetWindowText( Handle, text);=0A=
=20
     			if (HandleCreated !=3D null)
     				HandleCreated (this, e);
Index: ControlPaint.cs=0A=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A=
RCS file: =
/mono/mcs/class/System.Windows.Forms/System.Windows.Forms/ControlPaint.cs=
,v=0A=
retrieving revision 1.15=0A=
diff -u -b -B -r1.15 ControlPaint.cs=0A=
Index: Form.cs=0A=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A=
RCS file: =
/mono/mcs/class/System.Windows.Forms/System.Windows.Forms/Form.cs,v=0A=
retrieving revision 1.30=0A=
diff -u -b -B -r1.30 Form.cs=0A=
--- Form.cs	9 Mar 2003 17:38:41 -0000	1.30=0A=
+++ Form.cs	24 Mar 2003 09:21:38 -0000=0A=
@@ -615,14 +615,8 @@=0A=
     			return base.CreateControlsInstance ();
     		}
    =20
-    		protected override void CreateHandle ()
-    		{
+    		protected override void CreateHandle () {
     			base.CreateHandle ();
-/*
- *	This is called in base class   =20
-    			if (IsHandleCreated)
-    				OnHandleCreated (new EventArgs());
-*/				=09
     		}
    =20
     		protected override void DefWndProc (ref Message m)
Index: ListBox.cs=0A=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A=
RCS file: =
/mono/mcs/class/System.Windows.Forms/System.Windows.Forms/ListBox.cs,v=0A=
retrieving revision 1.22=0A=
diff -u -b -B -r1.22 ListBox.cs=0A=
Index: NativeWindow.cs=0A=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A=
RCS file: =
/mono/mcs/class/System.Windows.Forms/System.Windows.Forms/NativeWindow.cs=
,v=0A=
retrieving revision 1.21=0A=
diff -u -b -B -r1.21 NativeWindow.cs=0A=
--- NativeWindow.cs	1 Mar 2003 21:20:31 -0000	1.21=0A=
+++ NativeWindow.cs	24 Mar 2003 09:21:42 -0000=0A=
@@ -24,7 +24,8 @@=0A=
 		// the window's HWND
 		private IntPtr windowHandle;
 		static private Hashtable windowCollection =3D new Hashtable ();
-		static bool registeredClass =3D false;
+		/* static bool registeredClass =3D false; vkaluzhny@openlinksw.co.uk =
*/=0A=
+		static private Hashtable windowClassesCollection =3D new Hashtable ();=0A=
 	=09
 		// Important!  If this variable was initialized and supplied to =
Windows API,
 		// we cannot *free* (GC) a delegate until all our windows destroyed, =
or better=20
@@ -67,36 +68,13 @@=0A=
 			windowCollection.Add (windowHandle, this);
 			OnHandleChange ();
 		}
-
 		public virtual void CreateHandle (CreateParams cp)=20
 		{
 			if( cp !=3D null ) {
 				IntPtr createdHWnd =3D (IntPtr) 0;
 				Object lpParam =3D new Object();
=20
-				if (!registeredClass) {
-					WNDCLASS wndClass =3D new WNDCLASS();
-
-					wndClass.style =3D (int) (CS_.CS_OWNDC |
-						CS_.CS_VREDRAW |
-						CS_.CS_HREDRAW);
-					wndClass.lpfnWndProc =3D GetWindowProc();
-					wndClass.cbClsExtra =3D 0;
-					wndClass.cbWndExtra =3D 0;
-					wndClass.hInstance =3D (IntPtr)0;
-					wndClass.hIcon =3D (IntPtr)0;
-					wndClass.hCursor =3D Win32.LoadCursor( (IntPtr)0, LC_.IDC_ARROW);
-					wndClass.hbrBackground =3D =
(IntPtr)((int)GetSysColorIndex.COLOR_BTNFACE + 1);
-					wndClass.lpszMenuName =3D "";
-					wndClass.lpszClassName =3D "mono_native_window";
-
-					if (Win32.RegisterClass(ref wndClass) !=3D 0) {
-						registeredClass =3D true;
-					} else {
-						windowHandle =3D (IntPtr)0;
-						return;
-					}
-				}
+                RegisterClass( cp );=0A=
=20
 				windowHandle =3D Win32.CreateWindowEx (
 					(uint) cp.ExStyle, cp.ClassName,
@@ -225,5 +203,33 @@=0A=
 			}
 			return wp;
 		}
+        private void RegisterClass( CreateParams cp ) {            =0A=
+            // these are pre - registered class names;=0A=
+            if (cp.ClassName.ToUpper().CompareTo("BUTTON") =3D=3D 0 ||=0A=
+                cp.ClassName.ToUpper().CompareTo("STATIC") =3D=3D 0 ||=0A=
+                cp.ClassName.ToUpper().CompareTo("EDIT")   =3D=3D 0 ||=0A=
+                cp.ClassName.ToUpper().CompareTo("COMBOBOX") =3D=3D0 ||=0A=
+                cp.ClassName.ToUpper().CompareTo("LISTBOX") =3D=3D 0 ||=0A=
+                cp.ClassName.ToUpper().CompareTo("MDICLIENT") =3D=3D0 ||=0A=
+                cp.ClassName.ToUpper().CompareTo("SCROLLBAR") =3D=3D 0=0A=
+                )  return; // it is not required to reqister them=0A=
+            if (windowClassesCollection[cp.ClassName] =3D=3D null) {=0A=
+                WNDCLASS wndClass =3D new WNDCLASS();=0A=
+                wndClass.lpszClassName =3D cp.ClassName;=0A=
+                wndClass.style =3D cp.ClassStyle;=0A=
+                wndClass.cbWndExtra =3D cp.ExStyle;=0A=
+                wndClass.lpfnWndProc =3D GetWindowProc();=0A=
+                wndClass.cbClsExtra =3D 0;=0A=
+                wndClass.hInstance =3D (IntPtr)0;=0A=
+                wndClass.hIcon =3D (IntPtr)0;=0A=
+                wndClass.hCursor =3D Win32.LoadCursor( (IntPtr)0, =
LC_.IDC_ARROW);=0A=
+                wndClass.hbrBackground =3D =
(IntPtr)((int)GetSysColorIndex.COLOR_BTNFACE + 1);=0A=
+                wndClass.lpszMenuName =3D "";=0A=
+                if (Win32.RegisterClass(ref wndClass) !=3D 0) {=0A=
+                        windowClassesCollection[cp.ClassName] =3D this;=0A=
+                } else=0A=
+                        throw new Exception("Cannot register class: " + =
cp.ClassName);=0A=
+            }=0A=
+        }=0A=
 	}
 }
Index: ScrollableControl.cs=0A=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A=
RCS file: =
/mono/mcs/class/System.Windows.Forms/System.Windows.Forms/ScrollableContr=
ol.cs,v=0A=
retrieving revision 1.16=0A=
diff -u -b -B -r1.16 ScrollableControl.cs=0A=
--- ScrollableControl.cs	1 Mar 2003 21:20:31 -0000	1.16=0A=
+++ ScrollableControl.cs	24 Mar 2003 09:21:43 -0000=0A=
@@ -90,38 +91,21 @@=0A=
=20
 		protected override CreateParams CreateParams {
 			get {
-				if (!classRegistered) {
-					WNDCLASS wndClass =3D new WNDCLASS();
-=20
-					wndClass.style =3D (int) (CS_.CS_OWNDC | CS_.CS_VREDRAW | =
CS_.CS_HREDRAW);
-					wndClass.lpfnWndProc =3D NativeWindow.GetWindowProc();
-					wndClass.cbClsExtra =3D 0;
-					wndClass.cbWndExtra =3D 0;
-					wndClass.hInstance =3D (IntPtr)0;
-					wndClass.hIcon =3D (IntPtr)0;
-					wndClass.hCursor =3D Win32.LoadCursor( (IntPtr)0, LC_.IDC_ARROW);
-					wndClass.hbrBackground =3D =
(IntPtr)((int)GetSysColorIndex.COLOR_BTNFACE + 1);
-					wndClass.lpszMenuName =3D "";
-					wndClass.lpszClassName =3D "mono_scrollable_control";
-   =20
-					if (Win32.RegisterClass(ref wndClass) !=3D 0)=20
-						classRegistered =3D true;=20
-				}	=09
=20
 				CreateParams createParams =3D new CreateParams ();
-				createParams.Caption =3D "Hello World";
-				createParams.ClassName =3D "mono_scrollable_control";
+				createParams.Caption =3D Text;=0A=
+				createParams.ClassName =3D Win32.WindowClass;=0A=
 				createParams.X =3D Left;
 				createParams.Y =3D Top;
 				createParams.Width =3D Width;
 				createParams.Height =3D Height;
-				createParams.ClassStyle =3D 0;
+				createParams.ClassStyle =3D  (int) (CS_.CS_OWNDC | CS_.CS_VREDRAW | =
CS_.CS_HREDRAW);=0A=
 				createParams.ExStyle =3D 0;
 				createParams.Param =3D 0;
   			=09
-				//if (parent !=3D null)
-				//	createParams.Parent =3D parent.Handle;
-				//else=20
+				if (parent !=3D null)=0A=
+					createParams.Parent =3D parent.Handle;=0A=
+				else=0A=
 				createParams.Parent =3D (IntPtr) 0;
 	 =20
 				createParams.Style =3D (int) (WindowStyles.WS_OVERLAPPEDWINDOW);
Index: TextBox.cs=0A=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A=
RCS file: =
/mono/mcs/class/System.Windows.Forms/System.Windows.Forms/TextBox.cs,v=0A=
retrieving revision 1.11=0A=
diff -u -b -B -r1.11 TextBox.cs=0A=
--- TextBox.cs	6 Feb 2003 03:45:10 -0000	1.11=0A=
+++ TextBox.cs	24 Mar 2003 09:21:44 -0000=0A=
@@ -96,9 +96,6 @@=0A=
 			get {
 				if( Parent !=3D null) {
 					CreateParams createParams =3D new CreateParams ();
-					if (window =3D=3D null) {
-						window =3D new ControlNativeWindow (this);
-					}
=20
 					createParams.Caption =3D Text;
 					createParams.ClassName =3D "EDIT";
@@ -113,6 +110,7 @@=0A=
 					createParams.Style =3D (int) (
 						WindowStyles.WS_CHILD |=20
 						WindowStyles.WS_VISIBLE);
+					=0A=
 					return createParams;
 				}
 				return null;
Index: win32functions.cs=0A=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A=
RCS file: =
/mono/mcs/class/System.Windows.Forms/System.Windows.Forms/win32functions.=
cs,v=0A=
retrieving revision 1.14=0A=
diff -u -b -B -r1.14 win32functions.cs=0A=
--- win32functions.cs	15 Mar 2003 19:31:43 -0000	1.14=0A=
+++ win32functions.cs	24 Mar 2003 09:21:56 -0000=0A=
@@ -51,6 +51,10 @@=0A=
 		internal const string REBARCLASSNAME =3D "ReBarWindow32";
 		internal const string PROGRESSBARCLASSNAME =3D "msctls_progress32";
 		internal const string SCROLLBAR =3D "SCROLLBAR";
+        internal const string WindowClass =3D "WindowClass";
+        internal const string FrameClass =3D "FrameClass";
+        internal const string MdiChildClass =3D "MdiChildClass";
+
 		#endregion
=20
 		#region CallBacks

------=_NextPart_000_026E_01C2F21E.40E3B240--