1
0

ShowWindowFollowMouse.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. using GeekDesk.Constant;
  2. using System;
  3. using System.Drawing;
  4. using System.Runtime.InteropServices;
  5. using System.Windows;
  6. using System.Windows.Forms;
  7. using System.Windows.Input;
  8. using static GeekDesk.Util.ShowWindowFollowMouse;
  9. namespace GeekDesk.Util
  10. {
  11. public class ShowWindowFollowMouse
  12. {
  13. public enum MousePosition
  14. {
  15. CENTER = 1,
  16. LEFT_TOP = 2,
  17. LEFT_BOTTOM = 3,
  18. RIGHT_TOP = 4,
  19. RIGHT_BOTTOM = 5,
  20. LEFT_CENTER = 6,
  21. RIGHT_CENTER = 7
  22. }
  23. public static void FollowMouse(Window window)
  24. {
  25. // Get the mouse position
  26. var mousePosition = System.Windows.Forms.Control.MousePosition;
  27. // Get the window size
  28. var windowWidth = window.Width;
  29. var windowHeight = window.Height;
  30. Console.WriteLine("windowWidth + windowHeight:" + windowWidth + "+" + windowHeight);
  31. // Get the screen where the mouse is located
  32. var screen = System.Windows.Forms.Screen.FromPoint(new System.Drawing.Point(mousePosition.X, mousePosition.Y));
  33. var workingArea = screen.WorkingArea;
  34. // Get the DPI scaling factor for the screen
  35. //float dpiX, dpiY;
  36. //using (var graphics = System.Drawing.Graphics.FromHwnd(IntPtr.Zero))
  37. //{
  38. // dpiX = graphics.DpiX / 96f; // 96 is the default DPI
  39. // dpiY = graphics.DpiY / 96f; // 96 is the default DPI
  40. //}
  41. float dpiX = GetDpi( true);
  42. float dpiY = GetDpi(false);
  43. // Convert mouse position to logical pixels based on DPI
  44. double mouseX = mousePosition.X / dpiX;
  45. double mouseY = mousePosition.Y / dpiY;
  46. // Calculate target position to center the window on the mouse
  47. double targetLeft = mouseX - windowWidth / 2;
  48. double targetTop = mouseY - windowHeight / 2;
  49. // Ensure the window does not exceed the screen boundaries
  50. if (targetLeft < workingArea.Left / dpiX)
  51. targetLeft = workingArea.Left / dpiX;
  52. if (targetLeft + windowWidth / dpiX > workingArea.Right / dpiX)
  53. targetLeft = workingArea.Right / dpiX - windowWidth / dpiX;
  54. if (targetTop < workingArea.Top / dpiY)
  55. targetTop = workingArea.Top / dpiY;
  56. if (targetTop + windowHeight / dpiY > workingArea.Bottom / dpiY)
  57. targetTop = workingArea.Bottom / dpiY - windowHeight / dpiY;
  58. // Update window position
  59. window.Left = targetLeft * dpiX;
  60. window.Top = targetTop * dpiY;
  61. }
  62. private static float GetDpi(bool isX)
  63. {
  64. IntPtr hdc = WindowUtil.GetDC(IntPtr.Zero);
  65. int dpi = isX ? WindowUtil.GetDeviceCaps(hdc, LOGPIXELSX) : WindowUtil.GetDeviceCaps(hdc, LOGPIXELSY);
  66. WindowUtil.ReleaseDC(IntPtr.Zero, hdc);
  67. return dpi / 96f;
  68. }
  69. private static IntPtr GetScreenHandleFromMouse()
  70. {
  71. // Get the mouse position
  72. var mousePosition = System.Windows.Forms.Control.MousePosition;
  73. // Convert mouse position to a POINT structure
  74. System.Drawing.Point point = new System.Drawing.Point(mousePosition.X, mousePosition.Y);
  75. // Get the window handle from the point
  76. IntPtr windowHandle = WindowUtil.WindowFromPoint(point);
  77. return windowHandle;
  78. }
  79. // Constants for DPI
  80. private const int HORZRES = 8;
  81. private const int VERTRES = 10;
  82. private const int LOGPIXELSX = 88;
  83. private const int LOGPIXELSY = 90;
  84. /// <summary>
  85. /// 随鼠标位置显示面板 后面三个参数暂时没有使用
  86. /// </summary>
  87. public static void Show(Window window, MousePosition position, double widthOffset = 0, double heightOffset = 0)
  88. {
  89. //获取鼠标位置
  90. System.Drawing.Point p = System.Windows.Forms.Cursor.Position;
  91. // 获取鼠标所在的屏幕
  92. Screen currentScreen = Screen.FromPoint(p);
  93. float dpiX = GetDpi(true);
  94. float dpiY = GetDpi(false);
  95. p.X = (int)(p.X / dpiX);
  96. p.Y = (int)(p.Y / dpiY);
  97. //工作区宽度
  98. double screenWidth = currentScreen.WorkingArea.Width / dpiX;
  99. //工作区高度
  100. double screenHeight = currentScreen.WorkingArea.Height / dpiY;
  101. double screenX = currentScreen.WorkingArea.X / dpiX;
  102. double screenY = currentScreen.WorkingArea.Y / dpiY;
  103. //判断是否超出最左边缘
  104. if (p.X - window.Width / 2 < screenX)
  105. {
  106. //超出最左边缘
  107. window.Left = screenX - Constants.SHADOW_WIDTH;
  108. } else
  109. {
  110. window.Left = p.X - window.Width / 2 + Constants.SHADOW_WIDTH;
  111. }
  112. //判断是否超出最右边缘
  113. if (p.X + window.Width / 2 > screenWidth + screenX)
  114. {
  115. //超出最右边缘
  116. window.Left = screenWidth + screenX - window.Width + Constants.SHADOW_WIDTH;
  117. }
  118. //判断是否超出最上边缘
  119. if (p.Y - window.Height / 2 < screenY)
  120. {
  121. //超出最上边缘
  122. window.Top = screenY - Constants.SHADOW_WIDTH;
  123. } else
  124. {
  125. window.Top = p.Y - window.Height / 2 + Constants.SHADOW_WIDTH;
  126. }
  127. //判断是否超出最下边缘
  128. if (p.Y + window.Height / 2 > screenHeight + screenY)
  129. {
  130. //超出最下边缘
  131. window.Top = screenHeight + screenY - window.Height + Constants.SHADOW_WIDTH;
  132. }
  133. //// 显示屏幕信息
  134. //Console.WriteLine("鼠标当前所在屏幕的信息:");
  135. //Console.WriteLine($"屏幕设备名称: {currentScreen.DeviceName}");
  136. //Console.WriteLine($"屏幕分辨率: {currentScreen.Bounds.Width}x{currentScreen.Bounds.Height}");
  137. //Console.WriteLine($"屏幕工作区: {currentScreen.WorkingArea}");
  138. //Console.WriteLine($"主屏幕: {currentScreen.Primary}");
  139. //Console.WriteLine(p.X + "=" + p.Y);
  140. //float dpiX = GetDpi(true);
  141. //float dpiY = GetDpi(false);
  142. //p.X = (int)(p.X / dpiX);
  143. //p.Y = (int)(p.Y / dpiY);
  144. //Console.WriteLine(p.X + "=" + p.Y);
  145. //double left = SystemParameters.VirtualScreenLeft;
  146. //double top = SystemParameters.VirtualScreenTop;
  147. //double width = SystemParameters.VirtualScreenWidth;
  148. //double height = SystemParameters.WorkArea.Bottom;
  149. //Console.WriteLine("VirtualScreenTop:" + SystemParameters.VirtualScreenTop);
  150. //Console.WriteLine("VirtualScreenLeft:" + SystemParameters.VirtualScreenLeft);
  151. //Console.WriteLine("VirtualScreenWidth:" + SystemParameters.VirtualScreenWidth);
  152. //Console.WriteLine("VirtualScreenHeight:" + SystemParameters.VirtualScreenHeight);
  153. //Console.WriteLine("WorkArea.Bottom:" + SystemParameters.WorkArea.Bottom);
  154. //Console.WriteLine(" window.Height:" + window.Height);
  155. //double right = width - Math.Abs(left);
  156. //double bottom = height - Math.Abs(top);
  157. //double afterWidth;
  158. //double afterHeight;
  159. //switch (position)
  160. //{
  161. // case MousePosition.LEFT_BOTTOM:
  162. // afterWidth = 0;
  163. // afterHeight = window.Height;
  164. // break;
  165. // case MousePosition.LEFT_TOP:
  166. // afterWidth = 0;
  167. // afterHeight = 0;
  168. // break;
  169. // case MousePosition.LEFT_CENTER:
  170. // afterWidth = 0;
  171. // afterHeight = window.Height / 2;
  172. // break;
  173. // case MousePosition.RIGHT_BOTTOM:
  174. // afterWidth = window.Width;
  175. // afterHeight = window.Height;
  176. // break;
  177. // case MousePosition.RIGHT_TOP:
  178. // afterWidth = window.Width;
  179. // afterHeight = 0;
  180. // break;
  181. // case MousePosition.RIGHT_CENTER:
  182. // afterWidth = window.Width;
  183. // afterHeight = window.Height / 2;
  184. // break;
  185. // default:
  186. // afterWidth = window.Width / 2;
  187. // afterHeight = window.Height / 2;
  188. // break;
  189. //}
  190. //afterWidth += widthOffset;
  191. //afterHeight -= heightOffset;
  192. //if (p.X - afterWidth < left)
  193. //{
  194. // //判断是否在最左边缘
  195. // window.Left = left - Constants.SHADOW_WIDTH;
  196. //}
  197. //else if (p.X + afterWidth > right)
  198. //{
  199. // //判断是否在最右边缘
  200. // window.Left = right - window.Width + Constants.SHADOW_WIDTH;
  201. //}
  202. //else
  203. //{
  204. // window.Left = p.X - afterWidth;
  205. //}
  206. //if (p.Y - afterHeight < top)
  207. //{
  208. // //判断是否在最上边缘
  209. // window.Top = top - Constants.SHADOW_WIDTH;
  210. //}
  211. //else if (p.Y + afterHeight > bottom)
  212. //{
  213. // Console.WriteLine("p.Y:" + p.Y);
  214. // Console.WriteLine("afterHeight:" + afterHeight);
  215. // Console.WriteLine("bottom:" + bottom);
  216. // //判断是否在最下边缘
  217. // window.Top = bottom - window.Height + Constants.SHADOW_WIDTH;
  218. //}
  219. //else
  220. //{
  221. // window.Top = p.Y - afterHeight;
  222. //}
  223. }
  224. }
  225. }