NativeMethods.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. //#########################################################################################
  2. //★★★★★★★ http://www.cnpopsoft.com [华普软件] ★★★★★★★
  3. //★★★★★★★ 华普软件 - VB & C#.NET 专业论文与源码荟萃! ★★★★★★★
  4. //#########################################################################################
  5. /*
  6. * Copyright ?2005, Mathew Hall
  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. using XPTable.Themes;
  33. namespace XPTable.Win32
  34. {
  35. /// <summary>
  36. /// A class that contains methods for P/Invoking the Win32 API
  37. /// </summary>
  38. internal abstract class NativeMethods
  39. {
  40. /// <summary>
  41. /// Implemented by many of the Microsoft?Windows?Shell dynamic-link libraries
  42. /// (DLLs) to allow applications to obtain DLL-specific version information
  43. /// </summary>
  44. /// <param name="pdvi">Pointer to a DLLVERSIONINFO structure that receives the
  45. /// version information. The cbSize member must be filled in before calling
  46. /// the function</param>
  47. /// <returns>Returns NOERROR if successful, or an OLE-defined error value otherwise</returns>
  48. [DllImport("Comctl32.dll", CharSet=CharSet.Auto, SetLastError=true)]
  49. internal static extern int DllGetVersion(ref DLLVERSIONINFO pdvi);
  50. /// <summary>
  51. /// Tests if a visual style for the current application is active
  52. /// </summary>
  53. /// <returns>TRUE if a visual style is enabled, and windows with
  54. /// visual styles applied should call OpenThemeData to start using
  55. /// theme drawing services, FALSE otherwise</returns>
  56. [DllImport("UxTheme.dll", CharSet=CharSet.Auto, SetLastError=true)]
  57. internal static extern bool IsThemeActive();
  58. /// <summary>
  59. /// Reports whether the current application's user interface
  60. /// displays using visual styles
  61. /// </summary>
  62. /// <returns>TRUE if the application has a visual style applied,
  63. /// FALSE otherwise</returns>
  64. [DllImport("UxTheme.dll", CharSet=CharSet.Auto, SetLastError=true)]
  65. internal static extern bool IsAppThemed();
  66. /// <summary>
  67. /// Opens the theme data for a window and its associated class
  68. /// </summary>
  69. /// <param name="hwnd">Handle of the window for which theme data
  70. /// is required</param>
  71. /// <param name="pszClassList">Pointer to a string that contains
  72. /// a semicolon-separated list of classes</param>
  73. /// <returns>OpenThemeData tries to match each class, one at a
  74. /// time, to a class data section in the active theme. If a match
  75. /// is found, an associated HTHEME handle is returned. If no match
  76. /// is found NULL is returned</returns>
  77. [DllImport("UxTheme.dll", CharSet=CharSet.Auto, SetLastError=true)]
  78. internal static extern IntPtr OpenThemeData(IntPtr hwnd, [MarshalAs(UnmanagedType.LPTStr)] string pszClassList);
  79. /// <summary>
  80. /// Closes the theme data handle
  81. /// </summary>
  82. /// <param name="hTheme">Handle to a window's specified theme data.
  83. /// Use OpenThemeData to create an HTHEME</param>
  84. /// <returns>Returns S_OK if successful, or an error value otherwise</returns>
  85. [DllImport("UxTheme.dll", CharSet=CharSet.Auto, SetLastError=true)]
  86. internal static extern int CloseThemeData(IntPtr hTheme);
  87. /// <summary>
  88. /// Draws the background image defined by the visual style for the
  89. /// specified control part
  90. /// </summary>
  91. /// <param name="hTheme">Handle to a window's specified theme data.
  92. /// Use OpenThemeData to create an HTHEME</param>
  93. /// <param name="hdc">Handle to a device context (HDC) used for
  94. /// drawing the theme-defined background image</param>
  95. /// <param name="iPartId">Value of type int that specifies the part
  96. /// to draw</param>
  97. /// <param name="iStateId">Value of type int that specifies the state
  98. /// of the part to draw</param>
  99. /// <param name="pRect">Pointer to a RECT structure that contains the
  100. /// rectangle, in logical coordinates, in which the background image
  101. /// is drawn</param>
  102. /// <param name="pClipRect">Pointer to a RECT structure that contains
  103. /// a clipping rectangle. This parameter may be set to NULL</param>
  104. /// <returns>Returns S_OK if successful, or an error value otherwise</returns>
  105. [DllImport("UxTheme.dll", CharSet=CharSet.Auto, SetLastError=true)]
  106. internal static extern int DrawThemeBackground(IntPtr hTheme, IntPtr hdc, int iPartId, int iStateId, ref RECT pRect, ref RECT pClipRect);
  107. /// <summary>
  108. /// The SendMessage function sends the specified message to a
  109. /// window or windows. It calls the window procedure for the
  110. /// specified window and does not return until the window
  111. /// procedure has processed the message
  112. /// </summary>
  113. /// <param name="hwnd">Handle to the window whose window procedure will
  114. /// receive the message</param>
  115. /// <param name="msg">Specifies the message to be sent</param>
  116. /// <param name="wParam">Specifies additional message-specific information</param>
  117. /// <param name="lParam">Specifies additional message-specific information</param>
  118. /// <returns>The return value specifies the result of the message processing;
  119. /// it depends on the message sent</returns>
  120. [DllImport("User32.dll", CharSet=CharSet.Auto, SetLastError=true)]
  121. internal static extern int SendMessage(IntPtr hwnd, int msg, int wParam, int lParam);
  122. /// <summary>
  123. /// The TrackMouseEvent function posts messages when the mouse pointer
  124. /// leaves a window or hovers over a window for a specified amount of time
  125. /// </summary>
  126. /// <param name="tme">A TRACKMOUSEEVENT structure that contains tracking
  127. /// information</param>
  128. /// <returns>true if the function succeeds, false otherwise</returns>
  129. [DllImport("User32.dll", CharSet=CharSet.Auto, SetLastError=true)]
  130. internal static extern bool TrackMouseEvent(TRACKMOUSEEVENT tme);
  131. /// <summary>
  132. /// The PostMessage function places (posts) a message in the message queue associated
  133. /// with the thread that created the specified window and returns without waiting for
  134. /// the thread to process the message
  135. /// </summary>
  136. /// <param name="hwnd">Handle to the window whose window procedure is to receive the
  137. /// message</param>
  138. /// <param name="msg">Specifies the message to be posted</param>
  139. /// <param name="wparam">Specifies additional message-specific information</param>
  140. /// <param name="lparam">Specifies additional message-specific information</param>
  141. /// <returns>If the function succeeds, the return value is nonzero. If the function
  142. /// fails, the return value is zero</returns>
  143. [DllImport("User32.dll", CharSet=CharSet.Auto, SetLastError=true)]
  144. internal static extern IntPtr PostMessage(IntPtr hwnd, int msg, int wparam, int lparam);
  145. /// <summary>
  146. /// The MessageBeep function plays a waveform sound. The waveform sound for each
  147. /// sound type is identified by an entry in the registry
  148. /// </summary>
  149. /// <param name="type">Sound type, as identified by an entry in the registry</param>
  150. /// <returns>If the function succeeds, the return value is nonzero. If the function
  151. /// fails, the return value is zero</returns>
  152. [DllImport("User32.dll", CharSet=CharSet.Auto, SetLastError=true)]
  153. internal static extern bool MessageBeep(int type);
  154. /// <summary>
  155. /// The NotifyWinEvent function signals the system that a predefined event occurred.
  156. /// If any client applications have registered a hook function for the event, the
  157. /// system calls the client's hook function
  158. /// </summary>
  159. /// <param name="winEvent">Specifies the event that occurred</param>
  160. /// <param name="hwnd">Handle to the window that contains the object that generated
  161. /// the event</param>
  162. /// <param name="objType">Identifies the kind of object that generated the event</param>
  163. /// <param name="objID">Identifies whether the event was generated by an object or
  164. /// by a child element of the object. If this value is CHILDID_SELF, the event was
  165. /// generated by the object itself. If not, this value is the child ID of the element
  166. /// that generated the event</param>
  167. [DllImport("User32.dll", CharSet=CharSet.Auto, SetLastError=true)]
  168. internal static extern void NotifyWinEvent(int winEvent, IntPtr hwnd, int objType, int objID);
  169. /// <summary>
  170. /// The ScrollWindow function scrolls the contents of the specified window's client area
  171. /// </summary>
  172. /// <param name="hWnd">Handle to the window where the client area is to be scrolled</param>
  173. /// <param name="XAmount">Specifies the amount, in device units, of horizontal scrolling.
  174. /// This parameter must be a negative value to scroll the content of the window to the left</param>
  175. /// <param name="YAmount">Specifies the amount, in device units, of vertical scrolling.
  176. /// This parameter must be a negative value to scroll the content of the window up</param>
  177. /// <param name="lpRect">Pointer to the RECT structure specifying the portion of the
  178. /// client area to be scrolled. If this parameter is NULL, the entire client area is
  179. /// scrolled</param>
  180. /// <param name="lpClipRect">Pointer to the RECT structure containing the coordinates
  181. /// of the clipping rectangle. Only device bits within the clipping rectangle are affected.
  182. /// Bits scrolled from the outside of the rectangle to the inside are painted; bits scrolled
  183. /// from the inside of the rectangle to the outside are not painted</param>
  184. /// <returns>If the function succeeds, the return value is nonzero. If the function fails,
  185. /// the return value is zero</returns>
  186. [DllImport("User32.dll", CharSet=CharSet.Auto, SetLastError=true)]
  187. internal static extern bool ScrollWindow(IntPtr hWnd, int XAmount, int YAmount, ref RECT lpRect, ref RECT lpClipRect);
  188. /// <summary>
  189. /// The keybd_event function synthesizes a keystroke. The system can use such a synthesized
  190. /// keystroke to generate a WM_KEYUP or WM_KEYDOWN message. The keyboard driver's interrupt
  191. /// handler calls the keybd_event function
  192. /// </summary>
  193. /// <param name="bVk">Specifies a virtual-key code</param>
  194. /// <param name="bScan">This parameter is not used</param>
  195. /// <param name="dwFlags">Specifies various aspects of function operation</param>
  196. /// <param name="dwExtraInfo"></param>
  197. [DllImport("User32.dll", CharSet=CharSet.Auto, SetLastError=true)]
  198. internal extern static void keybd_event(byte bVk, byte bScan, KeyEventFFlags dwFlags, int dwExtraInfo);
  199. /// <summary>
  200. /// The PeekMessage function dispatches incoming sent messages, checks the thread message
  201. /// queue for a posted message, and retrieves the message (if any exist).
  202. /// </summary>
  203. /// <param name="msg">Pointer to an MSG structure that receives message information</param>
  204. /// <param name="hwnd">Handle to the window whose messages are to be examined. The window
  205. /// must belong to the current thread. If hWnd is NULL, PeekMessage retrieves messages for
  206. /// any window that belongs to the current thread. If hWnd is INVALID_HANDLE_VALUE,
  207. /// PeekMessage retrieves messages whose hWnd value is NULL, as posted by the PostThreadMessage
  208. /// function</param>
  209. /// <param name="msgMin">Specifies the value of the first message in the range of messages
  210. /// to be examined. Use WM_KEYFIRST to specify the first keyboard message or WM_MOUSEFIRST
  211. /// to specify the first mouse message. If wMsgFilterMin and wMsgFilterMax are both zero,
  212. /// PeekMessage returns all available messages (that is, no range filtering is performed).</param>
  213. /// <param name="msgMax">Specifies the value of the last message in the range of messages
  214. /// to be examined. Use WM_KEYLAST to specify the first keyboard message or WM_MOUSELAST
  215. /// to specify the last mouse message. If wMsgFilterMin and wMsgFilterMax are both zero,
  216. /// PeekMessage returns all available messages (that is, no range filtering is performed).</param>
  217. /// <param name="remove">Specifies how messages are handled</param>
  218. /// <returns>If a message is available, the return value is nonzero. If no messages are
  219. /// available, the return value is zero</returns>
  220. [DllImport("User32.dll", CharSet=CharSet.Auto, SetLastError=true)]
  221. internal static extern bool PeekMessage(ref MSG msg, IntPtr hwnd, int msgMin, int msgMax, int remove);
  222. /// <summary>
  223. /// The TranslateMessage function translates virtual-key messages into character messages.
  224. /// The character messages are posted to the calling thread's message queue, to be read the
  225. /// next time the thread calls the GetMessage or PeekMessage function
  226. /// </summary>
  227. /// <param name="msg">Pointer to an MSG structure that contains message information retrieved
  228. /// from the calling thread's message queue by using the GetMessage or PeekMessage function</param>
  229. /// <returns>If the message is translated (that is, a character message is posted to the
  230. /// thread's message queue), the return value is nonzero.If the message is WM_KEYDOWN,
  231. /// WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP, the return value is nonzero, regardless of
  232. /// the translation. If the message is not translated (that is, a character message is not
  233. /// posted to the thread's message queue), the return value is zero</returns>
  234. [DllImport("User32.dll", CharSet=CharSet.Auto, SetLastError=true)]
  235. internal static extern bool TranslateMessage(ref MSG msg);
  236. /// <summary>
  237. /// The DispatchMessage function dispatches a message to a window procedure. It is typically
  238. /// used to dispatch a message retrieved by the GetMessage funct
  239. /// </summary>
  240. /// <param name="msg">Pointer to an MSG structure that contains the message</param>
  241. /// <returns>The return value specifies the value returned by the window procedure. Although
  242. /// its meaning depends on the message being dispatched, the return value generally is ignored</returns>
  243. [DllImport("User32.dll", CharSet=CharSet.Auto, SetLastError=true)]
  244. internal static extern int DispatchMessage(ref MSG msg);
  245. }
  246. }