How to convert an API window to a control?
-
in C#, I call an API func CreateWindow to create a new window, and I have its handler which the type is IntPtr. Although I created it with WS_CHILD property and set it parent to a Form (get a handler by property System.Window.Forms.Form.Handler) but my window is still seperate from it parent form. So how can I convert my window to a control in order to add it to my form? Thanks for your attention :)
-
in C#, I call an API func CreateWindow to create a new window, and I have its handler which the type is IntPtr. Although I created it with WS_CHILD property and set it parent to a Form (get a handler by property System.Window.Forms.Form.Handler) but my window is still seperate from it parent form. So how can I convert my window to a control in order to add it to my form? Thanks for your attention :)
This has worked for me in some cases but in others it is kind of flaky. IntPtr hWnd = //your handle from CreateWindow Control control = new Control(); Type type = typeof(Control); MethodInfo method = type.GetMethod("WindowAssignHandle", BindingFlags.Instance | BindingFlags.NonPublic); method.Invoke(control, new object[] {hWnd, false}); {parent form/control}.Controls.Add(control);
-
This has worked for me in some cases but in others it is kind of flaky. IntPtr hWnd = //your handle from CreateWindow Control control = new Control(); Type type = typeof(Control); MethodInfo method = type.GetMethod("WindowAssignHandle", BindingFlags.Instance | BindingFlags.NonPublic); method.Invoke(control, new object[] {hWnd, false}); {parent form/control}.Controls.Add(control);
what version of .NET framework do you use? I listed all the NonPublic methods of type Control to find the function named "WindowAssignHandle" but it doesnt exist :( , whats wrong with me????
-
what version of .NET framework do you use? I listed all the NonPublic methods of type Control to find the function named "WindowAssignHandle" but it doesnt exist :( , whats wrong with me????
I am using 2.0. If you are using 1.1, try the following although I have not tried this: IntPtr hWnd = //your handle from CreateWindow Control control = new Control(); Type type = typeof(Control); FieldInfo field = type.GetField("window", BindingFlags.Instance | BindingFlags.NonPublic); Type windowType = typeof(NativeWindow); NativeWindow window = (NativeWindow)field.GetValue(control); MethodInfo method = windowType.GetMethod("AssignHandle", BindingFlags.Instance | BindingFlags.NonPublic); method.Invoke(window, new object[] {hWnd, false}); {parent form/control}.Controls.Add(control);