private Win32.POINT CheckDPI(Win32.POINT point) { Matrix m = PresentationSource.FromVisual(Application.Current.MainWindow).CompositionTarget.TransformToDevice; double dx = m.M11; // notice it's divided by 96 already double dy = m.M22; // notice it's divided by 96 already point.X = (int)(point.X / dx); point.Y = (int)(point.Y / dy); Console.WriteLine("鼠标修改后 " + point.X + ":" + point.Y); return(point); }
void timer_Tick(object sender, EventArgs e) { try { if (!win.IsLoaded) { timer.Stop(); IsWindowDestroyed = true; timer.Enabled = false; return; } Win32.POINT point = new Win32.POINT(); Win32.GetCursorPos(out point); System.Windows.Point mousePositionInApp = Mouse.GetPosition(win); System.Windows.Point mousePositionInScreenCoordinates = win.PointToScreen(mousePositionInApp); Console.WriteLine("-------------------"); Console.WriteLine("鼠标 " + point.X + ":" + point.Y); Console.WriteLine("窗体 " + this.win.Left); point = CheckDPI(point); ////Console.WriteLine("Screen:" + mousePositionInScreenCoordinates); //Console.WriteLine(win.Width+"====="+win.ActualWidth); //Console.WriteLine(win.Height); //Console.WriteLine(win.Top); //Console.WriteLine(point.X + "----" + point.Y+"宽度从"+win.Left+"到"+(win.Left+win.ActualWidth)); //窗体已经在上边缘了 if (this.win.Top <= 1) { //窗体已经隐藏了 if (IsHide) { //鼠标在窗体上边缘了 if (point.Y <= 2 && point.X > this.win.Left && point.X < (this.win.Left + this.win.Width)) { Console.WriteLine("显示"); DoubleAnimation animation = new DoubleAnimation(); animation.From = 0; animation.To = 1; animation.Duration = new Duration(TimeSpan.FromSeconds(0.2)); animation.Completed += (se, es) => { this.win.Visibility = Visibility.Visible; this.win.Activate(); IsHide = false; }; win.BeginAnimation(MainWindow.OpacityProperty, animation); } } //窗体没有隐藏 else { //鼠标离开了 if (point.Y > 50) { Console.WriteLine("隐藏"); //在使用 if (mousePositionInScreenCoordinates.X > 0.1 && mousePositionInScreenCoordinates.Y > 0.1) { return; } IsHide = true; DoubleAnimation animation = new DoubleAnimation(); animation.From = 1; animation.To = 0; animation.Duration = new Duration(TimeSpan.FromSeconds(0.2)); animation.Completed += (se, es) => { this.win.Visibility = Visibility.Hidden; IsHide = true; }; win.BeginAnimation(MainWindow.OpacityProperty, animation); } } } } catch (Exception ex) { Logger.Log("WindowHide.cs").Error("隐藏窗体模块出现异常 " + ex.Message); } }
void timer_Tick(object sender, EventArgs e) { Win32.POINT point = new Win32.POINT(); Win32.GetCursorPos(out point); System.Windows.Point mousePositionInApp = Mouse.GetPosition(win); System.Windows.Point mousePositionInScreenCoordinates = win.PointToScreen(mousePositionInApp); Console.WriteLine("Screen:" + mousePositionInScreenCoordinates); //Console.WriteLine(win.Width+"====="+win.ActualWidth); //Console.WriteLine(win.Height); //Point point = PointToScreen(System.Windows.Input.Mouse.GetPosition(this));//获取鼠标相对桌面的位置 //Console.WriteLine(win.Top); //Console.WriteLine(point.X + "----" + point.Y+"宽度从"+win.Left+"到"+(win.Left+win.ActualWidth)); //窗体已经在上边缘了 if (this.win.Top <= 1) { //窗体已经隐藏了 if (IsHide) { //鼠标在窗体上边缘了 if (point.Y <= 2) { Console.WriteLine("显示"); DoubleAnimation animation = new DoubleAnimation(); animation.From = 0; animation.To = 1; animation.Duration = new Duration(TimeSpan.FromSeconds(0.2)); animation.Completed += (se, es) => { this.win.Visibility = Visibility.Visible; IsHide = false; }; win.BeginAnimation(MainWindow.OpacityProperty, animation); } } //窗体没有隐藏 else { //鼠标离开了 if (point.Y > 50) { Console.WriteLine("隐藏"); //在使用 if (mousePositionInScreenCoordinates.X > 0.1 && mousePositionInScreenCoordinates.Y > 0.1) { return; } IsHide = true; DoubleAnimation animation = new DoubleAnimation(); animation.From = 1; animation.To = 0; animation.Duration = new Duration(TimeSpan.FromSeconds(0.2)); animation.Completed += (se, es) => { this.win.Visibility = Visibility.Hidden; IsHide = true; }; win.BeginAnimation(MainWindow.OpacityProperty, animation); } } } }