MarginHide.cs 10 KB

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