SafeNativeMethods.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Security;
  4. using System.Windows.Forms;
  5. namespace SmartCoalApplication.SystemLayer
  6. {
  7. [SuppressUnmanagedCodeSecurity]
  8. public static class SafeNativeMethods
  9. {
  10. [DllImport("user32.dll", SetLastError = true)]
  11. [return: MarshalAs(UnmanagedType.Bool)]
  12. internal static extern bool DrawMenuBar(IntPtr hWnd);
  13. [DllImport("user32.dll", SetLastError = false)]
  14. internal static extern IntPtr GetSystemMenu(
  15. IntPtr hWnd,
  16. [MarshalAs(UnmanagedType.Bool)] bool bRevert);
  17. [DllImport("user32.dll", SetLastError = false)]
  18. internal static extern int EnableMenuItem(
  19. IntPtr hMenu,
  20. uint uIDEnableItem,
  21. uint uEnable);
  22. [DllImport("user32.dll", SetLastError = false)]
  23. [return: MarshalAs(UnmanagedType.Bool)]
  24. internal static extern bool FlashWindow(
  25. IntPtr hWnd,
  26. [MarshalAs(UnmanagedType.Bool)] bool bInvert);
  27. [DllImport("user32.dll", SetLastError = true)]
  28. [return: MarshalAs(UnmanagedType.Bool)]
  29. internal static extern bool ShowScrollBar(
  30. IntPtr hWnd,
  31. int wBar,
  32. [MarshalAs(UnmanagedType.Bool)] bool bShow);
  33. [DllImport("user32.dll", SetLastError = true)]
  34. [return: MarshalAs(UnmanagedType.Bool)]
  35. internal static extern bool GetLayeredWindowAttributes(
  36. IntPtr hwnd,
  37. out uint pcrKey,
  38. out byte pbAlpha,
  39. out uint pdwFlags);
  40. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Portability", "CA1901:PInvokeDeclarationsShouldBePortable", MessageId = "2")]
  41. [DllImport("user32.dll", SetLastError = true)]
  42. [return: MarshalAs(UnmanagedType.Bool)]
  43. internal static extern bool SetLayeredWindowAttributes(
  44. IntPtr hwnd,
  45. uint crKey,
  46. byte bAlpha,
  47. uint dwFlags);
  48. [DllImport("gdi32.dll", SetLastError = true)]
  49. internal static extern IntPtr CreateDIBSection(
  50. IntPtr hdc,
  51. ref NativeStructs.BITMAPINFO pbmi,
  52. uint iUsage,
  53. out IntPtr ppvBits,
  54. IntPtr hSection,
  55. uint dwOffset);
  56. [DllImport("user32.dll", SetLastError = false)]
  57. internal static extern int GetUpdateRgn(
  58. IntPtr hWnd,
  59. IntPtr hRgn,
  60. [MarshalAs(UnmanagedType.Bool)] bool bErase);
  61. [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  62. internal static extern IntPtr FindWindowExW(
  63. IntPtr hwndParent,
  64. IntPtr hwndChildAfter,
  65. [MarshalAs(UnmanagedType.LPWStr)] string lpszClass,
  66. [MarshalAs(UnmanagedType.LPWStr)] string lpszWindow);
  67. [DllImport("user32.dll", SetLastError = false)]
  68. internal static extern IntPtr SendMessageW(
  69. IntPtr hWnd,
  70. uint msg,
  71. IntPtr wParam,
  72. IntPtr lParam);
  73. [DllImport("user32.dll", SetLastError = true)]
  74. [return: MarshalAs(UnmanagedType.Bool)]
  75. internal extern static bool PostMessageW(
  76. IntPtr handle,
  77. uint msg,
  78. IntPtr wParam,
  79. IntPtr lParam);
  80. [DllImport("user32.dll", SetLastError = true)]
  81. internal static extern uint GetWindowLongW(
  82. IntPtr hWnd,
  83. int nIndex);
  84. [DllImport("user32.dll", SetLastError = true)]
  85. internal static extern uint SetWindowLongW(
  86. IntPtr hWnd,
  87. int nIndex,
  88. uint dwNewLong);
  89. [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
  90. internal static extern unsafe void memcpy(void* dst, void* src, UIntPtr length);
  91. [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
  92. internal static extern unsafe void memset(void* dst, int c, UIntPtr length);
  93. [DllImport("User32.dll", SetLastError = false)]
  94. internal static extern int GetSystemMetrics(int nIndex);
  95. [DllImport("kernel32.dll", SetLastError = true)]
  96. [return: MarshalAs(UnmanagedType.Bool)]
  97. internal static extern bool QueryPerformanceCounter(out ulong lpPerformanceCount);
  98. [DllImport("kernel32.dll", SetLastError = true)]
  99. [return: MarshalAs(UnmanagedType.Bool)]
  100. internal static extern bool QueryPerformanceFrequency(out ulong lpFrequency);
  101. [DllImport("kernel32.dll", SetLastError = true)]
  102. [return: MarshalAs(UnmanagedType.Bool)]
  103. internal static extern bool CloseHandle(IntPtr hObject);
  104. [DllImport("kernel32.dll", SetLastError = true)]
  105. internal static extern uint WaitForSingleObject(IntPtr hHandle,uint dwMilliseconds);
  106. [DllImport("kernel32.dll", SetLastError = true)]
  107. [return: MarshalAs(UnmanagedType.Bool)]
  108. internal static extern bool SetHandleInformation(
  109. IntPtr hObject,
  110. uint dwMask,
  111. uint dwFlags);
  112. [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  113. internal static extern IntPtr FindWindowW(
  114. [MarshalAs(UnmanagedType.LPWStr)] string lpClassName,
  115. [MarshalAs(UnmanagedType.LPWStr)] string lpWindowName);
  116. [DllImport("user32.dll", SetLastError = false)]
  117. internal static extern uint GetWindowThreadProcessId(
  118. IntPtr hWnd,
  119. out uint lpdwProcessId);
  120. [DllImport("kernel32.dll", SetLastError = true)]
  121. internal static extern uint WaitForMultipleObjects(
  122. uint nCount,
  123. IntPtr[] lpHandles,
  124. [MarshalAs(UnmanagedType.Bool)] bool bWaitAll,
  125. uint dwMilliseconds);
  126. [DllImport("Gdi32.Dll", SetLastError = true)]
  127. public extern static IntPtr CreateCompatibleDC(IntPtr hdc);
  128. [DllImport("Gdi32.dll", SetLastError = true)]
  129. public extern static uint DeleteDC(IntPtr hdc);
  130. [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  131. [return: MarshalAs(UnmanagedType.Bool)]
  132. internal static extern bool GetVersionEx(ref NativeStructs.OSVERSIONINFOEX lpVersionInfo);
  133. internal static uint WaitForMultipleObjects(IntPtr[] lpHandles, bool bWaitAll, uint dwMilliseconds)
  134. {
  135. return WaitForMultipleObjects((uint)lpHandles.Length, lpHandles, bWaitAll, dwMilliseconds);
  136. }
  137. [DllImport("wtsapi32.dll", SetLastError = true)]
  138. internal static extern uint WTSRegisterSessionNotification(IntPtr hWnd, uint dwFlags);
  139. [DllImport("wtsapi32.dll", SetLastError = true)]
  140. internal static extern uint WTSUnRegisterSessionNotification(IntPtr hWnd);
  141. [DllImport("Gdi32.dll", SetLastError = true)]
  142. internal unsafe static extern uint GetRegionData(
  143. IntPtr hRgn,
  144. uint dwCount,
  145. NativeStructs.RGNDATA* lpRgnData);
  146. [DllImport("Gdi32.dll", SetLastError = true)]
  147. internal unsafe static extern IntPtr CreateRectRgn(
  148. int nLeftRect,
  149. int nTopRect,
  150. int nRightRect,
  151. int nBottomRect);
  152. [DllImport("Gdi32.dll", SetLastError = true)]
  153. [return: MarshalAs(UnmanagedType.Bool)]
  154. public extern static bool DeleteObject(IntPtr hObject);
  155. [DllImport("kernel32.dll", SetLastError = true)]
  156. internal static extern IntPtr VirtualAlloc(
  157. IntPtr lpAddress,
  158. UIntPtr dwSize,
  159. uint flAllocationType,
  160. uint flProtect);
  161. [DllImport("kernel32.dll", SetLastError = true)]
  162. [return: MarshalAs(UnmanagedType.Bool)]
  163. internal static extern bool VirtualFree(
  164. IntPtr lpAddress,
  165. UIntPtr dwSize,
  166. uint dwFreeType);
  167. [DllImport("Kernel32.dll", SetLastError = false)]
  168. internal static extern IntPtr HeapAlloc(IntPtr hHeap, uint dwFlags, UIntPtr dwBytes);
  169. [DllImport("Kernel32.dll", SetLastError = true)]
  170. [return: MarshalAs(UnmanagedType.Bool)]
  171. internal static extern bool HeapFree(IntPtr hHeap, uint dwFlags, IntPtr lpMem);
  172. [DllImport("Kernel32.dll", SetLastError = false)]
  173. internal static extern UIntPtr HeapSize(IntPtr hHeap, uint dwFlags, IntPtr lpMem);
  174. [DllImport("Kernel32.dll", SetLastError = true)]
  175. internal static extern IntPtr HeapCreate(
  176. uint flOptions,
  177. [MarshalAs(UnmanagedType.SysUInt)] IntPtr dwInitialSize,
  178. [MarshalAs(UnmanagedType.SysUInt)] IntPtr dwMaximumSize
  179. );
  180. [DllImport("Kernel32.dll", SetLastError = true)]
  181. internal static extern uint HeapDestroy(IntPtr hHeap);
  182. [DllImport("Kernel32.Dll", SetLastError = true)]
  183. internal unsafe static extern uint HeapSetInformation(
  184. IntPtr HeapHandle,
  185. int HeapInformationClass,
  186. void* HeapInformation,
  187. uint HeapInformationLength
  188. );
  189. [DllImport("user32.dll", SetLastError = false)]
  190. [return: MarshalAs(UnmanagedType.Bool)]
  191. internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  192. //
  193. // 以下是参考https://www.fangsi.net/30.html,为了实现hint
  194. //
  195. /// <summary>
  196. ///
  197. /// </summary>
  198. /// <param name="hWnd"></param>
  199. /// <param name="msg"></param>
  200. /// <param name="wParam"></param>
  201. /// <param name="lParam"></param>
  202. /// <returns></returns>
  203. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  204. private static extern Int32 SendMessage(IntPtr hWnd, int msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);
  205. [DllImport("user32.dll")]
  206. private static extern bool GetComboBoxInfo(IntPtr hwnd, ref COMBOBOXINFO pcbi);
  207. [StructLayout(LayoutKind.Sequential)]
  208. private struct COMBOBOXINFO
  209. {
  210. public int cbSize;
  211. public RECT rcItem;
  212. public RECT rcButton;
  213. public IntPtr stateButton;
  214. public IntPtr hwndCombo;
  215. public IntPtr hwndItem;
  216. public IntPtr hwndList;
  217. }
  218. [StructLayout(LayoutKind.Sequential)]
  219. public struct RECT
  220. {
  221. public int left;
  222. public int top;
  223. public int right;
  224. public int bottom;
  225. }
  226. private const int EM_SETCUEBANNER = 0x1501;
  227. public static void SetCueText(Control control, string text)
  228. {
  229. if (control is ComboBox)
  230. {
  231. COMBOBOXINFO info = GetComboBoxInfo(control);
  232. SendMessage(info.hwndItem, EM_SETCUEBANNER, 0, text);
  233. }
  234. else
  235. {
  236. SendMessage(control.Handle, EM_SETCUEBANNER, 0, text);
  237. }
  238. }
  239. private static COMBOBOXINFO GetComboBoxInfo(Control control)
  240. {
  241. COMBOBOXINFO info = new COMBOBOXINFO();
  242. info.cbSize = Marshal.SizeOf(info);
  243. GetComboBoxInfo(control.Handle, ref info);
  244. return info;
  245. }
  246. }
  247. }