NativeWindow.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //
  2. // System.Windows.Forms.NativeWindow.cs
  3. //
  4. // Author:
  5. // stubbed out by Paul Osman ([email protected])
  6. // Dennis Hayes ([email protected])
  7. // WINELib implementation started by John Sohn ([email protected])
  8. //
  9. // (C) 2002 Ximian, Inc
  10. //
  11. using System.Runtime.Remoting;
  12. using System.Runtime.InteropServices;
  13. using System.Runtime.CompilerServices;
  14. using System.Collections;
  15. namespace System.Windows.Forms {
  16. // <summary>
  17. // Implementation started.
  18. //
  19. // </summary>
  20. public class NativeWindow : MarshalByRefObject {
  21. // the window's HWND
  22. private IntPtr windowHandle;
  23. static private Hashtable windowCollection = new Hashtable ();
  24. static bool registeredClass = false;
  25. //
  26. // --- Constructor
  27. //
  28. public NativeWindow ()
  29. {
  30. windowHandle = (IntPtr) 0;
  31. }
  32. //
  33. // --- Public Properties
  34. //
  35. public IntPtr Handle
  36. {
  37. get {
  38. return windowHandle;
  39. }
  40. }
  41. //
  42. // --- Public Methods
  43. //
  44. public void AssignHandle (IntPtr handle)
  45. {
  46. if (windowHandle != (IntPtr) 0)
  47. windowCollection.Remove (windowHandle);
  48. windowHandle = handle;
  49. windowCollection.Add (windowHandle, this);
  50. OnHandleChange ();
  51. }
  52. public virtual void CreateHandle (CreateParams cp)
  53. {
  54. IntPtr createdHWnd = (IntPtr) 0;
  55. Object lpParam = new Object();
  56. if (!registeredClass) {
  57. Win32.WndProc wp = new Win32.WndProc (WndProc);
  58. if (Win32.MonoRegisterClass(
  59. (int) (Win32.CS_OWNDC |
  60. Win32.CS_VREDRAW |
  61. Win32.CS_HREDRAW),
  62. wp, 0, 0, (IntPtr) 0, (IntPtr) 0,
  63. (IntPtr) 0, (IntPtr) 6, "",
  64. "mono_native_window") != 0) {
  65. registeredClass = true;
  66. } else {
  67. windowHandle = (IntPtr) 0;
  68. return;
  69. }
  70. }
  71. windowHandle = Win32.CreateWindowExA (
  72. (uint) cp.ExStyle, cp.ClassName,
  73. cp.Caption,(uint) cp.Style,
  74. cp.X, cp.Y, cp.Width, cp.Height,
  75. (IntPtr) cp.Parent, (IntPtr) 0,
  76. (IntPtr) 0, ref lpParam);
  77. if (windowHandle != (IntPtr) 0)
  78. windowCollection.Add (windowHandle, this);
  79. }
  80. [MonoTODO]
  81. public override ObjRef CreateObjRef (Type requestedType)
  82. {
  83. throw new NotImplementedException ();
  84. }
  85. public void DefWndProc (ref Message m)
  86. {
  87. m.Result = Win32.DefWindowProcA (m.HWnd, m.Msg,
  88. m.WParam, m.LParam);
  89. }
  90. public virtual void DestroyHandle ()
  91. {
  92. windowCollection.Remove (windowHandle);
  93. Win32.DestroyWindow (windowHandle);
  94. }
  95. [MonoTODO]
  96. public override bool Equals (object o)
  97. {
  98. throw new NotImplementedException ();
  99. }
  100. //inherited
  101. //public static bool Equals(object o1, object o2)
  102. //{
  103. // throw new NotImplementedException ();
  104. //}
  105. [MonoTODO]
  106. public override int GetHashCode ()
  107. {
  108. //FIXME add our proprities
  109. return base.GetHashCode ();
  110. }
  111. public static NativeWindow FromHandle (IntPtr handle)
  112. {
  113. NativeWindow window = new NativeWindow ();
  114. window.AssignHandle (handle);
  115. return window;
  116. }
  117. //inherited
  118. //public object GetLifetimeService() {
  119. // throw new NotImplementedException ();
  120. //}
  121. //public Type GetType() {
  122. // throw new NotImplementedException ();
  123. //}
  124. //public virtual object InitializeLifetimeService(){
  125. // throw new NotImplementedException ();
  126. //}
  127. public virtual void ReleaseHandle ()
  128. {
  129. windowHandle = (IntPtr) 0;
  130. OnHandleChange ();
  131. }
  132. [MonoTODO]
  133. public override string ToString ()
  134. {
  135. throw new NotImplementedException ();
  136. }
  137. //
  138. // --- Protected Methods
  139. //
  140. //inherited
  141. //protected object MemberwiseClone() {
  142. // throw new NotImplementedException ();
  143. //}
  144. [MonoTODO]
  145. protected virtual void OnHandleChange ()
  146. {
  147. // to be overridden
  148. }
  149. [MonoTODO]
  150. protected virtual void OnThreadException (Exception e)
  151. {
  152. throw new NotImplementedException ();
  153. }
  154. protected virtual void WndProc (ref Message m)
  155. {
  156. if (m.Msg == Win32.WM_CREATE)
  157. Console.WriteLine ("NW WndProc WM_CREATE");
  158. DefWndProc (ref m);
  159. }
  160. //
  161. // --- Destructor
  162. //
  163. [MonoTODO]
  164. ~NativeWindow ()
  165. {
  166. }
  167. static private IntPtr WndProc (
  168. IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam)
  169. {
  170. // windowCollection is a collection of all the
  171. // NativeWindow(s) that have been created.
  172. // Dispatch the current message to the approriate
  173. // window.
  174. NativeWindow window =
  175. (NativeWindow) windowCollection[hWnd];
  176. Message message = new Message ();
  177. message.HWnd = hWnd;
  178. message.Msg = msg;
  179. message.WParam = wParam;
  180. message.LParam = lParam;
  181. message.Result = (IntPtr) 0;
  182. if (msg == Win32.WM_CREATE)
  183. Console.WriteLine ("WM_CREATE (static)");
  184. if (window != null) {
  185. if (msg == Win32.WM_CREATE)
  186. Console.WriteLine ("WM_CREATE (static != null)");
  187. window.WndProc(ref message);
  188. } else {
  189. Console.WriteLine ("no window, defwndproc");
  190. // even though we are not managing the
  191. // window let the window get the message
  192. message.Result = Win32.DefWindowProcA (
  193. hWnd, msg, wParam, lParam);
  194. }
  195. return message.Result;
  196. }
  197. }
  198. }