WPF窗体实现靠近窗体边缘自动贴边展示效果代码实现
前些天原本突发奇想的想做一个桌面助手的工具,用于管理日常用来远程向日葵,todesk等远程软件的账号信息的桌面管理软件,其中想实现类似与qq的自动贴边收起的效果,琢磨了好久终于实现出来类似效果了 代码非常简单
核心逻辑比较简单, 就是创建一个timer 定时检测窗体当前的位置坐标,当top的绝对值小于30时 自动修改成0(也可以使用动画) 后续紧接着在判断如果鼠标未处于窗体内 就使用动画效果,将窗体一直往顶部移动 直到看不见,动画结束事件里再隐藏窗体就实现了该效果
核心代码如下:
private void TopHideWindow()
{
int boundary = 30;//一定距离范围类自动贴边
//左侧贴边
if (this.Left < 0 || (this.Left > 0 && this.Left < boundary))
{
this.Left = 0;
}
//顶部贴边
if (this.Top < 0 || (this.Top > 0 && this.Top < boundary))
{
this.Top = 0;
}
//右侧贴边
int maxLeft = (int)(SystemParameters.PrimaryScreenWidth - this.Width);
if (this.Left > maxLeft || (this.Left < maxLeft && this.Left > maxLeft- boundary))
{
this.Left = maxLeft;
}
if(this.Top==0)
{
this.HideAnimation = new DoubleAnimation()
{
From = this.Top,
To = -this.Height,
Duration = new Duration(TimeSpan.FromSeconds(0.5))
};
this.HideAnimation.Completed += delegate (object sender, EventArgs e)
{
this.Hide();
};
}
}