MarginHide.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Drawing; //添加引用
  7. using System.Windows.Forms;//添加引用
  8. using MouseEventArgs = System.Windows.Input.MouseEventArgs;
  9. using System.Windows;
  10. using System.Windows.Media.Animation;
  11. using System.Windows.Media;
  12. namespace GeekDesk.Util
  13. {
  14. enum HidePosition
  15. {
  16. TOP = 1,
  17. LEFT = 2,
  18. RIGHT = 3
  19. }
  20. public class MarginHide
  21. {
  22. readonly Window window;//定义使用该方法的窗体
  23. private readonly int hideTime = 150;
  24. private readonly int taskTime = 200;
  25. private bool isHide;
  26. public Timer timer;
  27. //构造函数,传入将要匹配的窗体
  28. public MarginHide(Window window)
  29. {
  30. this.window = window;
  31. }
  32. /// <summary>
  33. /// 窗体是否贴边
  34. /// </summary>
  35. /// <returns></returns>
  36. public bool IsMargin()
  37. {
  38. double screenLeft = SystemParameters.VirtualScreenLeft;
  39. double screenTop = SystemParameters.VirtualScreenTop;
  40. double screenWidth = SystemParameters.VirtualScreenWidth;
  41. double windowWidth = window.Width;
  42. double windowTop = window.Top;
  43. double windowLeft = window.Left;
  44. //窗体是否贴边
  45. return (windowLeft <= screenLeft
  46. || windowTop <= screenTop
  47. || windowLeft + windowWidth + Math.Abs(screenLeft) >= screenWidth);
  48. }
  49. #region 窗体贴边隐藏功能
  50. private void TimerDealy(object o, EventArgs e)
  51. {
  52. if (window.Visibility != Visibility.Visible) return;
  53. double screenLeft = SystemParameters.VirtualScreenLeft;
  54. double screenTop = SystemParameters.VirtualScreenTop;
  55. double screenWidth = SystemParameters.VirtualScreenWidth;
  56. double windowHeight = window.Height;
  57. double windowWidth = window.Width;
  58. double windowTop = window.Top;
  59. double windowLeft = window.Left;
  60. //获取鼠标位置
  61. System.Windows.Point p = MouseUtil.GetMousePosition();
  62. double mouseX = p.X;
  63. double mouseY = p.Y;
  64. //鼠标不在窗口上
  65. if (mouseX < windowLeft || mouseX > windowLeft + windowWidth
  66. || mouseY < windowTop || mouseY > windowTop + windowHeight)
  67. {
  68. //上方隐藏条件
  69. if (windowTop <= screenTop)
  70. {
  71. HideAnimation(windowTop, screenTop - windowHeight + 1, Window.TopProperty);
  72. isHide = true;
  73. return;
  74. }
  75. //左侧隐藏条件
  76. if (windowLeft <= screenLeft)
  77. {
  78. HideAnimation(windowLeft, screenLeft - windowWidth + 1, Window.LeftProperty);
  79. return;
  80. }
  81. //右侧隐藏条件
  82. if (windowLeft + windowWidth + Math.Abs(screenLeft) >= screenWidth)
  83. {
  84. HideAnimation(windowLeft, screenWidth - Math.Abs(screenLeft) - 1, Window.LeftProperty);
  85. return;
  86. }
  87. } else if (mouseX >= windowLeft && mouseX <= windowLeft + windowWidth
  88. && mouseY >= windowTop && mouseY <= windowTop + windowHeight)
  89. {
  90. //上方显示
  91. if (windowTop <= screenTop - 1)
  92. {
  93. HideAnimation(windowTop, screenTop, Window.TopProperty);
  94. return;
  95. }
  96. //左侧显示
  97. if (windowLeft <= screenLeft -1)
  98. {
  99. HideAnimation(windowLeft, screenLeft, Window.LeftProperty);
  100. return;
  101. }
  102. //右侧显示
  103. if (windowLeft + Math.Abs(screenLeft) == screenWidth -1)
  104. {
  105. HideAnimation(windowLeft, screenWidth - Math.Abs(screenLeft) - windowWidth, Window.LeftProperty);
  106. return;
  107. }
  108. }
  109. }
  110. #endregion
  111. public void TimerSet()
  112. {
  113. timer = new Timer();//添加timer计时器,隐藏功能
  114. #region 计时器设置,隐藏功能
  115. timer.Interval = taskTime;
  116. timer.Tick += TimerDealy;
  117. timer.Start();
  118. #endregion
  119. }
  120. public void TimerStop()
  121. {
  122. timer.Stop();
  123. //功能关闭 如果界面是隐藏状态 那么要显示界面 ↓
  124. double screenLeft = SystemParameters.VirtualScreenLeft;
  125. double screenTop = SystemParameters.VirtualScreenTop;
  126. double screenWidth = SystemParameters.VirtualScreenWidth;
  127. double windowWidth = window.Width;
  128. double windowTop = window.Top;
  129. double windowLeft = window.Left;
  130. //左侧显示
  131. if (windowLeft <= screenLeft - 1)
  132. {
  133. HideAnimation(windowLeft, screenLeft, Window.LeftProperty);
  134. return;
  135. }
  136. //上方显示
  137. if (windowTop <= screenTop - 1)
  138. {
  139. HideAnimation(windowTop, screenTop, Window.TopProperty);
  140. return;
  141. }
  142. //右侧显示
  143. if (windowLeft + Math.Abs(screenLeft) == screenWidth - 1)
  144. {
  145. HideAnimation(windowLeft, screenWidth - Math.Abs(screenLeft) - windowWidth, Window.LeftProperty);
  146. return;
  147. }
  148. }
  149. private void HideAnimation(double from, double to, DependencyProperty property)
  150. {
  151. DoubleAnimation da = new DoubleAnimation
  152. {
  153. From = from,
  154. To = to,
  155. Duration = new Duration(TimeSpan.FromMilliseconds(hideTime))
  156. };
  157. da.Completed += (s, e) =>
  158. {
  159. window.BeginAnimation(property, null);
  160. };
  161. Timeline.SetDesiredFrameRate(da, 30);
  162. window.BeginAnimation(property, da);
  163. }
  164. }
  165. }