GetWindowHWND.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Runtime.InteropServices;
  5. using System.Windows.Forms;
  6. using System.Diagnostics;
  7. namespace US.Browser.IE.BLL
  8. {
  9. /// <summary>
  10. /// 调用c++系统api区域
  11. /// </summary>
  12. public static class WindowHWND
  13. {
  14. [DllImport("urlmon.dll", CharSet = CharSet.Ansi)]
  15. public static extern int UrlMkSetSessionOption(int dwOption, string pBuffer, int dwBufferLength, int dwReserved);
  16. public const int URLMON_OPTION_USERAGENT = 0x10000001;
  17. //实现F12功能的初始化。下一版本再做,初始化时已经屏蔽
  18. [DllImport("SuperF12.dll", EntryPoint = "InitSuperF12")]
  19. public static extern void InitSuperF12();
  20. public const int GWL_EXSTYLE = -20;
  21. public const int WS_EX_TRANSPARENT = 0x00000020;
  22. public const int WS_EX_LAYERED = 0x00080000;
  23. /// <summary>
  24. /// 使窗口有鼠标穿透功能
  25. /// </summary>
  26. private static void CanPenetrate(IntPtr ui)
  27. {
  28. long intExTemp = GetWindowLong(ui, (int)GWL_EXSTYLE);
  29. int oldGWLEx = SetWindowLong(ui, GWL_EXSTYLE, WS_EX_TRANSPARENT | WS_EX_LAYERED);
  30. }
  31. #region 内存处理
  32. [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId", SetLastError = true,
  33. CharSet = CharSet.Unicode, ExactSpelling = true,
  34. CallingConvention = CallingConvention.StdCall)]
  35. public static extern long GetWindowThreadProcessId(long hWnd, long lpdwProcessId);
  36. [DllImport("user32.dll", SetLastError = true)]
  37. public static extern int SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
  38. [DllImport("user32.dll", EntryPoint = "GetWindowLongA", SetLastError = true)]
  39. public static extern long GetWindowLong(IntPtr hwnd, int nIndex);
  40. [DllImport("user32.dll")]
  41. public static extern int SetWindowLong(IntPtr hwnd, int nIndex, int dwNewLong);
  42. public static IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong)
  43. {
  44. if (IntPtr.Size == 4)
  45. {
  46. return SetWindowLongPtr32(hWnd, nIndex, dwNewLong);
  47. }
  48. return SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
  49. }
  50. [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
  51. public static extern IntPtr SetWindowLongPtr32(HandleRef hWnd, int nIndex, int dwNewLong);
  52. [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", CharSet = CharSet.Auto)]
  53. public static extern IntPtr SetWindowLongPtr64(HandleRef hWnd, int nIndex, int dwNewLong);
  54. [DllImport("user32.dll", SetLastError = true)]
  55. public static extern long SetWindowPos(IntPtr hwnd, long hWndInsertAfter, long x, long y, long cx, long cy, long wFlags);
  56. [DllImport("user32.dll", SetLastError = true)]
  57. public static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);
  58. [DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)]
  59. public static extern bool PostMessage(IntPtr hwnd, uint Msg, uint wParam, uint lParam);
  60. /// <summary>
  61. /// 设置线程内存占用大小
  62. /// </summary>
  63. /// <param name="pProcess"></param>
  64. /// <param name="dwMinimumWorkingSetSize"></param>
  65. /// <param name="dwMaximumWorkingSetSize"></param>
  66. /// <returns></returns>
  67. //[DllImport("KERNEL32.DLL", EntryPoint = "SetProcessWorkingSetSize", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
  68. //public static extern bool SetProcessWorkingSetSize(IntPtr pProcess, int dwMinimumWorkingSetSize, int dwMaximumWorkingSetSize);
  69. //[DllImport("KERNEL32.DLL", EntryPoint = "GetCurrentProcess", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
  70. //public static extern IntPtr GetCurrentProcess();
  71. [DllImport("kernel32.dll")]
  72. [return: MarshalAs(UnmanagedType.Bool)]
  73. public static extern bool SetProcessWorkingSetSize(IntPtr process, UIntPtr minimumWorkingSetSize, UIntPtr maximumWorkingSetSize);
  74. /// <summary>
  75. /// 释放内存
  76. /// </summary>
  77. public static void freem()
  78. {
  79. //SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, (UIntPtr)0xFFFFFFFF, (UIntPtr)0xFFFFFFFF);
  80. GC.Collect(GC.MaxGeneration); GC.WaitForPendingFinalizers(); GC.Collect();
  81. }
  82. #endregion
  83. #region 窗体使用
  84. public const int WM_ACTIVATE = 0x006;
  85. public const int WM_ACTIVATEAPP = 0x01C;
  86. public const int WM_NCACTIVATE = 0x086;
  87. public const int WA_INACTIVE = 0;
  88. public const int WM_MOUSEACTIVATE = 0x21;
  89. public const int MA_NOACTIVATE = 3;
  90. /// <summary>
  91. /// 显示窗体的方法
  92. /// </summary>
  93. /// <param name="hWnd"></param>
  94. /// <param name="nCmdShow"></param>
  95. /// <returns></returns>
  96. [DllImport("user32.dll")]
  97. public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  98. [DllImport("user32.dll ", SetLastError = true)]
  99. public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
  100. [DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]
  101. public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
  102. public const int SW_RESTORE = 9;
  103. public const int SW_SHOW = 5;
  104. [DllImport("User32.dll")]
  105. public static extern bool SetForegroundWindow(System.IntPtr hWnd);
  106. [DllImport("User32.dll")]
  107. public static extern bool ShowWindowAsync(System.IntPtr hWnd, int cmdShow);
  108. [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
  109. public static extern bool ShowWindow(HandleRef hWnd, int nCmdShow);
  110. /// <summary>
  111. /// 聚焦窗体使用
  112. /// </summary>
  113. /// <returns></returns>
  114. [DllImport("user32.dll")]
  115. public static extern IntPtr GetActiveWindow();//获得当前活动窗体
  116. [DllImport("user32.dll")]
  117. public static extern IntPtr SetActiveWindow(IntPtr hwnd);//设置活动窗体
  118. /// <summary>
  119. /// 活动窗体闪烁
  120. /// </summary>
  121. /// <param name="hwnd"></param>
  122. /// <param name="bInvert"></param>
  123. /// <returns></returns>
  124. [DllImport("user32")]
  125. public static extern long FlashWindow(IntPtr hwnd, bool bInvert);
  126. /// <summary>
  127. /// 查找窗体
  128. /// </summary>
  129. /// <param name="lpClassName"></param>
  130. /// <param name="lpWindowName"></param>
  131. /// <returns></returns>
  132. [DllImport("user32.dll", EntryPoint = "GetWindow")]//获取窗体句柄,hwnd为源窗口句柄
  133. public static extern int GetWindow(int hwnd, int wCmd);
  134. [DllImport("user32.dll", EntryPoint = "SetParent")]//设置父窗体
  135. public static extern int SetParent(int hWndChild, int hWndNewParent);
  136. /*wCmd指定结果窗口与源窗口的关系,它们建立在下述常数基础上:
  137. GW_CHILD
  138. 寻找源窗口的第一个子窗口
  139. GW_HWNDFIRST
  140. 为一个源子窗口寻找第一个兄弟(同级)窗口,或寻找第一个顶级窗口
  141. GW_HWNDLAST
  142. 为一个源子窗口寻找最后一个兄弟(同级)窗口,或寻找最后一个顶级窗口
  143. GW_HWNDNEXT
  144. 为源窗口寻找下一个兄弟窗口
  145. GW_HWNDPREV
  146. 为源窗口寻找前一个兄弟窗口
  147. GW_OWNER
  148. 寻找窗口的所有者
  149. */
  150. #endregion
  151. #region 坐标使用
  152. [DllImport("user32.dll", EntryPoint = "GetCursorPos")]//获取鼠标坐标
  153. public static extern int GetCursorPos(ref POINTAPI lpPoint);
  154. [StructLayout(LayoutKind.Sequential)]//定义与API相兼容结构体,实际上是一种内存转换
  155. public struct POINTAPI { public int X; public int Y; }
  156. [DllImport("user32.dll", EntryPoint = "WindowFromPoint")]//指定坐标处窗体句柄
  157. public static extern int WindowFromPoint(int xPoint, int yPoint);
  158. [DllImport("User32.dll")]
  159. public static extern IntPtr WindowFromPoint(System.Drawing.Point p);
  160. /// <summary>
  161. /// 获取鼠标的位置
  162. /// </summary>
  163. /// <returns></returns>
  164. public static POINTAPI GetCur()
  165. {
  166. POINTAPI point = new POINTAPI();//必须用与之相兼容的结构体,类也可以
  167. GetCursorPos(ref point);//获取当前鼠标坐标
  168. return point;
  169. }
  170. /// <summary>
  171. /// 根据坐标获取指定窗体的位置
  172. /// </summary>
  173. /// <returns></returns>
  174. public static IntPtr GetHWND()
  175. {
  176. POINTAPI point = new POINTAPI();//必须用与之相兼容的结构体,类也可以
  177. GetCursorPos(ref point);//获取当前鼠标坐标
  178. int hwnd = WindowFromPoint(point.X, point.Y);//获取指定坐标处窗口的句柄
  179. return (IntPtr)hwnd;
  180. }
  181. #endregion
  182. #region 窗体美化
  183. [DllImport("gdi32.dll")]
  184. public static extern int CreateRoundRectRgn(int x1, int y1, int x2, int y2, int x3, int y3);
  185. /// <summary>
  186. /// 圆角
  187. /// </summary>
  188. /// <param name="hwnd"></param>
  189. /// <param name="hRgn"></param>
  190. /// <param name="bRedraw"></param>
  191. /// <returns></returns>
  192. [DllImport("user32.dll")]
  193. public static extern int SetWindowRgn(IntPtr hwnd, int hRgn, Boolean bRedraw);
  194. [DllImport("gdi32.dll", EntryPoint = "DeleteObject", CharSet = CharSet.Ansi)]
  195. public static extern int DeleteObject(int hObject);
  196. const int CS_DropSHADOW = 0x20000;
  197. const int GCL_STYLE = (-26);
  198. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  199. public static extern int SetClassLong(IntPtr hwnd, int nIndex, int dwNewLong);
  200. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  201. public static extern int GetClassLong(IntPtr hwnd, int nIndex);
  202. /// <summary>
  203. /// 添加圆角
  204. /// </summary>
  205. /// <param name="form"></param>
  206. /// <param name="rgnRadius"></param>
  207. public static void SetFormRoundRectRgn(Form form, int rgnRadius)
  208. {
  209. int hRgn = 0;
  210. hRgn = CreateRoundRectRgn(0, 0, form.Width + 1, form.Height + 1, rgnRadius, rgnRadius);
  211. SetWindowRgn(form.Handle, hRgn, true);
  212. DeleteObject(hRgn);
  213. }
  214. /// <summary>
  215. /// 添加阴影
  216. /// </summary>
  217. /// <param name="ui"></param>
  218. public static void SetFormShadow(IntPtr ui)
  219. {
  220. SetClassLong(ui, GCL_STYLE, GetClassLong(ui, GCL_STYLE) | CS_DropSHADOW); //API函数加载,实现窗体边框阴影效果
  221. }
  222. #endregion
  223. #region 动画
  224. /// <summary>
  225. /// 从左到右显示
  226. /// </summary>
  227. public const Int32 AW_HOR_POSITIVE = 0x00000001;
  228. /// <summary>
  229. /// 从右到左显示
  230. /// </summary>
  231. public const Int32 AW_HOR_NEGATIVE = 0x00000002;
  232. /// <summary>
  233. /// 从上到下显示
  234. /// </summary>
  235. public const Int32 AW_VER_POSITIVE = 0x00000004;
  236. /// <summary>
  237. /// 从下到上显示
  238. /// </summary>
  239. public const Int32 AW_VER_NEGATIVE = 0x00000008;
  240. /// <summary>
  241. /// 若使用了AW_HIDE标志,则使窗口向内重叠,即收缩窗口;否则使窗口向外扩展,即展开窗口
  242. /// </summary>
  243. public const Int32 AW_CENTER = 0x00000010;
  244. /// <summary>
  245. /// 隐藏窗口,缺省则显示窗口
  246. /// </summary>
  247. public const Int32 AW_HIDE = 0x00010000;
  248. /// <summary>
  249. /// 激活窗口。在使用了AW_HIDE标志后不能使用这个标志
  250. /// </summary>
  251. public const Int32 AW_ACTIVATE = 0x00020000;
  252. /// <summary>
  253. /// 使用滑动类型。缺省则为滚动动画类型。当使用AW_CENTER标志时,这个标志就被忽略
  254. /// </summary>
  255. public const Int32 AW_SLIDE = 0x00040000;
  256. /// <summary>
  257. /// 透明度从高到低
  258. /// </summary>
  259. public const Int32 AW_BLEND = 0x00080000;
  260. /// <summary>
  261. /// 执行动画
  262. /// </summary>
  263. /// <param name="whnd">控件句柄</param>
  264. /// <param name="dwtime">动画时间</param>
  265. /// <param name="dwflag">动画组合名称</param>
  266. /// <returns>bool值,动画是否成功</returns>
  267. [DllImport("user32")]
  268. public static extern bool AnimateWindow(IntPtr whnd, int dwtime, int dwflag);
  269. #endregion
  270. #region 窗体拖动
  271. //窗体拖动
  272. [DllImport("user32.dll")]
  273. public static extern bool ReleaseCapture();//API函数
  274. [DllImport("user32.dll")]
  275. public static extern bool SendMessage(IntPtr hwdn, int wMsg, int mParam, int lParam);//接受发送来的消息 并处理
  276. public const int WM_SYSCOMMAND = 0x0112;//该变量表示将向Windows发送消息的类型
  277. public const int SC_MOVE = 0xF010;//该变量表示发送消息的附加消息
  278. public const int HTCAPTION = 0x0002;
  279. /// <summary>
  280. /// 拖动
  281. /// </summary>
  282. public static void _Main_Drag(Form UFE)
  283. {
  284. ReleaseCapture();//该函数从当前线程中释放鼠标捕获事件,并恢复鼠标输入处理
  285. SendMessage(UFE.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
  286. }
  287. #endregion
  288. #region 内存控制
  289. public static string GetSystemXX()
  290. {
  291. Process CurrentProcess = Process.GetCurrentProcess(); //制定进程
  292. return (CurrentProcess.WorkingSet64 / 1024 / 1024).ToString() + "M (" + (CurrentProcess.WorkingSet64 / 1024).ToString() + "KB)";
  293. //CurrentProcess.Id.ToString();//PID
  294. //((Double)(CurrentProcess.TotalProcessorTime.TotalMilliseconds - CurrentProcess.UserProcessorTime.TotalMilliseconds)).ToString();//CPU
  295. //(CurrentProcess.WorkingSet64 / 1024 / 1024).ToString() + "M (" + (CurrentProcess.WorkingSet64 / 1024).ToString() + "KB)";//占用内存
  296. // CurrentProcess.Threads.Count.ToString();//线程
  297. }
  298. #region 使用值
  299. public const int GWL_STYLE = (-16);
  300. public const int WS_VISIBLE = 0x10000000;
  301. #endregion
  302. #endregion
  303. #region
  304. //[System.Runtime.InteropServices.DllImport("user32.dll")]
  305. //private const int WM_ACTIVATE = 0x006;
  306. //private const int WM_ACTIVATEAPP = 0x01C;
  307. //private const int WM_NCACTIVATE = 0x086;
  308. //private const int WA_INACTIVE = 0;
  309. //private const int WM_MOUSEACTIVATE = 0x21;
  310. //private const int MA_NOACTIVATE = 3;
  311. //protected override void WndProc(ref Message m)
  312. //{
  313. // if (m.Msg == WM_MOUSEACTIVATE)
  314. // {
  315. // m.Result = new IntPtr(MA_NOACTIVATE);
  316. // return;
  317. // }
  318. // else if (m.Msg == WM_NCACTIVATE)
  319. // {
  320. // if (((int)m.WParam & 0xFFFF) != WA_INACTIVE)
  321. // {
  322. // if (m.LParam != IntPtr.Zero)
  323. // {
  324. // SetActiveWindow(m.LParam);
  325. // }
  326. // else
  327. // {
  328. // SetActiveWindow(IntPtr.Zero);
  329. // }
  330. // }
  331. // }
  332. // base.WndProc(ref m);
  333. //}
  334. #endregion
  335. }
  336. }