MarginHide.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 HideType
  15. {
  16. TOP_SHOW = 1,
  17. LEFT_SHOW = 2,
  18. RIGHT_SHOW = 3,
  19. TOP_HIDE = 4,
  20. LEFT_HIDE = 5,
  21. RIGHT_HIDE = 6
  22. }
  23. public class MarginHide
  24. {
  25. private static Window window;//定义使用该方法的窗体
  26. private static readonly int hideTime = 200;
  27. private static readonly int fadeHideTime = 180;
  28. private static readonly int fadeShowTime = 200;
  29. private static readonly int taskTime = 250;
  30. private static double showMarginWidth = 1;
  31. public static bool IS_RUN = false;
  32. public static bool IS_HIDE = false;
  33. private static Timer timer = null;
  34. public static void ReadyHide(Window window)
  35. {
  36. MarginHide.window = window;
  37. if (timer != null) return;
  38. timer = new Timer();//添加timer计时器,隐藏功能
  39. timer.Interval = taskTime;
  40. timer.Tick += HideWindow;
  41. timer.Start();
  42. }
  43. /// <summary>
  44. /// 窗体是否贴边
  45. /// </summary>
  46. /// <returns></returns>
  47. public static bool IsMargin()
  48. {
  49. double screenLeft = SystemParameters.VirtualScreenLeft;
  50. double screenTop = SystemParameters.VirtualScreenTop;
  51. double screenWidth = SystemParameters.VirtualScreenWidth;
  52. double windowWidth = window.Width;
  53. double windowTop = window.Top;
  54. double windowLeft = window.Left;
  55. //窗体是否贴边
  56. return (windowLeft <= screenLeft
  57. || windowTop <= screenTop
  58. || windowLeft + windowWidth + Math.Abs(screenLeft) >= screenWidth);
  59. }
  60. #region 窗体贴边隐藏功能
  61. private static void HideWindow(object o, EventArgs e)
  62. {
  63. if (window.Visibility != Visibility.Visible || !IS_RUN) return;
  64. double screenLeft = SystemParameters.VirtualScreenLeft;
  65. double screenTop = SystemParameters.VirtualScreenTop;
  66. double screenWidth = SystemParameters.VirtualScreenWidth;
  67. double windowHeight = window.Height;
  68. double windowWidth = window.Width;
  69. double windowTop = window.Top;
  70. double windowLeft = window.Left;
  71. //获取鼠标位置
  72. System.Windows.Point p = MouseUtil.GetMousePosition();
  73. double mouseX = p.X;
  74. double mouseY = p.Y;
  75. //鼠标不在窗口上
  76. if ((mouseX < windowLeft || mouseX > windowLeft + windowWidth
  77. || mouseY < windowTop || mouseY > windowTop + windowHeight) && !IS_HIDE)
  78. {
  79. //上方隐藏条件
  80. if (windowTop <= screenTop)
  81. {
  82. IS_HIDE = true;
  83. FadeAnimation(1, 0);
  84. HideAnimation(windowTop, screenTop - windowHeight + showMarginWidth, Window.TopProperty, HideType.TOP_HIDE);
  85. return;
  86. }
  87. //左侧隐藏条件
  88. if (windowLeft <= screenLeft)
  89. {
  90. IS_HIDE = true;
  91. FadeAnimation(1, 0);
  92. HideAnimation(windowLeft, screenLeft - windowWidth + showMarginWidth, Window.LeftProperty, HideType.LEFT_HIDE);
  93. return;
  94. }
  95. //右侧隐藏条件
  96. if (windowLeft + windowWidth + Math.Abs(screenLeft) >= screenWidth)
  97. {
  98. IS_HIDE = true;
  99. FadeAnimation(1, 0);
  100. HideAnimation(windowLeft, screenWidth - Math.Abs(screenLeft) - showMarginWidth, Window.LeftProperty, HideType.RIGHT_HIDE);
  101. return;
  102. }
  103. }
  104. else if (mouseX >= windowLeft && mouseX <= windowLeft + windowWidth
  105. && mouseY >= windowTop && mouseY <= windowTop + windowHeight && IS_HIDE)
  106. {
  107. //上方显示
  108. if (windowTop <= screenTop - showMarginWidth)
  109. {
  110. IS_HIDE = false;
  111. HideAnimation(windowTop, screenTop, Window.TopProperty, HideType.TOP_SHOW);
  112. FadeAnimation(0, 1);
  113. return;
  114. }
  115. //左侧显示
  116. if (windowLeft <= screenLeft - showMarginWidth)
  117. {
  118. IS_HIDE = false;
  119. HideAnimation(windowLeft, screenLeft, Window.LeftProperty, HideType.LEFT_SHOW);
  120. FadeAnimation(0, 1);
  121. return;
  122. }
  123. //右侧显示
  124. if (windowLeft + Math.Abs(screenLeft) == screenWidth - showMarginWidth)
  125. {
  126. IS_HIDE = false;
  127. HideAnimation(windowLeft, screenWidth - Math.Abs(screenLeft) - windowWidth, Window.LeftProperty, HideType.RIGHT_SHOW);
  128. FadeAnimation(0, 1);
  129. return;
  130. }
  131. }
  132. }
  133. #endregion
  134. public static void StartHide()
  135. {
  136. IS_RUN = true;
  137. }
  138. public static void StopHide()
  139. {
  140. IS_RUN = false;
  141. //功能关闭 如果界面是隐藏状态 那么要显示界面 ↓
  142. if (IS_HIDE)
  143. {
  144. double screenLeft = SystemParameters.VirtualScreenLeft;
  145. double screenTop = SystemParameters.VirtualScreenTop;
  146. double screenWidth = SystemParameters.VirtualScreenWidth;
  147. double windowWidth = window.Width;
  148. double windowTop = window.Top;
  149. double windowLeft = window.Left;
  150. //左侧显示
  151. if (windowLeft <= screenLeft - showMarginWidth)
  152. {
  153. IS_HIDE = false;
  154. FadeAnimation(0, 1);
  155. HideAnimation(windowLeft, screenLeft, Window.LeftProperty, HideType.LEFT_SHOW);
  156. return;
  157. }
  158. //上方显示
  159. if (windowTop <= screenTop - showMarginWidth)
  160. {
  161. IS_HIDE = false;
  162. FadeAnimation(0, 1);
  163. HideAnimation(windowTop, screenTop, Window.TopProperty, HideType.TOP_SHOW);
  164. return;
  165. }
  166. //右侧显示
  167. if (windowLeft + Math.Abs(screenLeft) == screenWidth - showMarginWidth)
  168. {
  169. IS_HIDE = false;
  170. FadeAnimation(0, 1);
  171. HideAnimation(windowLeft, screenWidth - Math.Abs(screenLeft) - windowWidth, Window.LeftProperty, HideType.RIGHT_SHOW);
  172. return;
  173. }
  174. }
  175. }
  176. private static void HideAnimation(double from, double to, DependencyProperty property, HideType hideType)
  177. {
  178. double toTemp = to;
  179. double leftT = window.Width / 4 * 3;
  180. double topT = window.Height / 4 * 3;
  181. switch (hideType)
  182. {
  183. case HideType.LEFT_HIDE:
  184. to += leftT;
  185. break;
  186. case HideType.LEFT_SHOW:
  187. to -= leftT;
  188. break;
  189. case HideType.RIGHT_HIDE:
  190. to -= leftT;
  191. break;
  192. case HideType.RIGHT_SHOW:
  193. to += leftT;
  194. break;
  195. case HideType.TOP_HIDE:
  196. to += topT;
  197. break;
  198. case HideType.TOP_SHOW:
  199. to -= topT;
  200. break;
  201. }
  202. DoubleAnimation da = new DoubleAnimation
  203. {
  204. From = from,
  205. To = to,
  206. Duration = new Duration(TimeSpan.FromMilliseconds(hideTime))
  207. };
  208. da.Completed += (s, e) =>
  209. {
  210. if ("Top".Equals(property.Name))
  211. {
  212. window.Top = toTemp;
  213. }
  214. else
  215. {
  216. window.Left = toTemp;
  217. }
  218. window.BeginAnimation(property, null);
  219. };
  220. Timeline.SetDesiredFrameRate(da, 60);
  221. window.BeginAnimation(property, da);
  222. }
  223. private static void FadeAnimation(double from, double to)
  224. {
  225. double time;
  226. if (to == 0D)
  227. {
  228. time = fadeHideTime;
  229. }
  230. else
  231. {
  232. time = fadeShowTime;
  233. }
  234. DoubleAnimation opacityAnimation = new DoubleAnimation
  235. {
  236. From = from,
  237. To = to,
  238. Duration = new Duration(TimeSpan.FromMilliseconds(time))
  239. };
  240. opacityAnimation.Completed += (s, e) =>
  241. {
  242. //window.Opacity = to;
  243. window.BeginAnimation(Window.OpacityProperty, null);
  244. };
  245. Timeline.SetDesiredFrameRate(opacityAnimation, 60);
  246. window.BeginAnimation(Window.OpacityProperty, opacityAnimation);
  247. }
  248. }
  249. }