2021-08-19 13:59:47 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
|
2022-01-09 15:38:46 +08:00
|
|
|
|
using System.Windows.Forms;
|
2021-08-19 13:59:47 +08:00
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Media.Animation;
|
2022-04-07 20:28:00 +08:00
|
|
|
|
using GeekDesk.Constant;
|
2021-08-19 13:59:47 +08:00
|
|
|
|
|
|
|
|
|
|
namespace GeekDesk.Util
|
|
|
|
|
|
{
|
|
|
|
|
|
|
2022-01-09 15:38:46 +08:00
|
|
|
|
enum HideType
|
2021-08-19 13:59:47 +08:00
|
|
|
|
{
|
2022-01-09 15:38:46 +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
|
|
|
|
}
|
|
|
|
|
|
|
2022-04-07 20:28:00 +08:00
|
|
|
|
|
2021-08-19 13:59:47 +08:00
|
|
|
|
public class MarginHide
|
|
|
|
|
|
{
|
2022-01-09 15:38:46 +08:00
|
|
|
|
private static Window window;//定义使用该方法的窗体
|
2021-08-19 13:59:47 +08:00
|
|
|
|
|
2022-01-09 15:38:46 +08:00
|
|
|
|
private static readonly int hideTime = 200;
|
|
|
|
|
|
private static readonly int fadeHideTime = 180;
|
|
|
|
|
|
private static readonly int fadeShowTime = 200;
|
|
|
|
|
|
private static readonly int taskTime = 250;
|
2021-08-19 13:59:47 +08:00
|
|
|
|
|
2022-04-07 20:28:00 +08:00
|
|
|
|
public static readonly int shadowWidth = 20;
|
|
|
|
|
|
|
2022-04-20 16:38:55 +08:00
|
|
|
|
public static bool ON_HIDE = false;
|
|
|
|
|
|
|
2022-04-07 20:28:00 +08:00
|
|
|
|
|
2022-01-09 15:38:46 +08:00
|
|
|
|
private static double showMarginWidth = 1;
|
2021-08-19 13:59:47 +08:00
|
|
|
|
|
2022-01-09 15:38:46 +08:00
|
|
|
|
public static bool IS_HIDE = false;
|
2021-08-20 15:53:58 +08:00
|
|
|
|
|
2022-01-09 15:38:46 +08:00
|
|
|
|
private static Timer timer = null;
|
2021-08-19 13:59:47 +08:00
|
|
|
|
|
2022-01-09 15:38:46 +08:00
|
|
|
|
public static void ReadyHide(Window window)
|
2021-08-19 13:59:47 +08:00
|
|
|
|
{
|
2022-01-09 15:38:46 +08:00
|
|
|
|
MarginHide.window = window;
|
2021-08-19 13:59:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 窗体是否贴边
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
2022-01-09 15:38:46 +08:00
|
|
|
|
public static bool IsMargin()
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
//窗体是否贴边
|
|
|
|
|
|
return (windowLeft <= screenLeft
|
|
|
|
|
|
|| windowTop <= screenTop
|
|
|
|
|
|
|| windowLeft + windowWidth + Math.Abs(screenLeft) >= screenWidth);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-09 15:38:46 +08:00
|
|
|
|
|
2021-08-19 13:59:47 +08:00
|
|
|
|
|
|
|
|
|
|
#region 窗体贴边隐藏功能
|
2022-01-09 15:38:46 +08:00
|
|
|
|
private static void HideWindow(object o, EventArgs e)
|
2021-08-19 13:59:47 +08:00
|
|
|
|
{
|
2022-04-20 16:38:55 +08:00
|
|
|
|
if (window.Visibility != Visibility.Visible
|
|
|
|
|
|
|| RunTimeStatus.MARGIN_HIDE_AND_OTHER_SHOW) return;
|
2021-08-19 13:59:47 +08:00
|
|
|
|
|
|
|
|
|
|
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-09 15:38:46 +08:00
|
|
|
|
if ((mouseX < windowLeft || mouseX > windowLeft + windowWidth
|
|
|
|
|
|
|| mouseY < windowTop || mouseY > windowTop + windowHeight) && !IS_HIDE)
|
2021-08-19 13:59:47 +08:00
|
|
|
|
{
|
|
|
|
|
|
//上方隐藏条件
|
|
|
|
|
|
if (windowTop <= screenTop)
|
|
|
|
|
|
{
|
2022-01-09 15:38:46 +08:00
|
|
|
|
IS_HIDE = true;
|
|
|
|
|
|
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-09 15:38:46 +08:00
|
|
|
|
IS_HIDE = 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-09 15:38:46 +08:00
|
|
|
|
IS_HIDE = 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;
|
|
|
|
|
|
}
|
2022-01-09 15:38:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
else if (mouseX >= windowLeft && mouseX <= windowLeft + windowWidth
|
|
|
|
|
|
&& mouseY >= windowTop && mouseY <= windowTop + windowHeight && IS_HIDE)
|
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-09 15:38:46 +08:00
|
|
|
|
IS_HIDE = 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-09 15:38:46 +08:00
|
|
|
|
IS_HIDE = 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-09 15:38:46 +08:00
|
|
|
|
IS_HIDE = 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
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 15:38:46 +08:00
|
|
|
|
public static void StartHide()
|
2021-08-19 13:59:47 +08:00
|
|
|
|
{
|
2022-04-20 16:38:55 +08:00
|
|
|
|
ON_HIDE = true;
|
2022-04-07 20:28:00 +08:00
|
|
|
|
if (timer != null) return;
|
|
|
|
|
|
timer = new Timer
|
|
|
|
|
|
{
|
|
|
|
|
|
Interval = taskTime
|
|
|
|
|
|
};//添加timer计时器,隐藏功能
|
|
|
|
|
|
timer.Tick += HideWindow;
|
|
|
|
|
|
timer.Start();
|
2021-08-19 13:59:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-09 15:38:46 +08:00
|
|
|
|
public static void StopHide()
|
2021-08-19 13:59:47 +08:00
|
|
|
|
{
|
2022-04-20 16:38:55 +08:00
|
|
|
|
ON_HIDE = false;
|
2022-04-07 20:28:00 +08:00
|
|
|
|
if (timer == null) return;
|
|
|
|
|
|
timer.Stop();
|
|
|
|
|
|
timer.Dispose();
|
|
|
|
|
|
timer = null;
|
2021-08-19 13:59:47 +08:00
|
|
|
|
//功能关闭 如果界面是隐藏状态 那么要显示界面 ↓
|
2022-01-09 15:38:46 +08:00
|
|
|
|
if (IS_HIDE)
|
|
|
|
|
|
{
|
|
|
|
|
|
double screenLeft = SystemParameters.VirtualScreenLeft;
|
|
|
|
|
|
double screenTop = SystemParameters.VirtualScreenTop;
|
|
|
|
|
|
double screenWidth = SystemParameters.VirtualScreenWidth;
|
2021-08-19 13:59:47 +08:00
|
|
|
|
|
2022-01-09 15:38:46 +08:00
|
|
|
|
double windowWidth = window.Width;
|
2021-08-19 13:59:47 +08:00
|
|
|
|
|
2022-01-09 15:38:46 +08:00
|
|
|
|
double windowTop = window.Top;
|
|
|
|
|
|
double windowLeft = window.Left;
|
2021-08-19 13:59:47 +08:00
|
|
|
|
|
2022-01-09 15:38:46 +08:00
|
|
|
|
//左侧显示
|
|
|
|
|
|
if (windowLeft <= screenLeft - showMarginWidth)
|
|
|
|
|
|
{
|
|
|
|
|
|
IS_HIDE = false;
|
|
|
|
|
|
FadeAnimation(0, 1);
|
|
|
|
|
|
HideAnimation(windowLeft, screenLeft, Window.LeftProperty, HideType.LEFT_SHOW);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2021-08-19 13:59:47 +08:00
|
|
|
|
|
2022-01-09 15:38:46 +08:00
|
|
|
|
//上方显示
|
|
|
|
|
|
if (windowTop <= screenTop - showMarginWidth)
|
|
|
|
|
|
{
|
|
|
|
|
|
IS_HIDE = false;
|
|
|
|
|
|
FadeAnimation(0, 1);
|
|
|
|
|
|
HideAnimation(windowTop, screenTop, Window.TopProperty, HideType.TOP_SHOW);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2021-08-19 13:59:47 +08:00
|
|
|
|
|
2022-01-09 15:38:46 +08:00
|
|
|
|
//右侧显示
|
|
|
|
|
|
if (windowLeft + Math.Abs(screenLeft) == screenWidth - showMarginWidth)
|
|
|
|
|
|
{
|
|
|
|
|
|
IS_HIDE = false;
|
|
|
|
|
|
FadeAnimation(0, 1);
|
|
|
|
|
|
HideAnimation(windowLeft, screenWidth - Math.Abs(screenLeft) - windowWidth, Window.LeftProperty, HideType.RIGHT_SHOW);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2021-08-19 13:59:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 15:38:46 +08:00
|
|
|
|
private static void HideAnimation(double from, double to, DependencyProperty property, HideType hideType)
|
2021-08-19 13:59:47 +08:00
|
|
|
|
{
|
2022-01-09 15:38:46 +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))
|
|
|
|
|
|
};
|
2022-04-07 20:28:00 +08:00
|
|
|
|
// 如果是显示 则贴屏幕侧不显示阴影
|
|
|
|
|
|
bool isShow = false;
|
|
|
|
|
|
int shadowWidthTemp = Constants.SHADOW_WIDTH;
|
|
|
|
|
|
if (hideType <= HideType.RIGHT_SHOW)
|
|
|
|
|
|
{
|
|
|
|
|
|
isShow = true;
|
|
|
|
|
|
if (hideType == HideType.RIGHT_SHOW)
|
|
|
|
|
|
{
|
|
|
|
|
|
shadowWidthTemp = -shadowWidthTemp;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-08-19 13:59:47 +08:00
|
|
|
|
da.Completed += (s, e) =>
|
|
|
|
|
|
{
|
2022-01-09 15:38:46 +08:00
|
|
|
|
if ("Top".Equals(property.Name))
|
|
|
|
|
|
{
|
2022-04-07 20:28:00 +08:00
|
|
|
|
window.Top = isShow ? toTemp - shadowWidthTemp : toTemp;
|
2022-01-09 15:38:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2022-04-07 20:28:00 +08:00
|
|
|
|
window.Left = isShow ? toTemp - shadowWidthTemp : toTemp;
|
2022-01-09 15:38:46 +08:00
|
|
|
|
}
|
2021-08-19 13:59:47 +08:00
|
|
|
|
window.BeginAnimation(property, null);
|
|
|
|
|
|
};
|
2022-01-09 15:38:46 +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-09 15:38:46 +08:00
|
|
|
|
|
|
|
|
|
|
private static 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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-04-20 16:38:55 +08:00
|
|
|
|
public static void WaitHide(int waitTime)
|
|
|
|
|
|
{
|
|
|
|
|
|
new System.Threading.Thread(()=>
|
|
|
|
|
|
{
|
|
|
|
|
|
System.Threading.Thread.Sleep(waitTime);
|
|
|
|
|
|
//修改状态为false 继续执行贴边隐藏
|
|
|
|
|
|
RunTimeStatus.MARGIN_HIDE_AND_OTHER_SHOW = false;
|
|
|
|
|
|
}).Start();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-08-19 13:59:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|