From e99eee08ff6d7111cacf5869c9533d953152c03b Mon Sep 17 00:00:00 2001 From: liufei Date: Thu, 6 Jan 2022 17:39:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=B4=B4=E8=BE=B9=E9=9A=90?= =?UTF-8?q?=E8=97=8F=E5=8A=A8=E7=94=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/MarginHide.cs | 129 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 107 insertions(+), 22 deletions(-) diff --git a/Util/MarginHide.cs b/Util/MarginHide.cs index 861c72f..d6fe6ac 100644 --- a/Util/MarginHide.cs +++ b/Util/MarginHide.cs @@ -4,8 +4,8 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -using System.Drawing; //添加引用 -using System.Windows.Forms;//添加引用 +using System.Drawing; +using System.Windows.Forms; using MouseEventArgs = System.Windows.Input.MouseEventArgs; using System.Windows; using System.Windows.Media.Animation; @@ -14,25 +14,32 @@ using System.Windows.Media; namespace GeekDesk.Util { - enum HidePosition + enum HideType { - TOP = 1, - LEFT = 2, - RIGHT = 3 + TOP_SHOW = 1, + LEFT_SHOW = 2, + RIGHT_SHOW = 3, + TOP_HIDE = 4, + LEFT_HIDE = 5, + RIGHT_HIDE = 6 } public class MarginHide { readonly Window window;//定义使用该方法的窗体 - private readonly int hideTime = 80; + private readonly int hideTime = 200; - private readonly int taskTime = 200; + private readonly int fadeHideTime = 180; + + private readonly int fadeShowTime = 200; + + private readonly int taskTime = 250; private double showMarginWidth = 1; #pragma warning disable CS0414 // 字段“MarginHide.isHide”已被赋值,但从未使用过它的值 - private bool isHide; + public static bool isHide = false; #pragma warning restore CS0414 // 字段“MarginHide.isHide”已被赋值,但从未使用过它的值 public Timer timer; @@ -87,47 +94,58 @@ namespace GeekDesk.Util double mouseY = p.Y; //鼠标不在窗口上 - if (mouseX < windowLeft || mouseX > windowLeft + windowWidth - || mouseY < windowTop || mouseY > windowTop + windowHeight) + if ((mouseX < windowLeft || mouseX > windowLeft + windowWidth + || mouseY < windowTop || mouseY > windowTop + windowHeight) && !isHide) { //上方隐藏条件 if (windowTop <= screenTop) { - HideAnimation(windowTop, screenTop - windowHeight + showMarginWidth, Window.TopProperty); isHide = true; + FadeAnimation(1, 0); + HideAnimation(windowTop, screenTop - windowHeight + showMarginWidth, Window.TopProperty, HideType.TOP_HIDE); return; } //左侧隐藏条件 if (windowLeft <= screenLeft) { - HideAnimation(windowLeft, screenLeft - windowWidth + showMarginWidth, Window.LeftProperty); + isHide = true; + FadeAnimation(1, 0); + HideAnimation(windowLeft, screenLeft - windowWidth + showMarginWidth, Window.LeftProperty, HideType.LEFT_HIDE); return; } //右侧隐藏条件 if (windowLeft + windowWidth + Math.Abs(screenLeft) >= screenWidth) { - HideAnimation(windowLeft, screenWidth - Math.Abs(screenLeft) - showMarginWidth, Window.LeftProperty); + isHide = true; + FadeAnimation(1, 0); + HideAnimation(windowLeft, screenWidth - Math.Abs(screenLeft) - showMarginWidth, Window.LeftProperty, HideType.RIGHT_HIDE); return; } } else if (mouseX >= windowLeft && mouseX <= windowLeft + windowWidth - && mouseY >= windowTop && mouseY <= windowTop + windowHeight) + && mouseY >= windowTop && mouseY <= windowTop + windowHeight && isHide) { //上方显示 if (windowTop <= screenTop - showMarginWidth) { - HideAnimation(windowTop, screenTop, Window.TopProperty); + isHide = false; + HideAnimation(windowTop, screenTop, Window.TopProperty, HideType.TOP_SHOW); + FadeAnimation(0, 1); return; } //左侧显示 if (windowLeft <= screenLeft - showMarginWidth) { - HideAnimation(windowLeft, screenLeft, Window.LeftProperty); + isHide = false; + HideAnimation(windowLeft, screenLeft, Window.LeftProperty, HideType.LEFT_SHOW); + FadeAnimation(0, 1); return; } //右侧显示 if (windowLeft + Math.Abs(screenLeft) == screenWidth - showMarginWidth) { - HideAnimation(windowLeft, screenWidth - Math.Abs(screenLeft) - windowWidth, Window.LeftProperty); + isHide = false; + HideAnimation(windowLeft, screenWidth - Math.Abs(screenLeft) - windowWidth, Window.LeftProperty, HideType.RIGHT_SHOW); + FadeAnimation(0, 1); return; } } @@ -166,28 +184,59 @@ namespace GeekDesk.Util //左侧显示 if (windowLeft <= screenLeft - showMarginWidth) { - HideAnimation(windowLeft, screenLeft, Window.LeftProperty); + isHide = false; + FadeAnimation(0, 1); + HideAnimation(windowLeft, screenLeft, Window.LeftProperty, HideType.LEFT_SHOW); return; } //上方显示 if (windowTop <= screenTop - showMarginWidth) { - HideAnimation(windowTop, screenTop, Window.TopProperty); + isHide = false; + FadeAnimation(0, 1); + HideAnimation(windowTop, screenTop, Window.TopProperty, HideType.TOP_SHOW); return; } //右侧显示 if (windowLeft + Math.Abs(screenLeft) == screenWidth - showMarginWidth) { - HideAnimation(windowLeft, screenWidth - Math.Abs(screenLeft) - windowWidth, Window.LeftProperty); + isHide = false; + FadeAnimation(0, 1); + HideAnimation(windowLeft, screenWidth - Math.Abs(screenLeft) - windowWidth, Window.LeftProperty, HideType.RIGHT_SHOW); return; } } - private void HideAnimation(double from, double to, DependencyProperty property) + private void HideAnimation(double from, double to, DependencyProperty property, HideType hideType) { + + double toTemp = to; + double leftT = window.Width / 4 * 3; + double topT = window.Height / 4 * 3; + switch (hideType) + { + case HideType.LEFT_HIDE: + to += leftT; + break; + case HideType.LEFT_SHOW: + to -= leftT; + break; + case HideType.RIGHT_HIDE: + to -= leftT; + break; + case HideType.RIGHT_SHOW: + to += leftT; + break; + case HideType.TOP_HIDE: + to += topT; + break; + case HideType.TOP_SHOW: + to -= topT; + break; + } DoubleAnimation da = new DoubleAnimation { From = from, @@ -196,11 +245,47 @@ namespace GeekDesk.Util }; da.Completed += (s, e) => { + if ("Top".Equals(property.Name)) + { + window.Top = toTemp; + } + else + { + window.Left = toTemp; + } window.BeginAnimation(property, null); }; + Timeline.SetDesiredFrameRate(da, 60); window.BeginAnimation(property, da); } + + private void FadeAnimation(double from, double to) + { + double time; + if (to == 0D) + { + time = fadeHideTime; + } else + { + time = fadeShowTime; + } + DoubleAnimation opacityAnimation = new DoubleAnimation + { + From = from, + To = to, + Duration = new Duration(TimeSpan.FromMilliseconds(time)) + }; + opacityAnimation.Completed += (s, e) => + { + //window.Opacity = to; + window.BeginAnimation(Window.OpacityProperty, null); + }; + Timeline.SetDesiredFrameRate(opacityAnimation, 60); + window.BeginAnimation(Window.OpacityProperty, opacityAnimation); + } + + } }