2021-08-19 13:59:47 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
2022-01-06 17:39:04 +08:00
|
|
|
|
using System.Drawing;
|
|
|
|
|
|
using System.Windows.Forms;
|
2021-08-19 13:59:47 +08:00
|
|
|
|
using MouseEventArgs = System.Windows.Input.MouseEventArgs;
|
|
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Media.Animation;
|
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
|
|
|
|
|
|
|
namespace GeekDesk.Util
|
|
|
|
|
|
{
|
|
|
|
|
|
|
2022-01-06 17:39:04 +08:00
|
|
|
|
enum HideType
|
2021-08-19 13:59:47 +08:00
|
|
|
|
{
|
2022-01-06 17:39:04 +08:00
|
|
|
|
TOP_SHOW = 1,
|
|
|
|
|
|
LEFT_SHOW = 2,
|
|
|
|
|
|
RIGHT_SHOW = 3,
|
|
|
|
|
|
TOP_HIDE = 4,
|
|
|
|
|
|
LEFT_HIDE = 5,
|
|
|
|
|
|
RIGHT_HIDE = 6
|
2021-08-19 13:59:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class MarginHide
|
|
|
|
|
|
{
|
|
|
|
|
|
readonly Window window;//定义使用该方法的窗体
|
|
|
|
|
|
|
2022-01-06 17:39:04 +08:00
|
|
|
|
private readonly int hideTime = 200;
|
2021-08-19 13:59:47 +08:00
|
|
|
|
|
2022-01-06 17:39:04 +08:00
|
|
|
|
private readonly int fadeHideTime = 180;
|
|
|
|
|
|
|
|
|
|
|
|
private readonly int fadeShowTime = 200;
|
|
|
|
|
|
|
|
|
|
|
|
private readonly int taskTime = 250;
|
2021-08-19 13:59:47 +08:00
|
|
|
|
|
2021-08-20 15:53:58 +08:00
|
|
|
|
private double showMarginWidth = 1;
|
|
|
|
|
|
|
2021-12-10 17:58:23 +08:00
|
|
|
|
#pragma warning disable CS0414 // 字段“MarginHide.isHide”已被赋值,但从未使用过它的值
|
2022-01-06 17:39:04 +08:00
|
|
|
|
public static bool isHide = false;
|
2021-12-10 17:58:23 +08:00
|
|
|
|
#pragma warning restore CS0414 // 字段“MarginHide.isHide”已被赋值,但从未使用过它的值
|
2021-08-19 13:59:47 +08:00
|
|
|
|
|
|
|
|
|
|
public Timer timer;
|
|
|
|
|
|
//构造函数,传入将要匹配的窗体
|
|
|
|
|
|
public MarginHide(Window window)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.window = window;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 窗体是否贴边
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public bool IsMargin()
|
|
|
|
|
|
{
|
|
|
|
|
|
double screenLeft = SystemParameters.VirtualScreenLeft;
|
|
|
|
|
|
double screenTop = SystemParameters.VirtualScreenTop;
|
|
|
|
|
|
double screenWidth = SystemParameters.VirtualScreenWidth;
|
|
|
|
|
|
|
|
|
|
|
|
double windowWidth = window.Width;
|
|
|
|
|
|
|
|
|
|
|
|
double windowTop = window.Top;
|
|
|
|
|
|
double windowLeft = window.Left;
|
|
|
|
|
|
|
|
|
|
|
|
//窗体是否贴边
|
|
|
|
|
|
return (windowLeft <= screenLeft
|
|
|
|
|
|
|| windowTop <= screenTop
|
|
|
|
|
|
|| windowLeft + windowWidth + Math.Abs(screenLeft) >= screenWidth);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region 窗体贴边隐藏功能
|
|
|
|
|
|
private void TimerDealy(object o, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (window.Visibility != Visibility.Visible) return;
|
|
|
|
|
|
|
|
|
|
|
|
double screenLeft = SystemParameters.VirtualScreenLeft;
|
|
|
|
|
|
double screenTop = SystemParameters.VirtualScreenTop;
|
|
|
|
|
|
double screenWidth = SystemParameters.VirtualScreenWidth;
|
|
|
|
|
|
|
|
|
|
|
|
double windowHeight = window.Height;
|
|
|
|
|
|
double windowWidth = window.Width;
|
|
|
|
|
|
|
|
|
|
|
|
double windowTop = window.Top;
|
|
|
|
|
|
double windowLeft = window.Left;
|
|
|
|
|
|
|
|
|
|
|
|
//获取鼠标位置
|
|
|
|
|
|
System.Windows.Point p = MouseUtil.GetMousePosition();
|
|
|
|
|
|
double mouseX = p.X;
|
|
|
|
|
|
double mouseY = p.Y;
|
|
|
|
|
|
|
|
|
|
|
|
//鼠标不在窗口上
|
2022-01-06 17:39:04 +08:00
|
|
|
|
if ((mouseX < windowLeft || mouseX > windowLeft + windowWidth
|
|
|
|
|
|
|| mouseY < windowTop || mouseY > windowTop + windowHeight) && !isHide)
|
2021-08-19 13:59:47 +08:00
|
|
|
|
{
|
|
|
|
|
|
//上方隐藏条件
|
|
|
|
|
|
if (windowTop <= screenTop)
|
|
|
|
|
|
{
|
|
|
|
|
|
isHide = true;
|
2022-01-06 17:39:04 +08:00
|
|
|
|
FadeAnimation(1, 0);
|
|
|
|
|
|
HideAnimation(windowTop, screenTop - windowHeight + showMarginWidth, Window.TopProperty, HideType.TOP_HIDE);
|
2021-08-19 13:59:47 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
//左侧隐藏条件
|
|
|
|
|
|
if (windowLeft <= screenLeft)
|
|
|
|
|
|
{
|
2022-01-06 17:39:04 +08:00
|
|
|
|
isHide = true;
|
|
|
|
|
|
FadeAnimation(1, 0);
|
|
|
|
|
|
HideAnimation(windowLeft, screenLeft - windowWidth + showMarginWidth, Window.LeftProperty, HideType.LEFT_HIDE);
|
2021-08-19 13:59:47 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
//右侧隐藏条件
|
|
|
|
|
|
if (windowLeft + windowWidth + Math.Abs(screenLeft) >= screenWidth)
|
|
|
|
|
|
{
|
2022-01-06 17:39:04 +08:00
|
|
|
|
isHide = true;
|
|
|
|
|
|
FadeAnimation(1, 0);
|
|
|
|
|
|
HideAnimation(windowLeft, screenWidth - Math.Abs(screenLeft) - showMarginWidth, Window.LeftProperty, HideType.RIGHT_HIDE);
|
2021-08-19 13:59:47 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if (mouseX >= windowLeft && mouseX <= windowLeft + windowWidth
|
2022-01-06 17:39:04 +08:00
|
|
|
|
&& mouseY >= windowTop && mouseY <= windowTop + windowHeight && isHide)
|
2021-08-19 13:59:47 +08:00
|
|
|
|
{
|
|
|
|
|
|
//上方显示
|
2021-08-20 15:53:58 +08:00
|
|
|
|
if (windowTop <= screenTop - showMarginWidth)
|
2021-08-19 13:59:47 +08:00
|
|
|
|
{
|
2022-01-06 17:39:04 +08:00
|
|
|
|
isHide = false;
|
|
|
|
|
|
HideAnimation(windowTop, screenTop, Window.TopProperty, HideType.TOP_SHOW);
|
|
|
|
|
|
FadeAnimation(0, 1);
|
2021-08-19 13:59:47 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
//左侧显示
|
2021-08-20 15:53:58 +08:00
|
|
|
|
if (windowLeft <= screenLeft - showMarginWidth)
|
2021-08-19 13:59:47 +08:00
|
|
|
|
{
|
2022-01-06 17:39:04 +08:00
|
|
|
|
isHide = false;
|
|
|
|
|
|
HideAnimation(windowLeft, screenLeft, Window.LeftProperty, HideType.LEFT_SHOW);
|
|
|
|
|
|
FadeAnimation(0, 1);
|
2021-08-19 13:59:47 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
//右侧显示
|
2021-08-20 15:53:58 +08:00
|
|
|
|
if (windowLeft + Math.Abs(screenLeft) == screenWidth - showMarginWidth)
|
2021-08-19 13:59:47 +08:00
|
|
|
|
{
|
2022-01-06 17:39:04 +08:00
|
|
|
|
isHide = false;
|
|
|
|
|
|
HideAnimation(windowLeft, screenWidth - Math.Abs(screenLeft) - windowWidth, Window.LeftProperty, HideType.RIGHT_SHOW);
|
|
|
|
|
|
FadeAnimation(0, 1);
|
2021-08-19 13:59:47 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void TimerSet()
|
|
|
|
|
|
{
|
2021-08-20 15:53:58 +08:00
|
|
|
|
if (timer != null) return;
|
2021-08-19 13:59:47 +08:00
|
|
|
|
timer = new Timer();//添加timer计时器,隐藏功能
|
|
|
|
|
|
#region 计时器设置,隐藏功能
|
|
|
|
|
|
timer.Interval = taskTime;
|
|
|
|
|
|
timer.Tick += TimerDealy;
|
|
|
|
|
|
timer.Start();
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void TimerStop()
|
|
|
|
|
|
{
|
|
|
|
|
|
timer.Stop();
|
2021-08-20 15:53:58 +08:00
|
|
|
|
timer.Dispose();
|
|
|
|
|
|
timer = null;
|
2021-08-19 13:59:47 +08:00
|
|
|
|
//功能关闭 如果界面是隐藏状态 那么要显示界面 ↓
|
|
|
|
|
|
|
|
|
|
|
|
double screenLeft = SystemParameters.VirtualScreenLeft;
|
|
|
|
|
|
double screenTop = SystemParameters.VirtualScreenTop;
|
|
|
|
|
|
double screenWidth = SystemParameters.VirtualScreenWidth;
|
|
|
|
|
|
|
|
|
|
|
|
double windowWidth = window.Width;
|
|
|
|
|
|
|
|
|
|
|
|
double windowTop = window.Top;
|
|
|
|
|
|
double windowLeft = window.Left;
|
|
|
|
|
|
|
|
|
|
|
|
//左侧显示
|
2021-08-20 15:53:58 +08:00
|
|
|
|
if (windowLeft <= screenLeft - showMarginWidth)
|
2021-08-19 13:59:47 +08:00
|
|
|
|
{
|
2022-01-06 17:39:04 +08:00
|
|
|
|
isHide = false;
|
|
|
|
|
|
FadeAnimation(0, 1);
|
|
|
|
|
|
HideAnimation(windowLeft, screenLeft, Window.LeftProperty, HideType.LEFT_SHOW);
|
2021-08-19 13:59:47 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//上方显示
|
2021-08-20 15:53:58 +08:00
|
|
|
|
if (windowTop <= screenTop - showMarginWidth)
|
2021-08-19 13:59:47 +08:00
|
|
|
|
{
|
2022-01-06 17:39:04 +08:00
|
|
|
|
isHide = false;
|
|
|
|
|
|
FadeAnimation(0, 1);
|
|
|
|
|
|
HideAnimation(windowTop, screenTop, Window.TopProperty, HideType.TOP_SHOW);
|
2021-08-19 13:59:47 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//右侧显示
|
2021-08-20 15:53:58 +08:00
|
|
|
|
if (windowLeft + Math.Abs(screenLeft) == screenWidth - showMarginWidth)
|
2021-08-19 13:59:47 +08:00
|
|
|
|
{
|
2022-01-06 17:39:04 +08:00
|
|
|
|
isHide = false;
|
|
|
|
|
|
FadeAnimation(0, 1);
|
|
|
|
|
|
HideAnimation(windowLeft, screenWidth - Math.Abs(screenLeft) - windowWidth, Window.LeftProperty, HideType.RIGHT_SHOW);
|
2021-08-19 13:59:47 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-01-06 17:39:04 +08:00
|
|
|
|
private void HideAnimation(double from, double to, DependencyProperty property, HideType hideType)
|
2021-08-19 13:59:47 +08:00
|
|
|
|
{
|
2022-01-06 17:39:04 +08:00
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
2021-08-19 13:59:47 +08:00
|
|
|
|
DoubleAnimation da = new DoubleAnimation
|
|
|
|
|
|
{
|
|
|
|
|
|
From = from,
|
|
|
|
|
|
To = to,
|
|
|
|
|
|
Duration = new Duration(TimeSpan.FromMilliseconds(hideTime))
|
|
|
|
|
|
};
|
|
|
|
|
|
da.Completed += (s, e) =>
|
|
|
|
|
|
{
|
2022-01-06 17:39:04 +08:00
|
|
|
|
if ("Top".Equals(property.Name))
|
|
|
|
|
|
{
|
|
|
|
|
|
window.Top = toTemp;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
window.Left = toTemp;
|
|
|
|
|
|
}
|
2021-08-19 13:59:47 +08:00
|
|
|
|
window.BeginAnimation(property, null);
|
|
|
|
|
|
};
|
2022-01-06 17:39:04 +08:00
|
|
|
|
|
2021-08-20 15:53:58 +08:00
|
|
|
|
Timeline.SetDesiredFrameRate(da, 60);
|
2021-08-19 13:59:47 +08:00
|
|
|
|
window.BeginAnimation(property, da);
|
|
|
|
|
|
}
|
2022-01-06 17:39:04 +08:00
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-08-19 13:59:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|