ShowWindowFollowMouse.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using GeekDesk.Constant;
  2. using System;
  3. using System.Windows;
  4. namespace GeekDesk.Util
  5. {
  6. public class ShowWindowFollowMouse
  7. {
  8. public enum MousePosition
  9. {
  10. CENTER = 1,
  11. LEFT_TOP = 2,
  12. LEFT_BOTTOM = 3,
  13. RIGHT_TOP = 4,
  14. RIGHT_BOTTOM = 5,
  15. LEFT_CENTER = 6,
  16. RIGHT_CENTER = 7
  17. }
  18. /// <summary>
  19. /// 随鼠标位置显示面板
  20. /// </summary>
  21. public static void Show(Window window, MousePosition position, double widthOffset = 0, double heightOffset = 0)
  22. {
  23. //获取鼠标位置
  24. System.Windows.Point p = MouseUtil.GetMousePosition();
  25. double left = SystemParameters.VirtualScreenLeft;
  26. double top = SystemParameters.VirtualScreenTop;
  27. double width = SystemParameters.VirtualScreenWidth;
  28. double height = SystemParameters.WorkArea.Height;
  29. double right = width - Math.Abs(left);
  30. double bottom = height - Math.Abs(top);
  31. double afterWidth;
  32. double afterHeight;
  33. switch (position)
  34. {
  35. case MousePosition.LEFT_BOTTOM:
  36. afterWidth = 0;
  37. afterHeight = window.Height;
  38. break;
  39. case MousePosition.LEFT_TOP:
  40. afterWidth = 0;
  41. afterHeight = 0;
  42. break;
  43. case MousePosition.LEFT_CENTER:
  44. afterWidth = 0;
  45. afterHeight = window.Height / 2;
  46. break;
  47. case MousePosition.RIGHT_BOTTOM:
  48. afterWidth = window.Width;
  49. afterHeight = window.Height;
  50. break;
  51. case MousePosition.RIGHT_TOP:
  52. afterWidth = window.Width;
  53. afterHeight = 0;
  54. break;
  55. case MousePosition.RIGHT_CENTER:
  56. afterWidth = window.Width;
  57. afterHeight = window.Height / 2;
  58. break;
  59. default:
  60. afterWidth = window.Width / 2;
  61. afterHeight = window.Height / 2;
  62. break;
  63. }
  64. afterWidth += widthOffset;
  65. afterHeight -= heightOffset;
  66. if (p.X - afterWidth < left)
  67. {
  68. //判断是否在最左边缘
  69. window.Left = left - Constants.SHADOW_WIDTH;
  70. }
  71. else if (p.X + afterWidth > right)
  72. {
  73. //判断是否在最右边缘
  74. window.Left = right - window.Width + Constants.SHADOW_WIDTH;
  75. }
  76. else
  77. {
  78. window.Left = p.X - afterWidth;
  79. }
  80. if (p.Y - afterHeight < top)
  81. {
  82. //判断是否在最上边缘
  83. window.Top = top - Constants.SHADOW_WIDTH;
  84. }
  85. else if (p.Y + afterHeight > bottom)
  86. {
  87. //判断是否在最下边缘
  88. window.Top = bottom - window.Height + Constants.SHADOW_WIDTH;
  89. }
  90. else
  91. {
  92. window.Top = p.Y - afterHeight;
  93. }
  94. }
  95. }
  96. }