ToolWinApi.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Runtime.InteropServices;
  6. using System.Security;
  7. using System.Text;
  8. using System.Windows;
  9. using System.Windows.Interop;
  10. namespace Ips.Library.DxpLib
  11. {
  12. #pragma warning disable CS0649 // 从未对字段“ToolWinApi.Import.WINDOWPOS.hwnd”赋值,字段将一直保持其默认值
  13. public static class ToolWinApi
  14. {
  15. public class WinProcHook
  16. {
  17. public event Action<byte[]> OnDataReceived;
  18. public event Action<ToolWinApi.WindowPosition> OnWindowPosChanged;
  19. public bool ProcessWmCopyData
  20. {
  21. get;
  22. set;
  23. }
  24. public bool ProcessWmWindowPosChanged
  25. {
  26. get;
  27. set;
  28. }
  29. [SecuritySafeCritical]
  30. public static ToolWinApi.WinProcHook Get(IntPtr hwnd)
  31. {
  32. ToolWinApi.WinProcHook winProcHook;
  33. if (!ToolWinApi.winProcs.TryGetValue(hwnd, out winProcHook))
  34. {
  35. winProcHook = new ToolWinApi.WinProcHook(hwnd);
  36. ToolWinApi.winProcs.Add(hwnd, winProcHook);
  37. }
  38. return winProcHook;
  39. }
  40. private WinProcHook(IntPtr hwnd)
  41. {
  42. HwndSource hwndSource = HwndSource.FromHwnd(hwnd);
  43. hwndSource.AddHook(new HwndSourceHook(this.HookProc));
  44. }
  45. [SecuritySafeCritical]
  46. private IntPtr HookProc(IntPtr handle, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
  47. {
  48. if (this.ProcessWmWindowPosChanged && msg == 71)
  49. {
  50. ToolWinApi.Import.WINDOWPOS wINDOWPOS = (ToolWinApi.Import.WINDOWPOS)Marshal.PtrToStructure(lParam, typeof(ToolWinApi.Import.WINDOWPOS));
  51. ToolWinApi.WindowPosition windowPosition = new ToolWinApi.WindowPosition();
  52. windowPosition.Hwnd = wINDOWPOS.hwnd;
  53. windowPosition.HwndInsertAfter = wINDOWPOS.hwndInsertAfter;
  54. windowPosition.Left = wINDOWPOS.x;
  55. windowPosition.Top = wINDOWPOS.y;
  56. windowPosition.Width = wINDOWPOS.cx;
  57. windowPosition.Height = wINDOWPOS.cy;
  58. windowPosition.Flags = (ToolWinApi.SetWindowPosFlags)wINDOWPOS.flags;
  59. if (this.OnWindowPosChanged != null)
  60. {
  61. this.OnWindowPosChanged(windowPosition);
  62. }
  63. }
  64. if (this.ProcessWmCopyData && msg == 74)
  65. {
  66. ToolWinApi.Import.COPYDATASTRUCT cOPYDATASTRUCT = (ToolWinApi.Import.COPYDATASTRUCT)Marshal.PtrToStructure(lParam, typeof(ToolWinApi.Import.COPYDATASTRUCT));
  67. byte[] array = new byte[1];
  68. int num = cOPYDATASTRUCT.cbData / Marshal.SizeOf(array[0]);
  69. array = new byte[num];
  70. Marshal.Copy(cOPYDATASTRUCT.lpData, array, 0, array.Length);
  71. if (this.OnDataReceived != null)
  72. {
  73. this.OnDataReceived(array);
  74. }
  75. handled = true;
  76. }
  77. return IntPtr.Zero;
  78. }
  79. }
  80. public class WindowPosition
  81. {
  82. public IntPtr Hwnd
  83. {
  84. get;
  85. set;
  86. }
  87. public IntPtr HwndInsertAfter
  88. {
  89. get;
  90. set;
  91. }
  92. public int Left
  93. {
  94. get;
  95. set;
  96. }
  97. public int Top
  98. {
  99. get;
  100. set;
  101. }
  102. public int Width
  103. {
  104. get;
  105. set;
  106. }
  107. public int Height
  108. {
  109. get;
  110. set;
  111. }
  112. public ToolWinApi.SetWindowPosFlags Flags
  113. {
  114. get;
  115. set;
  116. }
  117. }
  118. [Flags]
  119. public enum SetWindowPosFlags
  120. {
  121. NoSize = 1,
  122. NoMove = 2,
  123. NoZOrder = 4,
  124. NoRedraw = 8,
  125. NoActivate = 16,
  126. DrawFrame = 32,
  127. FrameChanged = 32,
  128. ShowWindow = 64,
  129. HideWindow = 128,
  130. NoCopyBits = 256,
  131. NoOwnerZOrder = 512,
  132. NoReposition = 512,
  133. NoSendChanging = 1024,
  134. DeferErase = 8192,
  135. AsyncWindowPos = 16384
  136. }
  137. public enum SpecialFolderType
  138. {
  139. CommonDocuments = 46
  140. }
  141. private static class Import
  142. {
  143. public enum SpecialFolderType
  144. {
  145. AdministrativeTools = 48,
  146. CommonAdministrativeTools = 47,
  147. ApplicationData = 26,
  148. CommonAppData = 35,
  149. CommonDocuments = 46,
  150. Cookies = 33,
  151. CreateFlag = 32768,
  152. History = 34,
  153. InternetCache = 32,
  154. LocalApplicationData = 28,
  155. MyPictures = 39,
  156. Personal = 5,
  157. ProgramFiles = 38,
  158. CommonProgramFiles = 43,
  159. System = 37,
  160. Windows = 36,
  161. Fonts = 20
  162. }
  163. public enum RFlags
  164. {
  165. Any = 65535,
  166. RegNone = 1,
  167. Noexpand = 268435456,
  168. RegBinary = 8,
  169. Dword = 24,
  170. RegDword = 16,
  171. Qword = 72,
  172. RegQword = 64,
  173. RegSz = 2,
  174. RegMultiSz = 32,
  175. RegExpandSz = 4,
  176. RrfZeroonfailure = 536870912
  177. }
  178. public enum RType
  179. {
  180. RegNone,
  181. RegSz,
  182. RegExpandSz,
  183. RegMultiSz = 7,
  184. RegBinary = 3,
  185. RegDword,
  186. RegQword = 11,
  187. RegQwordLittleEndian = 11,
  188. RegDwordLittleEndian = 4,
  189. RegDwordBigEndian,
  190. RegLink,
  191. RegResourceList = 8,
  192. RegFullResourceDescriptor,
  193. RegResourceRequirementsList
  194. }
  195. public enum HookType
  196. {
  197. WH_JOURNALRECORD,
  198. WH_JOURNALPLAYBACK,
  199. WH_KEYBOARD,
  200. WH_GETMESSAGE,
  201. WH_CALLWNDPROC,
  202. WH_CBT,
  203. WH_SYSMSGFILTER,
  204. WH_MOUSE,
  205. WH_HARDWARE,
  206. WH_DEBUG,
  207. WH_SHELL,
  208. WH_FOREGROUNDIDLE,
  209. WH_CALLWNDPROCRET,
  210. WH_KEYBOARD_LL,
  211. WH_MOUSE_LL
  212. }
  213. public struct WINDOWPLACEMENT
  214. {
  215. public int length;
  216. public int flags;
  217. public ToolWinApi.Import.ShowWindowCommands showCmd;
  218. public System.Drawing.Point ptMinPosition;
  219. public System.Drawing.Point ptMaxPosition;
  220. public Rectangle rcNormalPosition;
  221. }
  222. public enum ShowWindowCommands
  223. {
  224. Hide,
  225. Normal,
  226. ShowMinimized,
  227. ShowMaximized,
  228. ShowNoActivate,
  229. Show,
  230. Minimize,
  231. ShowMinNoActive,
  232. ShowNA,
  233. Restore,
  234. ShowDefault,
  235. ForceMinimize
  236. }
  237. public enum GetWindowCmd : uint
  238. {
  239. GW_HWNDFIRST,
  240. GW_HWNDLAST,
  241. GW_HWNDNEXT,
  242. GW_HWNDPREV,
  243. GW_OWNER,
  244. GW_CHILD,
  245. GW_ENABLEDPOPUP
  246. }
  247. public enum SetWindowPosFlags : uint
  248. {
  249. NOSIZE = 1u,
  250. NOMOVE,
  251. NOZORDER = 4u,
  252. NOREDRAW = 8u,
  253. NOACTIVATE = 16u,
  254. DRAWFRAME = 32u,
  255. FRAMECHANGED = 32u,
  256. SHOWWINDOW = 64u,
  257. HIDEWINDOW = 128u,
  258. NOCOPYBITS = 256u,
  259. NOOWNERZORDER = 512u,
  260. NOREPOSITION = 512u,
  261. NOSENDCHANGING = 1024u,
  262. DEFERERASE = 8192u,
  263. ASYNCWINDOWPOS = 16384u
  264. }
  265. public struct WINDOWPOS
  266. {
  267. public IntPtr hwnd;
  268. public IntPtr hwndInsertAfter;
  269. public int x;
  270. public int y;
  271. public int cx;
  272. public int cy;
  273. public int flags;
  274. }
  275. public struct COPYDATASTRUCT
  276. {
  277. public IntPtr dwData;
  278. public int cbData;
  279. public IntPtr lpData;
  280. }
  281. public enum WM : uint
  282. {
  283. WM_COPYDATA = 74u,
  284. WM_WINDOWPOSCHANGED = 71u,
  285. WM_WINDOWPOSCHANGING = 70u
  286. }
  287. public enum GWL
  288. {
  289. GWL_WNDPROC = -4,
  290. GWL_HINSTANCE = -6,
  291. GWL_HWNDPARENT = -8,
  292. GWL_STYLE = -16,
  293. GWL_EXSTYLE = -20,
  294. GWL_USERDATA = -21,
  295. GWL_ID = -12
  296. }
  297. [Flags]
  298. public enum WS : uint
  299. {
  300. WS_OVERLAPPED = 0u,
  301. WS_POPUP = 2147483648u,
  302. WS_CHILD = 1073741824u,
  303. WS_MINIMIZE = 536870912u,
  304. WS_VISIBLE = 268435456u,
  305. WS_DISABLED = 134217728u,
  306. WS_CLIPSIBLINGS = 67108864u,
  307. WS_CLIPCHILDREN = 33554432u,
  308. WS_MAXIMIZE = 16777216u,
  309. WS_BORDER = 8388608u,
  310. WS_DLGFRAME = 4194304u,
  311. WS_VSCROLL = 2097152u,
  312. WS_HSCROLL = 1048576u,
  313. WS_SYSMENU = 524288u,
  314. WS_THICKFRAME = 262144u,
  315. WS_GROUP = 131072u,
  316. WS_TABSTOP = 65536u,
  317. WS_MINIMIZEBOX = 131072u,
  318. WS_MAXIMIZEBOX = 65536u,
  319. WS_CAPTION = 12582912u,
  320. WS_TILED = 0u,
  321. WS_ICONIC = 536870912u,
  322. WS_SIZEBOX = 262144u,
  323. WS_TILEDWINDOW = 13565952u,
  324. WS_OVERLAPPEDWINDOW = 13565952u,
  325. WS_POPUPWINDOW = 2156396544u,
  326. WS_CHILDWINDOW = 1073741824u,
  327. WS_EX_DLGMODALFRAME = 1u,
  328. WS_EX_NOPARENTNOTIFY = 4u,
  329. WS_EX_TOPMOST = 8u,
  330. WS_EX_ACCEPTFILES = 16u,
  331. WS_EX_TRANSPARENT = 32u,
  332. WS_EX_MDICHILD = 64u,
  333. WS_EX_TOOLWINDOW = 128u,
  334. WS_EX_WINDOWEDGE = 256u,
  335. WS_EX_CLIENTEDGE = 512u,
  336. WS_EX_CONTEXTHELP = 1024u,
  337. WS_EX_RIGHT = 4096u,
  338. WS_EX_LEFT = 0u,
  339. WS_EX_RTLREADING = 8192u,
  340. WS_EX_LTRREADING = 0u,
  341. WS_EX_LEFTSCROLLBAR = 16384u,
  342. WS_EX_RIGHTSCROLLBAR = 0u,
  343. WS_EX_CONTROLPARENT = 65536u,
  344. WS_EX_STATICEDGE = 131072u,
  345. WS_EX_APPWINDOW = 262144u,
  346. WS_EX_OVERLAPPEDWINDOW = 768u,
  347. WS_EX_PALETTEWINDOW = 392u,
  348. WS_EX_LAYERED = 524288u,
  349. WS_EX_NOINHERITLAYOUT = 1048576u,
  350. WS_EX_LAYOUTRTL = 4194304u,
  351. WS_EX_COMPOSITED = 33554432u,
  352. WS_EX_NOACTIVATE = 134217728u
  353. }
  354. public const int MAX_PATH = 260;
  355. public const int KEY_QUERY_VALUE = 1;
  356. public const int KEY_SET_VALUE = 2;
  357. public const int KEY_CREATE_SUB_KEY = 4;
  358. public const int KEY_ENUMERATE_SUB_KEYS = 8;
  359. public const int KEY_NOTIFY = 16;
  360. public const int KEY_CREATE_LINK = 32;
  361. public const int KEY_WOW64_32KEY = 512;
  362. public const int KEY_WOW64_64KEY = 256;
  363. public const int KEY_WOW64_RES = 768;
  364. public const int KEY_READ = 131097;
  365. public static UIntPtr HKEY_LOCAL_MACHINE = new UIntPtr(2147483650u);
  366. public static UIntPtr HKEY_CURRENT_USER = new UIntPtr(2147483649u);
  367. public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
  368. public static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
  369. public static readonly IntPtr HWND_TOP = new IntPtr(0);
  370. public static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
  371. [DllImport("shell32.dll")]
  372. public static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken, uint dwFlags, [Out] StringBuilder pszPath);
  373. [DllImport("advapi32.dll", CharSet = CharSet.Unicode, EntryPoint = "RegQueryValueExW", SetLastError = true)]
  374. public static extern uint RegQueryValueEx(IntPtr hKey, string lpValueName, int lpReserved, ref ToolWinApi.Import.RType lpType, byte[] pvData, ref uint pcbData);
  375. [DllImport("advapi32.dll", SetLastError = true)]
  376. public static extern int RegCloseKey(IntPtr hKey);
  377. [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
  378. public static extern int RegOpenKeyEx(UIntPtr hKey, string subKey, int ulOptions, int samDesired, out IntPtr hkResult);
  379. [DllImport("user32.dll")]
  380. [return: MarshalAs(UnmanagedType.Bool)]
  381. public static extern bool ShowWindow(IntPtr hWnd, ToolWinApi.Import.ShowWindowCommands nCmdShow);
  382. [DllImport("user32.dll", SetLastError = true)]
  383. [return: MarshalAs(UnmanagedType.Bool)]
  384. public static extern bool SetWindowPlacement(IntPtr hWnd, [In] ref ToolWinApi.Import.WINDOWPLACEMENT lpwndpl);
  385. [DllImport("user32.dll", SetLastError = true)]
  386. public static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr onlyZero);
  387. [DllImport("user32.dll", SetLastError = true)]
  388. [return: MarshalAs(UnmanagedType.Bool)]
  389. public static extern bool UnhookWindowsHookEx(IntPtr hhk);
  390. [DllImport("user32.dll", SetLastError = true)]
  391. public static extern IntPtr SetWindowsHookEx(ToolWinApi.Import.HookType hookType, IntPtr lpfn, IntPtr hMod, uint dwThreadId);
  392. [DllImport("kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
  393. public static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
  394. [DllImport("kernel32.dll", SetLastError = true)]
  395. [return: MarshalAs(UnmanagedType.Bool)]
  396. public static extern bool FreeLibrary(IntPtr hModule);
  397. [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  398. public static extern IntPtr LoadLibrary(string lpFileName);
  399. [DllImport("user32.dll")]
  400. [return: MarshalAs(UnmanagedType.Bool)]
  401. public static extern bool GetWindowPlacement(IntPtr hWnd, ref ToolWinApi.Import.WINDOWPLACEMENT lpwndpl);
  402. [DllImport("user32.dll")]
  403. [return: MarshalAs(UnmanagedType.Bool)]
  404. public static extern bool SetForegroundWindow(IntPtr hWnd);
  405. [DllImport("user32.dll")]
  406. public static extern IntPtr SetFocus(IntPtr hWnd);
  407. [DllImport("user32.dll")]
  408. [return: MarshalAs(UnmanagedType.Bool)]
  409. public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, ToolWinApi.Import.SetWindowPosFlags uFlags);
  410. [DllImport("user32.dll", SetLastError = true)]
  411. public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
  412. [DllImport("user32.dll", SetLastError = true)]
  413. public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
  414. [DllImport("user32.dll", SetLastError = true)]
  415. public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
  416. [DllImport("user32.dll", SetLastError = true)]
  417. public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
  418. [DllImport("user32.dll", SetLastError = true)]
  419. public static extern IntPtr GetWindow(IntPtr hWnd, ToolWinApi.Import.GetWindowCmd uCmd);
  420. [DllImport("user32.dll")]
  421. public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
  422. [DllImport("user32.dll")]
  423. public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
  424. }
  425. private static Dictionary<IntPtr, ToolWinApi.WinProcHook> winProcs = new Dictionary<IntPtr, ToolWinApi.WinProcHook>();
  426. public static readonly IntPtr HwndTopmost = ToolWinApi.Import.HWND_TOPMOST;
  427. public static readonly IntPtr HwndNoTopmost = ToolWinApi.Import.HWND_NOTOPMOST;
  428. public static readonly IntPtr HwndTop = ToolWinApi.Import.HWND_TOP;
  429. public static readonly IntPtr HwndBottom = ToolWinApi.Import.HWND_BOTTOM;
  430. [SecuritySafeCritical]
  431. public static IntPtr LoadLibrary(string filePath)
  432. {
  433. return ToolWinApi.Import.LoadLibrary(filePath);
  434. }
  435. [SecuritySafeCritical]
  436. public static void FreeLibrary(IntPtr hmodule)
  437. {
  438. ToolWinApi.Import.FreeLibrary(hmodule);
  439. }
  440. [SecuritySafeCritical]
  441. public static IntPtr GetProcAddress(IntPtr hmodule, string procName)
  442. {
  443. return ToolWinApi.Import.GetProcAddress(hmodule, procName);
  444. }
  445. [SecuritySafeCritical]
  446. public static void MakeWindowChild(IntPtr hwnd)
  447. {
  448. ToolWinApi.Import.WS wS = (ToolWinApi.Import.WS)ToolWinApi.Import.GetWindowLong(hwnd, -16);
  449. wS |= ToolWinApi.Import.WS.WS_CHILD;
  450. wS &= ~ToolWinApi.Import.WS.WS_POPUP;
  451. ToolWinApi.Import.SetWindowLong(hwnd, -16, (int)wS);
  452. }
  453. [SecuritySafeCritical]
  454. public static void SetWindowParent(IntPtr hwnd, IntPtr newParentHwnd)
  455. {
  456. ToolWinApi.Import.SetParent(hwnd, newParentHwnd);
  457. }
  458. [SecuritySafeCritical]
  459. public static void MoveWindow(IntPtr hwnd, int x, int y, int width, int height, bool repaint)
  460. {
  461. ToolWinApi.Import.MoveWindow(hwnd, x, y, width, height, repaint);
  462. }
  463. [SecuritySafeCritical]
  464. public static IntPtr GetPrevWindow(IntPtr hwnd)
  465. {
  466. return ToolWinApi.Import.GetWindow(hwnd, ToolWinApi.Import.GetWindowCmd.GW_HWNDPREV);
  467. }
  468. [SecuritySafeCritical]
  469. public static IntPtr GetNextWindow(IntPtr hwnd)
  470. {
  471. return ToolWinApi.Import.GetWindow(hwnd, ToolWinApi.Import.GetWindowCmd.GW_HWNDNEXT);
  472. }
  473. [SecuritySafeCritical]
  474. public static void SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter, int x, int y, int width, int height, ToolWinApi.SetWindowPosFlags flags)
  475. {
  476. ToolWinApi.Import.SetWindowPos(hwnd, hwndInsertAfter, x, y, width, height, (ToolWinApi.Import.SetWindowPosFlags)flags);
  477. }
  478. [SecuritySafeCritical]
  479. public static IntPtr SetFocus(IntPtr hwnd)
  480. {
  481. return ToolWinApi.Import.SetFocus(hwnd);
  482. }
  483. [SecuritySafeCritical]
  484. public static bool SetForegroundWindow(IntPtr hwnd)
  485. {
  486. return ToolWinApi.Import.SetForegroundWindow(hwnd);
  487. }
  488. [SecuritySafeCritical]
  489. public static bool RestoreWindowAsync(IntPtr hwnd)
  490. {
  491. return ToolWinApi.Import.ShowWindowAsync(hwnd, 9);
  492. }
  493. [SecuritySafeCritical]
  494. public static void SendData(IntPtr hwnd, byte[] data)
  495. {
  496. int cbData;
  497. IntPtr intPtr = ToolWinApi.IntPtrAlloc(data, out cbData);
  498. IntPtr intPtr2 = ToolWinApi.IntPtrAlloc<ToolWinApi.Import.COPYDATASTRUCT>(new ToolWinApi.Import.COPYDATASTRUCT
  499. {
  500. dwData = (IntPtr)49329,
  501. lpData = intPtr,
  502. cbData = cbData
  503. });
  504. ToolWinApi.Import.SendMessage(hwnd, 74, IntPtr.Zero, intPtr2);
  505. ToolWinApi.IntPtrFree(intPtr2);
  506. intPtr2 = IntPtr.Zero;
  507. ToolWinApi.IntPtrFree(intPtr);
  508. intPtr = IntPtr.Zero;
  509. }
  510. [SecuritySafeCritical]
  511. public static IntPtr IntPtrAlloc<T>(T param)
  512. {
  513. IntPtr intPtr = Marshal.AllocHGlobal(Marshal.SizeOf(param));
  514. Marshal.StructureToPtr(param, intPtr, false);
  515. return intPtr;
  516. }
  517. [SecuritySafeCritical]
  518. public static IntPtr IntPtrAlloc(byte[] data, out int size)
  519. {
  520. size = Marshal.SizeOf(data[0]) * data.Length;
  521. IntPtr intPtr = Marshal.AllocHGlobal(size);
  522. Marshal.Copy(data, 0, intPtr, data.Length);
  523. return intPtr;
  524. }
  525. [SecuritySafeCritical]
  526. public static void IntPtrFree(IntPtr preAllocated)
  527. {
  528. if (IntPtr.Zero == preAllocated)
  529. {
  530. return;
  531. }
  532. Marshal.FreeHGlobal(preAllocated);
  533. preAllocated = IntPtr.Zero;
  534. }
  535. [SecuritySafeCritical]
  536. public static IntPtr SetWndProcHook(IntPtr hwnd, IntPtr module, IntPtr hookPtr)
  537. {
  538. return ToolWinApi.Import.SetWindowsHookEx(ToolWinApi.Import.HookType.WH_CALLWNDPROC, hookPtr, module, (uint)ToolWinApi.GetWindowThreadProcessId(hwnd));
  539. }
  540. [SecuritySafeCritical]
  541. public static bool UnhookWindowsHookEx(IntPtr hhook)
  542. {
  543. return ToolWinApi.Import.UnhookWindowsHookEx(hhook);
  544. }
  545. [SecuritySafeCritical]
  546. public static int GetWindowThreadProcessId(IntPtr hwnd)
  547. {
  548. return (int)ToolWinApi.Import.GetWindowThreadProcessId(hwnd, IntPtr.Zero);
  549. }
  550. [SecuritySafeCritical]
  551. public static Rectangle? GetWindowNormalPlacement(IntPtr hwnd)
  552. {
  553. ToolWinApi.Import.WINDOWPLACEMENT wINDOWPLACEMENT = default(ToolWinApi.Import.WINDOWPLACEMENT);
  554. wINDOWPLACEMENT.length = Marshal.SizeOf(wINDOWPLACEMENT);
  555. if (!ToolWinApi.Import.GetWindowPlacement(hwnd, ref wINDOWPLACEMENT))
  556. {
  557. return null;
  558. }
  559. return new Rectangle?(wINDOWPLACEMENT.rcNormalPosition);
  560. }
  561. [SecuritySafeCritical]
  562. public static bool CopyWindowPlacement(IntPtr source, IntPtr dest)
  563. {
  564. ToolWinApi.Import.WINDOWPLACEMENT wINDOWPLACEMENT = default(ToolWinApi.Import.WINDOWPLACEMENT);
  565. wINDOWPLACEMENT.length = Marshal.SizeOf(wINDOWPLACEMENT);
  566. return ToolWinApi.Import.GetWindowPlacement(source, ref wINDOWPLACEMENT) && ToolWinApi.Import.SetWindowPlacement(dest, ref wINDOWPLACEMENT);
  567. }
  568. [SecuritySafeCritical]
  569. public static bool SetWindowState(IntPtr hwnd, WindowState state)
  570. {
  571. ToolWinApi.Import.ShowWindowCommands nCmdShow;
  572. switch (state)
  573. {
  574. case WindowState.Minimized:
  575. nCmdShow = ToolWinApi.Import.ShowWindowCommands.ShowMinimized;
  576. break;
  577. case WindowState.Maximized:
  578. nCmdShow = ToolWinApi.Import.ShowWindowCommands.ShowMaximized;
  579. break;
  580. default:
  581. nCmdShow = ToolWinApi.Import.ShowWindowCommands.Normal;
  582. break;
  583. }
  584. return ToolWinApi.Import.ShowWindow(hwnd, nCmdShow);
  585. }
  586. [SecuritySafeCritical]
  587. public static int SetWindowNoMaximize(IntPtr hwnd)
  588. {
  589. ToolWinApi.Import.WS wS = (ToolWinApi.Import.WS)ToolWinApi.Import.GetWindowLong(hwnd, -16);
  590. wS &= ~ToolWinApi.Import.WS.WS_TABSTOP;
  591. return ToolWinApi.Import.SetWindowLong(hwnd, -16, (int)wS);
  592. }
  593. [SecuritySafeCritical]
  594. public static void MakeWindowTransparent(IntPtr hwnd)
  595. {
  596. ToolWinApi.Import.WS wS = (ToolWinApi.Import.WS)ToolWinApi.Import.GetWindowLong(hwnd, -20);
  597. wS |= (ToolWinApi.Import.WS.WS_SYSMENU | ToolWinApi.Import.WS.WS_EX_TRANSPARENT);
  598. ToolWinApi.Import.SetWindowLong(hwnd, -20, (int)wS);
  599. }
  600. [SecuritySafeCritical]
  601. public static string GetFolderPath(ToolWinApi.SpecialFolderType folderType)
  602. {
  603. StringBuilder stringBuilder = new StringBuilder(260);
  604. int num = ToolWinApi.Import.SHGetFolderPath(IntPtr.Zero, (int)folderType, IntPtr.Zero, 0u, stringBuilder);
  605. if (num != 0)
  606. {
  607. throw new Win32Exception(num);
  608. }
  609. return stringBuilder.ToString();
  610. }
  611. }
  612. #pragma warning restore CS0649 // 从未对字段“ToolWinApi.Import.WINDOWPOS.hwnd”赋值,字段将一直保持其默认值
  613. }