_Util.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System;
  2. namespace Microsoft.Win32
  3. {
  4. /// <summary>
  5. /// Util
  6. /// </summary>
  7. public static partial class Util
  8. {
  9. /// <summary>
  10. /// 获取有符号低16位整数
  11. /// </summary>
  12. /// <param name="lParam">句柄</param>
  13. /// <returns>有符号低16位整数</returns>
  14. public static int GET_X_LPARAM(IntPtr lParam)
  15. {
  16. return LOWORD(lParam);
  17. }
  18. /// <summary>
  19. /// 获取有符号高十六位整数
  20. /// </summary>
  21. /// <param name="lParam">句柄</param>
  22. /// <returns>有符号高十六位整数</returns>
  23. public static int GET_Y_LPARAM(IntPtr lParam)
  24. {
  25. return HIWORD(lParam);
  26. }
  27. /// <summary>
  28. /// 获取无符号高8位整数
  29. /// </summary>
  30. /// <param name="wValue">无符号16位整数</param>
  31. /// <returns>无符号高8位整数</returns>
  32. public static byte HIBYTE(ushort wValue)
  33. {
  34. return (byte)((wValue >> 0x8) & 0xff);
  35. }
  36. /// <summary>
  37. /// 获取无符号低8位整数
  38. /// </summary>
  39. /// <param name="wValue">无符号16位整数</param>
  40. /// <returns>无符号低8位整数</returns>
  41. public static byte LOBYTE(ushort wValue)
  42. {
  43. return (byte)(wValue & 0xff);
  44. }
  45. /// <summary>
  46. /// 获取无符号高16位整数
  47. /// </summary>
  48. /// <param name="dwValue">无符号32位整数</param>
  49. /// <returns>无符号高16位整数</returns>
  50. public static ushort HIWORD(uint dwValue)
  51. {
  52. return (ushort)((dwValue >> 0x10) & 0xffff);
  53. }
  54. /// <summary>
  55. /// 获取无符号低16位整数
  56. /// </summary>
  57. /// <param name="dwValue">无符号32位整数</param>
  58. /// <returns>无符号低16位整数</returns>
  59. public static ushort LOWORD(uint dwValue)
  60. {
  61. return (ushort)(dwValue & 0xffff);
  62. }
  63. /// <summary>
  64. /// 将两个无符号8位整数合并为一个无符号16位整数
  65. /// </summary>
  66. /// <param name="bLow">无符号低8位整数</param>
  67. /// <param name="bHigh">无符号高8位整数</param>
  68. /// <returns>无符号16位整数</returns>
  69. public static ushort MAKEWORD(byte bLow, byte bHigh)
  70. {
  71. return (ushort)((((ushort)bHigh) << 0x8) | ((ushort)(bLow & 0xff)));
  72. }
  73. /// <summary>
  74. /// 将两个无符号16位整数合并为一个无符号32位整数
  75. /// </summary>
  76. /// <param name="wLow">无符号低16位整数</param>
  77. /// <param name="wHigh">无符号高16位整数</param>
  78. /// <returns>无符号32位整数</returns>
  79. public static uint MAKELONG(ushort wLow, ushort wHigh)
  80. {
  81. return ((((uint)wHigh) << 0x10) | ((uint)(wLow & 0xffff)));
  82. }
  83. /// <summary>
  84. /// 将两个无符号16位整数合并为一个有符号32位整数
  85. /// </summary>
  86. /// <param name="wLow">无符号低16位整数</param>
  87. /// <param name="wHigh">无符号高16位整数</param>
  88. /// <returns>有符号32位整数</returns>
  89. public static int MAKELPARAM(ushort wLow, ushort wHigh)
  90. {
  91. return (int)MAKELONG(wLow, wHigh);
  92. }
  93. /// <summary>
  94. /// 将两个无符号16位整数合并为一个有符号32位整数
  95. /// </summary>
  96. /// <param name="wLow">无符号低16位整数</param>
  97. /// <param name="wHigh">无符号高16位整数</param>
  98. /// <returns>有符号32位整数</returns>
  99. public static int MAKEWPARAM(ushort wLow, ushort wHigh)
  100. {
  101. return (int)MAKELONG(wLow, wHigh);
  102. }
  103. /// <summary>
  104. /// 将两个无符号16位整数合并为一个有符号句柄
  105. /// </summary>
  106. /// <param name="wLow">无符号低16位整数</param>
  107. /// <param name="wHigh">无符号高16位整数</param>
  108. /// <returns>有符号句柄</returns>
  109. public static IntPtr MAKELRESULT(ushort wLow, ushort wHigh)
  110. {
  111. return (IntPtr)MAKELONG(wLow, wHigh);
  112. }
  113. /// <summary>
  114. /// 获取无符号高16位整数
  115. /// </summary>
  116. /// <param name="n">32位有符号整数</param>
  117. /// <returns>无符号高16位整数</returns>
  118. public static int HIWORD(int n)
  119. {
  120. return ((n >> 0x10) & 0xffff);
  121. }
  122. /// <summary>
  123. /// 获取无符号高16位整数
  124. /// </summary>
  125. /// <param name="n">句柄</param>
  126. /// <returns>无符号高16位整数</returns>
  127. public static int HIWORD(IntPtr n)
  128. {
  129. return HIWORD((int)((long)n));
  130. }
  131. /// <summary>
  132. /// 获取无符号低16位整数
  133. /// </summary>
  134. /// <param name="n">32位有符号整数</param>
  135. /// <returns>无符号低16位整数</returns>
  136. public static int LOWORD(int n)
  137. {
  138. return (n & 0xffff);
  139. }
  140. /// <summary>
  141. /// 获取无符号低16位整数
  142. /// </summary>
  143. /// <param name="n">句柄</param>
  144. /// <returns>无符号低16位整数</returns>
  145. public static int LOWORD(IntPtr n)
  146. {
  147. return LOWORD((int)((long)n));
  148. }
  149. /// <summary>
  150. /// 获取有符号高16位整数
  151. /// </summary>
  152. /// <param name="n">32位有符号整数</param>
  153. /// <returns>有符号高16位整数</returns>
  154. public static int SignedHIWORD(int n)
  155. {
  156. return (short)((n >> 0x10) & 0xffff);
  157. }
  158. /// <summary>
  159. /// 获取有符号高16位整数
  160. /// </summary>
  161. /// <param name="n">句柄</param>
  162. /// <returns>有符号高16位整数</returns>
  163. public static int SignedHIWORD(IntPtr n)
  164. {
  165. return SignedHIWORD((int)((long)n));
  166. }
  167. /// <summary>
  168. /// 获取有符号低16位整数
  169. /// </summary>
  170. /// <param name="n">32位有符号整数</param>
  171. /// <returns>有符号低16位整数</returns>
  172. public static int SignedLOWORD(int n)
  173. {
  174. return (short)(n & 0xffff);
  175. }
  176. /// <summary>
  177. /// 获取有符号低16位整数
  178. /// </summary>
  179. /// <param name="n">句柄</param>
  180. /// <returns>有符号低16位整数</returns>
  181. public static int SignedLOWORD(IntPtr n)
  182. {
  183. return SignedLOWORD((int)((long)n));
  184. }
  185. /// <summary>
  186. /// 将两个16位整数合并为一个32位整数
  187. /// </summary>
  188. /// <param name="low">低16位整数</param>
  189. /// <param name="high">高16为整数</param>
  190. /// <returns>32位整数</returns>
  191. public static int MAKELONG(int low, int high)
  192. {
  193. return ((high << 0x10) | (low & 0xffff));
  194. }
  195. /// <summary>
  196. /// 将两个16位整数合并为句柄
  197. /// </summary>
  198. /// <param name="low">低16位整数</param>
  199. /// <param name="high">高16为整数</param>
  200. /// <returns>LParam</returns>
  201. public static IntPtr MAKELPARAM(int low, int high)
  202. {
  203. return (IntPtr)((high << 0x10) | (low & 0xffff));
  204. }
  205. }
  206. }