WPF靠边停靠,贴边隐藏,窗体位移动画

全局变量

private bool blnTopHide = false;
private bool IsMouseEnter = false;//窗体状态 true为显示 false为隐藏
private System.Windows.Forms.Timer timer;//计时器 通过win32api实时获取鼠标位置

核心是使用Timer定时器,检查鼠标在窗体内部还是外部

 void timer_Tick(object sender, EventArgs e)
        {
            Point point = PointToScreen(Mouse.GetPosition(this));//获取鼠标相对桌面的位置
            if (point.X >= this.Left && point.X <= this.Left + this.Width+30 && point.Y >= this.Top && point.Y <= this.Top + this.Height+30)              
                IsMouseEnter = true;    //鼠标在窗体内部
            else
                IsMouseEnter = false;   //鼠标离开窗体
            if (this.Top<=0){
                if (blnTopHide){
                    if (point.X >= this.Left && point.X <= this.Left + this.Width){
                        if (point.Y <= 0){
                            TranslateTransform tt = new TranslateTransform();
                            DoubleAnimation da = new DoubleAnimation();
                            Duration duration = new Duration(TimeSpan.FromSeconds(0.6));//动画时间
                            this.RenderTransform = tt;//设置按钮的转换效果
                            tt.Y = 5 - this.Height;
                            da.To = 0;
                            da.SpeedRatio = 2;
                            da.Duration = duration;
            
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
WPF仿QQ贴边自动隐藏功能主要是通过设置窗口的动画效果来实现的。首先,需要监听窗口的位置变化事件,当窗口靠近屏幕边缘时,触发隐藏动画效果。其次,可以使用WPF自带的动画库,如DoubleAnimation、Storyboard等,来定义窗口隐藏时的动画效果。在动画完成后,将窗口的Visibility属性设置为Collapsed来实现窗口的隐藏。 下面是实现这一功能的一段简单源码示例: ```csharp private void Window_LocationChanged(object sender, EventArgs e) { double screenWidth = SystemParameters.PrimaryScreenWidth; double screenHeight = SystemParameters.PrimaryScreenHeight; double pixelsFromTop = this.Top; double pixelsFromBottom = screenHeight - this.Top - this.ActualHeight; double pixelsFromLeft = this.Left; double pixelsFromRight = screenWidth - this.Left - this.ActualWidth; double boundary = 10; // 靠边触发隐藏的距离阈值 if (pixelsFromTop < boundary || pixelsFromBottom < boundary || pixelsFromLeft < boundary || pixelsFromRight < boundary) { // 如果靠近边缘,执行隐藏动画 DoubleAnimation animation = new DoubleAnimation(1, 0, new Duration(TimeSpan.FromSeconds(0.5))); this.BeginAnimation(UIElement.OpacityProperty, animation); } else { // 如果不靠近边缘,取消隐藏动画 DoubleAnimation animation = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromSeconds(0.5))); this.BeginAnimation(UIElement.OpacityProperty, animation); } } ``` 这段代码中,通过监听窗口的LocationChanged事件,计算窗口与屏幕边缘的距离,并根据一定的阈值来触发隐藏动画效果。当窗口靠近边缘时,执行隐藏动画动画完成后将窗口的Opacity属性设置为0,实现窗口的隐藏。当窗口远离边缘时,取消隐藏动画,并将窗口的Opacity属性设置为1,使窗口重新显示出来。通过这种方式,就可以实现仿QQ贴边自动隐藏的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值