NativeWindow.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //#########################################################################################
  2. //★★★★★★★ http://www.cnpopsoft.com [华普软件] ★★★★★★★
  3. //★★★★★★★ 华普软件 - VB & C#.NET 专业论文与源码荟萃! ★★★★★★★
  4. //#########################################################################################
  5. /*
  6. * Copyright ?2004, Daniel Turini
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. *
  12. * - Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  25. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  28. * OF SUCH DAMAGE.
  29. */
  30. using System;
  31. using System.Runtime.InteropServices;
  32. namespace XPTable.Win32
  33. {
  34. /// <summary>
  35. /// Summary description for NativeWindow
  36. /// </summary>
  37. internal class NativeWindow
  38. {
  39. #region Class Data
  40. /// <summary>
  41. ///
  42. /// </summary>
  43. private IntPtr handle;
  44. /// <summary>
  45. /// Prevents the delegate being collected
  46. /// </summary>
  47. private WndProcDelegate wndProcDelegate;
  48. /// <summary>
  49. ///
  50. /// </summary>
  51. private IntPtr oldWndFunc;
  52. /// <summary>
  53. ///
  54. /// </summary>
  55. private delegate IntPtr WndProcDelegate(IntPtr hwnd, int Msg, IntPtr wParam, IntPtr lParam);
  56. /// <summary>
  57. ///
  58. /// </summary>
  59. private const int GWL_WNDPROC = -4;
  60. #endregion
  61. #region Constructor
  62. /// <summary>
  63. /// Initializes a new instance of the NativeWindow class
  64. /// </summary>
  65. public NativeWindow()
  66. {
  67. wndProcDelegate = new WndProcDelegate(this.WndProc);
  68. }
  69. #endregion
  70. #region Methods
  71. /// <summary>
  72. /// Assigns a handle to this window
  73. /// </summary>
  74. /// <param name="hWnd">The handle to assign to this window</param>
  75. public void AssignHandle(IntPtr hWnd)
  76. {
  77. handle = hWnd;
  78. oldWndFunc = SetWindowLong(hWnd, GWL_WNDPROC, wndProcDelegate);
  79. }
  80. /// <summary>
  81. /// Releases the handle associated with this window
  82. /// </summary>
  83. public void ReleaseHandle()
  84. {
  85. SetWindowLong(handle, GWL_WNDPROC, oldWndFunc);
  86. handle = IntPtr.Zero;
  87. oldWndFunc = IntPtr.Zero;
  88. }
  89. /// <summary>
  90. /// Invokes the default window procedure associated with this window
  91. /// </summary>
  92. /// <param name="msg">A Message that is associated with the current Windows message</param>
  93. protected virtual void WndProc(ref System.Windows.Forms.Message msg)
  94. {
  95. DefWndProc(ref msg);
  96. }
  97. /// <summary>
  98. /// Invokes the default window procedure associated with this window.
  99. /// It is an error to call this method when the Handle property is 0
  100. /// </summary>
  101. /// <param name="m">A Message that is associated with the current Windows message</param>
  102. public void DefWndProc(ref System.Windows.Forms.Message m)
  103. {
  104. m.Result = CallWindowProc(oldWndFunc, m.HWnd, m.Msg, m.WParam, m.LParam);
  105. }
  106. /// <summary>
  107. /// Handler for the WndProcDelegate
  108. /// </summary>
  109. /// <param name="hWnd">Handle to the window procedure to receive the message</param>
  110. /// <param name="msg">Specifies the message</param>
  111. /// <param name="wParam">Specifies additional message-specific information. The contents
  112. /// of this parameter depend on the value of the Msg parameter</param>
  113. /// <param name="lParam">Specifies additional message-specific information. The contents
  114. /// of this parameter depend on the value of the Msg parameter</param>
  115. /// <returns>The return value specifies the result of the message processing and depends
  116. /// on the message sent</returns>
  117. private IntPtr WndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam)
  118. {
  119. System.Windows.Forms.Message m = System.Windows.Forms.Message.Create(hWnd, msg, wParam, lParam);
  120. WndProc(ref m);
  121. return m.Result;
  122. }
  123. /// <summary>
  124. /// The SetWindowLong function changes an attribute of the specified window. The
  125. /// function also sets the 32-bit (long) value at the specified offset into the
  126. /// extra window memory
  127. /// </summary>
  128. /// <param name="hWnd">Handle to the window and, indirectly, the class to which
  129. /// the window belongs</param>
  130. /// <param name="nIndex">Specifies the zero-based offset to the value to be set.</param>
  131. /// <param name="wndProcDelegate">Specifies the replacement value</param>
  132. /// <returns>If the function succeeds, the return value is the previous value of
  133. /// the specified 32-bit integer. If the function fails, the return value is zero</returns>
  134. [DllImport("User32.dll", CharSet=CharSet.Auto, SetLastError=true)]
  135. private static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, WndProcDelegate wndProcDelegate);
  136. /// <summary>
  137. /// The SetWindowLong function changes an attribute of the specified window. The
  138. /// function also sets the 32-bit (long) value at the specified offset into the
  139. /// extra window memory
  140. /// </summary>
  141. /// <param name="hWnd">Handle to the window and, indirectly, the class to which
  142. /// the window belongs</param>
  143. /// <param name="nIndex">Specifies the zero-based offset to the value to be set.</param>
  144. /// <param name="wndFunc">Specifies the replacement value</param>
  145. /// <returns>If the function succeeds, the return value is the previous value of
  146. /// the specified 32-bit integer. If the function fails, the return value is zero</returns>
  147. [DllImport("User32.dll", CharSet=CharSet.Auto, SetLastError=true)]
  148. private static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr wndFunc);
  149. /// <summary>
  150. /// The CallWindowProc function passes message information to the specified window
  151. /// procedure
  152. /// </summary>
  153. /// <param name="prevWndFunc">Pointer to the previous window procedure. If this value
  154. /// is obtained by calling the GetWindowLong function with the nIndex parameter set to
  155. /// GWL_WNDPROC or DWL_DLGPROC, it is actually either the address of a window or dialog
  156. /// box procedure, or a special internal value meaningful only to CallWindowProc</param>
  157. /// <param name="hWnd">Handle to the window procedure to receive the message</param>
  158. /// <param name="iMsg">Specifies the message</param>
  159. /// <param name="wParam">Specifies additional message-specific information. The contents
  160. /// of this parameter depend on the value of the Msg parameter</param>
  161. /// <param name="lParam">Specifies additional message-specific information. The contents
  162. /// of this parameter depend on the value of the Msg parameter</param>
  163. /// <returns>The return value specifies the result of the message processing and depends
  164. /// on the message sent</returns>
  165. [DllImport("User32.dll", CharSet=CharSet.Auto, SetLastError=true)]
  166. private static extern IntPtr CallWindowProc(IntPtr prevWndFunc, IntPtr hWnd, int iMsg, IntPtr wParam, IntPtr lParam);
  167. #endregion
  168. #region Properties
  169. /// <summary>
  170. /// Gets the handle for this window
  171. /// </summary>
  172. public IntPtr Handle
  173. {
  174. get
  175. {
  176. return this.handle;
  177. }
  178. }
  179. #endregion
  180. }
  181. }