ShowWindowFollowMouse.cs 3.2 KB

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