WindowsTools.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using UnityEngine;
  4. namespace Chiva.Toolkit.Function.NoBorder
  5. {
  6. /// <summary>
  7. /// 系统工具类
  8. /// </summary>
  9. public class WindowsTools
  10. {
  11. /// <summary>
  12. /// 获取应用对应进程句柄
  13. /// </summary>
  14. private static IntPtr ProcessHandle;
  15. #region 方法
  16. /// <summary>
  17. /// 获取顶层窗口的句柄,不搜索子窗口
  18. /// </summary>
  19. /// <param name="lpClassName">类名</param>
  20. /// <param name="lpWindowName">窗口名</param>
  21. /// <returns></returns>
  22. [DllImport("user32.dll")]
  23. private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  24. /// <summary>
  25. /// 设置指定窗口的显示状态
  26. /// </summary>
  27. /// <param name="hwnd"></param>
  28. /// <param name="nCmdShow"></param>
  29. /// <returns></returns>
  30. [DllImport("user32.dll")]
  31. public static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
  32. /// <summary>
  33. /// 获取一个前台窗口的句柄(用户当前工作的窗口)
  34. /// </summary>
  35. /// <returns></returns>
  36. [DllImport("user32.dll")]
  37. private static extern IntPtr GetForegroundWindow();
  38. /// <summary>
  39. /// 用来改变指定窗口的属性
  40. /// </summary>
  41. /// <param name="hwnd">窗口句柄及间接给出的窗口所属的类</param>
  42. /// <param name="nIndex">将设定的大于等于0的偏移值</param>
  43. /// <param name="dwNewLong">替换值</param>
  44. /// <returns></returns>
  45. [DllImport("user32.dll")]
  46. private static extern IntPtr SetWindowLong(IntPtr hwnd, int nIndex, int dwNewLong);
  47. /// <summary>
  48. /// 改变一个子窗口,弹出式窗口或顶层窗口的尺寸
  49. /// </summary>
  50. /// <param name="hWnd"></param>
  51. /// <param name="hWndInsertAfter"></param>
  52. /// <param name="X"></param>
  53. /// <param name="Y"></param>
  54. /// <param name="cx"></param>
  55. /// <param name="cy"></param>
  56. /// <param name="uFlags"></param>
  57. /// <returns></returns>
  58. [DllImport("user32.dll")]
  59. private static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
  60. /// <summary>
  61. /// 释放当前线程窗口的鼠标捕获
  62. /// </summary>
  63. /// <returns></returns>
  64. [DllImport("user32.dll")]
  65. private static extern bool ReleaseCapture();
  66. /// <summary>
  67. /// 将指定的消息发送到一个或多个窗口
  68. /// </summary>
  69. /// <param name="hwnd">指定要接收消息的窗口的句柄</param>
  70. /// <param name="wMsg">指定被发送的消息</param>
  71. /// <param name="wParam">指定附加的消息特定信息</param>
  72. /// <param name="IParam">指定附加的消息特定信息</param>
  73. /// <returns></returns>
  74. [DllImport("user32.dll")]
  75. private static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int IParam);
  76. #endregion
  77. #region 常量
  78. /// <summary>
  79. /// 显示窗口
  80. /// </summary>
  81. public const int SWP_SHOWWINDOW = 0x0040;
  82. /// <summary>
  83. /// 边框样式
  84. /// </summary>
  85. public const int GWL_STYLE = -16;
  86. /// <summary>
  87. /// 创建一个单边框的窗口
  88. /// </summary>
  89. public const int WS_BORDER = 1;
  90. /// <summary>
  91. /// 该窗口是一个弹出窗口
  92. /// </summary>
  93. public const int WS_POPUP = 0x800000;
  94. /// <summary>
  95. /// 激活窗口并将其最小化
  96. /// </summary>
  97. public const int SW_SHOWMINIMIZED = 2;
  98. /// <summary>
  99. /// 激活窗口并将其最大化
  100. /// </summary>
  101. public const int SW_SHOWMAXIMIZED = 3;
  102. /// <summary>
  103. /// 当光标在一个窗口的非客户区同时按下鼠标左键时提交此消息
  104. /// </summary>
  105. private const int WM_NCLBUTTONDOWN = 0xA1;
  106. /// <summary>
  107. /// 当窗口被销毁时发送
  108. /// </summary>
  109. private const int WM_DESTROY = 0x02;
  110. /// <summary>
  111. /// 当用户在光标位于窗口的工作区时释放鼠标左键时发布
  112. /// </summary>
  113. private const int WM_LBUTTONUP = 0x0202;
  114. #endregion
  115. #region 公开
  116. /// <summary>
  117. /// 获取当前窗口句柄
  118. /// </summary>
  119. /// <param name="_productName">产品名称</param>
  120. public static void GetCurrentWindowHandle(string _productName)
  121. {
  122. IntPtr hWnd = FindWindow("UnityWndClass", _productName);
  123. if (hWnd != null)
  124. {
  125. ProcessHandle = hWnd;
  126. }
  127. else
  128. {
  129. ProcessHandle = GetForegroundWindow();
  130. }
  131. }
  132. private static string msg = "";
  133. /// <summary>
  134. /// 最小化窗口
  135. /// </summary>
  136. public static void SetMinWindows()
  137. {
  138. ShowWindow(ProcessHandle, SW_SHOWMINIMIZED);
  139. }
  140. /// <summary>
  141. /// 设置无边框窗口
  142. /// </summary>
  143. /// <param name="_rect"></param>
  144. public static void SetNoBorderWindow(Rect _rect)
  145. {
  146. //修改指定句柄窗口的样式、框体大小
  147. SetWindowLong(ProcessHandle, GWL_STYLE, WS_POPUP);
  148. //设置位置
  149. SetWindowPos(
  150. ProcessHandle,
  151. 0,
  152. (int)_rect.x,
  153. (int)_rect.y,
  154. (int)_rect.width,
  155. (int)_rect.height,
  156. SWP_SHOWWINDOW);
  157. }
  158. /// <summary>
  159. /// 拖动窗口
  160. /// </summary>
  161. public static void DragWindow()
  162. {
  163. ReleaseCapture();
  164. SendMessage(ProcessHandle, WM_NCLBUTTONDOWN, WM_DESTROY, 0);
  165. SendMessage(ProcessHandle, WM_LBUTTONUP, 0, 0);
  166. }
  167. #endregion
  168. }
  169. }