MarginHide.cs 12 KB

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