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

  • Post author:
  • Post category:其他


全局变量

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;
            



版权声明:本文为weixin_40467130原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。