NativeWindow.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2004 Novell, Inc.
  21. //
  22. // Authors:
  23. // Peter Bartok [email protected]
  24. //
  25. //
  26. // $Revision: 1.2 $
  27. // $Modtime: $
  28. // $Log: NativeWindow.cs,v $
  29. // Revision 1.2 2004/08/11 22:20:59 pbartok
  30. // - Signature fixes
  31. //
  32. // Revision 1.1 2004/07/09 05:21:25 pbartok
  33. // - Initial check-in
  34. //
  35. //
  36. // COMPLETE
  37. using System.Runtime.Remoting;
  38. using System.Runtime.InteropServices;
  39. using System.Runtime.CompilerServices;
  40. using System.Collections;
  41. using System.Diagnostics;
  42. namespace System.Windows.Forms
  43. {
  44. public class NativeWindow : MarshalByRefObject {
  45. internal IntPtr window_handle;
  46. static private Hashtable window_collection = new Hashtable();
  47. #region Public Constructors
  48. public NativeWindow() {
  49. window_handle=IntPtr.Zero;
  50. }
  51. #endregion // Public Constructors
  52. #region Public Instance Properties
  53. public IntPtr Handle {
  54. get {
  55. return window_handle;
  56. }
  57. }
  58. #endregion // Public Instance Properties
  59. #region Public Static Methods
  60. public static NativeWindow FromHandle(IntPtr handle) {
  61. NativeWindow window=new NativeWindow();
  62. window.AssignHandle(handle);
  63. return window;
  64. }
  65. #endregion // Public Static Methods
  66. #region Public Instance Methods
  67. public void AssignHandle(IntPtr handle) {
  68. if (window_handle != IntPtr.Zero) {
  69. window_collection.Remove(window_handle);
  70. }
  71. window_handle=handle;
  72. window_collection.Add(window_handle, this);
  73. OnHandleChange();
  74. }
  75. public virtual void CreateHandle(CreateParams create_params) {
  76. if (create_params != null) {
  77. window_handle=XplatUI.CreateWindow(create_params);
  78. if (window_handle != IntPtr.Zero) {
  79. window_collection.Add(window_handle, this);
  80. }
  81. }
  82. }
  83. public void DefWndProc(ref Message m) {
  84. m.Result=XplatUI.DefWndProc(ref m);
  85. }
  86. public virtual void DestroyHandle() {
  87. window_collection.Remove(window_handle);
  88. XplatUI.DestroyWindow(window_handle);
  89. window_handle=IntPtr.Zero;
  90. }
  91. public virtual void ReleaseHandle() {
  92. window_handle=IntPtr.Zero;
  93. OnHandleChange();
  94. }
  95. #endregion // Public Instance Methods
  96. #region Protected Instance Methods
  97. ~NativeWindow() {
  98. if (window_handle!=IntPtr.Zero) {
  99. DestroyHandle();
  100. }
  101. }
  102. protected virtual void OnHandleChange() {
  103. }
  104. protected virtual void OnThreadException(Exception e) {
  105. Application.OnThreadException(e);
  106. }
  107. protected virtual void WndProc(ref Message m) {
  108. #if debug
  109. Console.WriteLine("NativeWindow.cs: WndProc(ref Message m) called");
  110. #endif
  111. DefWndProc(ref m);
  112. }
  113. internal static IntPtr WndProc(IntPtr hWnd, Msg msg, IntPtr wParam, IntPtr lParam) {
  114. Message m = new Message();
  115. NativeWindow window = null;
  116. try {
  117. window = (NativeWindow)window_collection[hWnd];
  118. m.HWnd=hWnd;
  119. m.Msg=(int)msg;
  120. m.WParam=wParam;
  121. m.LParam=lParam;
  122. m.Result=IntPtr.Zero;
  123. if (window != null) {
  124. window.WndProc(ref m);
  125. } else {
  126. m.Result=XplatUI.DefWndProc(ref m);
  127. }
  128. }
  129. catch(System.Exception ex) {
  130. if (window != null) {
  131. window.OnThreadException(ex);
  132. }
  133. }
  134. #if debug
  135. Console.WriteLine("NativeWindow.cs: Message {0}, result {1}", msg, m.Result);
  136. #endif
  137. return m.Result;
  138. }
  139. #endregion // Protected Instance Methods
  140. }
  141. }